]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/usage.rs
Introduce snippet_with_applicability and hir_with_applicability functions
[rust.git] / clippy_lints / src / utils / usage.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 use crate::rustc::lint::LateContext;
12
13 use crate::rustc::hir::def::Def;
14 use crate::rustc::hir::*;
15 use crate::rustc::middle::expr_use_visitor::*;
16 use crate::rustc::middle::mem_categorization::cmt_;
17 use crate::rustc::middle::mem_categorization::Categorization;
18 use crate::rustc::ty;
19 use crate::rustc_data_structures::fx::FxHashSet;
20 use crate::syntax::ast::NodeId;
21 use crate::syntax::source_map::Span;
22
23 /// Returns a set of mutated local variable ids or None if mutations could not be determined.
24 pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<NodeId>> {
25     let mut delegate = MutVarsDelegate {
26         used_mutably: FxHashSet::default(),
27         skip: false,
28     };
29     let def_id = def_id::DefId::local(expr.hir_id.owner);
30     let region_scope_tree = &cx.tcx.region_scope_tree(def_id);
31     ExprUseVisitor::new(&mut delegate, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(expr);
32
33     if delegate.skip {
34         return None;
35     }
36     Some(delegate.used_mutably)
37 }
38
39 pub fn is_potentially_mutated<'a, 'tcx: 'a>(
40     variable: &'tcx Path,
41     expr: &'tcx Expr,
42     cx: &'a LateContext<'a, 'tcx>,
43 ) -> bool {
44     let id = match variable.def {
45         Def::Local(id) | Def::Upvar(id, ..) => id,
46         _ => return true,
47     };
48     mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
49 }
50
51 struct MutVarsDelegate {
52     used_mutably: FxHashSet<NodeId>,
53     skip: bool,
54 }
55
56 impl<'tcx> MutVarsDelegate {
57     #[allow(clippy::similar_names)]
58     fn update(&mut self, cat: &'tcx Categorization<'_>) {
59         match *cat {
60             Categorization::Local(id) => {
61                 self.used_mutably.insert(id);
62             },
63             Categorization::Upvar(_) => {
64                 //FIXME: This causes false negatives. We can't get the `NodeId` from
65                 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
66                 //`while`-body, not just the ones in the condition.
67                 self.skip = true
68             },
69             Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat),
70             _ => {},
71         }
72     }
73 }
74
75 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
76     fn consume(&mut self, _: NodeId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {}
77
78     fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
79
80     fn consume_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: ConsumeMode) {}
81
82     fn borrow(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) {
83         if let ty::BorrowKind::MutBorrow = bk {
84             self.update(&cmt.cat)
85         }
86     }
87
88     fn mutate(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: MutateMode) {
89         self.update(&cmt.cat)
90     }
91
92     fn decl_without_init(&mut self, _: NodeId, _: Span) {}
93 }