]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/usage.rs
Auto merge of #4061 - rust-lang:rustup, 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(&mut delegate, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(expr);
20
21     if delegate.skip {
22         return None;
23     }
24     Some(delegate.used_mutably)
25 }
26
27 pub fn is_potentially_mutated<'a, 'tcx: 'a>(
28     variable: &'tcx Path,
29     expr: &'tcx Expr,
30     cx: &'a LateContext<'a, 'tcx>,
31 ) -> bool {
32     let id = match variable.res {
33         Res::Local(id) | Res::Upvar(id, ..) => id,
34         _ => return true,
35     };
36     mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
37 }
38
39 struct MutVarsDelegate {
40     used_mutably: FxHashSet<HirId>,
41     skip: bool,
42 }
43
44 impl<'tcx> MutVarsDelegate {
45     #[allow(clippy::similar_names)]
46     fn update(&mut self, cat: &'tcx Categorization<'_>) {
47         match *cat {
48             Categorization::Local(id) => {
49                 self.used_mutably.insert(id);
50             },
51             Categorization::Upvar(_) => {
52                 //FIXME: This causes false negatives. We can't get the `NodeId` from
53                 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
54                 //`while`-body, not just the ones in the condition.
55                 self.skip = true
56             },
57             Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat),
58             _ => {},
59         }
60     }
61 }
62
63 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
64     fn consume(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {}
65
66     fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
67
68     fn consume_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: ConsumeMode) {}
69
70     fn borrow(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) {
71         if let ty::BorrowKind::MutBorrow = bk {
72             self.update(&cmt.cat)
73         }
74     }
75
76     fn mutate(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, _: MutateMode) {
77         self.update(&cmt.cat)
78     }
79
80     fn decl_without_init(&mut self, _: HirId, _: Span) {}
81 }