]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/usage.rs
Auto merge of #4165 - BO41:needless_range_loop, r=phansch
[rust.git] / clippy_lints / src / utils / usage.rs
1 use rustc::hir::def::Res;
2 use rustc::hir::*;
3 use rustc::lint::LateContext;
4 use rustc::middle::expr_use_visitor::*;
5 use rustc::middle::mem_categorization::cmt_;
6 use rustc::middle::mem_categorization::Categorization;
7 use rustc::ty;
8 use rustc_data_structures::fx::FxHashSet;
9 use syntax::source_map::Span;
10
11 /// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
12 pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<HirId>> {
13     let mut delegate = MutVarsDelegate {
14         used_mutably: FxHashSet::default(),
15         skip: false,
16     };
17     let def_id = def_id::DefId::local(expr.hir_id.owner);
18     let region_scope_tree = &cx.tcx.region_scope_tree(def_id);
19     ExprUseVisitor::new(
20         &mut delegate,
21         cx.tcx,
22         def_id,
23         cx.param_env,
24         region_scope_tree,
25         cx.tables,
26         None,
27     )
28     .walk_expr(expr);
29
30     if delegate.skip {
31         return None;
32     }
33     Some(delegate.used_mutably)
34 }
35
36 pub fn is_potentially_mutated<'a, 'tcx: 'a>(
37     variable: &'tcx Path,
38     expr: &'tcx Expr,
39     cx: &'a LateContext<'a, 'tcx>,
40 ) -> bool {
41     if let Res::Local(id) = variable.res {
42         mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
43     } else {
44         return true;
45     }
46 }
47
48 struct MutVarsDelegate {
49     used_mutably: FxHashSet<HirId>,
50     skip: bool,
51 }
52
53 impl<'tcx> MutVarsDelegate {
54     #[allow(clippy::similar_names)]
55     fn update(&mut self, cat: &'tcx Categorization<'_>) {
56         match *cat {
57             Categorization::Local(id) => {
58                 self.used_mutably.insert(id);
59             },
60             Categorization::Upvar(_) => {
61                 //FIXME: This causes false negatives. We can't get the `NodeId` from
62                 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
63                 //`while`-body, not just the ones in the condition.
64                 self.skip = true
65             },
66             Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat),
67             _ => {},
68         }
69     }
70 }
71
72 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
73     fn consume(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {}
74
75     fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
76
77     fn consume_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: ConsumeMode) {}
78
79     fn borrow(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) {
80         if let ty::BorrowKind::MutBorrow = bk {
81             self.update(&cmt.cat)
82         }
83     }
84
85     fn mutate(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, _: MutateMode) {
86         self.update(&cmt.cat)
87     }
88
89     fn decl_without_init(&mut self, _: HirId, _: Span) {}
90 }