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