]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/utils/usage.rs
0492878fc272f19a8028fa6ac04a168fd76c6a8b
[rust.git] / src / tools / clippy / clippy_lints / src / utils / usage.rs
1 use crate::utils::match_var;
2 use rustc_data_structures::fx::FxHashSet;
3 use rustc_hir::def::Res;
4 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc_hir::{Expr, HirId, Path};
6 use rustc_infer::infer::TyCtxtInferExt;
7 use rustc_lint::LateContext;
8 use rustc_middle::hir::map::Map;
9 use rustc_middle::ty;
10 use rustc_span::symbol::{Ident, Symbol};
11 use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
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>(expr: &'tcx Expr<'_>, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<HirId>> {
15     let mut delegate = MutVarsDelegate {
16         used_mutably: FxHashSet::default(),
17         skip: false,
18     };
19     let def_id = expr.hir_id.owner.to_def_id();
20     cx.tcx.infer_ctxt().enter(|infcx| {
21         ExprUseVisitor::new(&mut delegate, &infcx, def_id.expect_local(), cx.param_env, cx.tables).walk_expr(expr);
22     });
23
24     if delegate.skip {
25         return None;
26     }
27     Some(delegate.used_mutably)
28 }
29
30 pub fn is_potentially_mutated<'a, 'tcx>(
31     variable: &'tcx Path<'_>,
32     expr: &'tcx Expr<'_>,
33     cx: &'a LateContext<'a, 'tcx>,
34 ) -> bool {
35     if let Res::Local(id) = variable.res {
36         mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
37     } else {
38         true
39     }
40 }
41
42 struct MutVarsDelegate {
43     used_mutably: FxHashSet<HirId>,
44     skip: bool,
45 }
46
47 impl<'tcx> MutVarsDelegate {
48     #[allow(clippy::similar_names)]
49     fn update(&mut self, cat: &PlaceWithHirId<'tcx>) {
50         match cat.place.base {
51             PlaceBase::Local(id) => {
52                 self.used_mutably.insert(id);
53             },
54             PlaceBase::Upvar(_) => {
55                 //FIXME: This causes false negatives. We can't get the `NodeId` from
56                 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
57                 //`while`-body, not just the ones in the condition.
58                 self.skip = true
59             },
60             _ => {},
61         }
62     }
63 }
64
65 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
66     fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: ConsumeMode) {}
67
68     fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
69         if let ty::BorrowKind::MutBorrow = bk {
70             self.update(&cmt)
71         }
72     }
73
74     fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
75         self.update(&cmt)
76     }
77 }
78
79 pub struct UsedVisitor {
80     pub var: Symbol, // var to look for
81     pub used: bool,  // has the var been used otherwise?
82 }
83
84 impl<'tcx> Visitor<'tcx> for UsedVisitor {
85     type Map = Map<'tcx>;
86
87     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
88         if match_var(expr, self.var) {
89             self.used = true;
90         } else {
91             walk_expr(self, expr);
92         }
93     }
94
95     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
96         NestedVisitorMap::None
97     }
98 }
99
100 pub fn is_unused<'tcx>(ident: &'tcx Ident, body: &'tcx Expr<'_>) -> bool {
101     let mut visitor = UsedVisitor {
102         var: ident.name,
103         used: false,
104     };
105     walk_expr(&mut visitor, body);
106     !visitor.used
107 }