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