1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
12 use rustc::hir::{self, HirId};
13 use rustc::lint::builtin::UNUSED_MUT;
15 use rustc::util::nodemap::{FxHashMap, FxHashSet};
19 use borrowck::BorrowckCtxt;
21 pub fn check<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, body: &'tcx hir::Body) {
22 let mut used_mut = bccx.used_mut_nodes.borrow().clone();
26 }.visit_expr(&body.value);
27 let mut cx = UnusedMutCx { bccx, used_mut };
28 for arg in body.arguments.iter() {
29 cx.check_unused_mut_pat(slice::from_ref(&arg.pat));
31 cx.visit_expr(&body.value);
34 struct UsedMutFinder<'a, 'tcx: 'a> {
35 bccx: &'a BorrowckCtxt<'a, 'tcx>,
36 set: &'a mut FxHashSet<HirId>,
39 struct UnusedMutCx<'a, 'tcx: 'a> {
40 bccx: &'a BorrowckCtxt<'a, 'tcx>,
41 used_mut: FxHashSet<HirId>,
44 impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> {
45 fn check_unused_mut_pat(&self, pats: &[P<hir::Pat>]) {
46 let tcx = self.bccx.tcx;
47 let mut mutables = FxHashMap();
49 p.each_binding(|_, hir_id, span, ident| {
50 // Skip anything that looks like `_foo`
51 if ident.as_str().starts_with("_") {
55 // Skip anything that looks like `&foo` or `&mut foo`, only look
56 // for by-value bindings
57 let bm = match self.bccx.tables.pat_binding_modes().get(hir_id) {
59 None => span_bug!(span, "missing binding mode"),
62 ty::BindByValue(hir::MutMutable) => {}
66 mutables.entry(ident.name).or_insert(Vec::new()).push((hir_id, span));
70 for (_name, ids) in mutables {
71 // If any id for this name was used mutably then consider them all
72 // ok, so move on to the next
73 if ids.iter().any(|&(ref hir_id, _)| self.used_mut.contains(hir_id)) {
77 let (hir_id, span) = ids[0];
78 let mut_span = tcx.sess.codemap().span_until_non_whitespace(span);
80 // Ok, every name wasn't used mutably, so issue a warning that this
81 // didn't need to be mutable.
82 tcx.struct_span_lint_hir(UNUSED_MUT,
85 "variable does not need to be mutable")
86 .span_suggestion_short(mut_span, "remove this `mut`", "".to_owned())
92 impl<'a, 'tcx> Visitor<'tcx> for UnusedMutCx<'a, 'tcx> {
93 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
94 NestedVisitorMap::OnlyBodies(&self.bccx.tcx.hir)
97 fn visit_arm(&mut self, arm: &hir::Arm) {
98 self.check_unused_mut_pat(&arm.pats)
101 fn visit_local(&mut self, local: &hir::Local) {
102 self.check_unused_mut_pat(slice::from_ref(&local.pat));
106 impl<'a, 'tcx> Visitor<'tcx> for UsedMutFinder<'a, 'tcx> {
107 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
108 NestedVisitorMap::OnlyBodies(&self.bccx.tcx.hir)
111 fn visit_nested_body(&mut self, id: hir::BodyId) {
112 let def_id = self.bccx.tcx.hir.body_owner_def_id(id);
113 self.set.extend(self.bccx.tcx.borrowck(def_id).used_mut_nodes.iter().cloned());
114 self.visit_body(self.bccx.tcx.hir.body(id));