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