]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrow.rs
Auto merge of #3706 - robamler:patch-1, r=phansch
[rust.git] / clippy_lints / src / needless_borrow.rs
1 //! Checks for needless address of operations (`&`)
2 //!
3 //! This lint is **warn** by default
4
5 use crate::utils::{in_macro, snippet_opt, span_lint_and_then};
6 use if_chain::if_chain;
7 use rustc::hir::{BindingAnnotation, Expr, ExprKind, Item, MutImmutable, Pat, PatKind};
8 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
9 use rustc::ty;
10 use rustc::ty::adjustment::{Adjust, Adjustment};
11 use rustc::{declare_tool_lint, lint_array};
12 use rustc_errors::Applicability;
13 use syntax::ast::NodeId;
14
15 /// **What it does:** Checks for address of operations (`&`) that are going to
16 /// be dereferenced immediately by the compiler.
17 ///
18 /// **Why is this bad?** Suggests that the receiver of the expression borrows
19 /// the expression.
20 ///
21 /// **Example:**
22 /// ```rust
23 /// let x: &i32 = &&&&&&5;
24 /// ```
25 ///
26 /// **Known problems:** None.
27 declare_clippy_lint! {
28     pub NEEDLESS_BORROW,
29     nursery,
30     "taking a reference that is going to be automatically dereferenced"
31 }
32
33 #[derive(Default)]
34 pub struct NeedlessBorrow {
35     derived_item: Option<NodeId>,
36 }
37
38 impl LintPass for NeedlessBorrow {
39     fn get_lints(&self) -> LintArray {
40         lint_array!(NEEDLESS_BORROW)
41     }
42
43     fn name(&self) -> &'static str {
44         "NeedlessBorrow"
45     }
46 }
47
48 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
49     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
50         if in_macro(e.span) || self.derived_item.is_some() {
51             return;
52         }
53         if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node {
54             if let ty::Ref(..) = cx.tables.expr_ty(inner).sty {
55                 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
56                     if let [Adjustment {
57                         kind: Adjust::Deref(_), ..
58                     }, Adjustment {
59                         kind: Adjust::Deref(_), ..
60                     }, Adjustment {
61                         kind: Adjust::Borrow(_),
62                         ..
63                     }] = *adj3
64                     {
65                         span_lint_and_then(
66                             cx,
67                             NEEDLESS_BORROW,
68                             e.span,
69                             "this expression borrows a reference that is immediately dereferenced \
70                              by the compiler",
71                             |db| {
72                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
73                                     db.span_suggestion(
74                                         e.span,
75                                         "change this to",
76                                         snippet,
77                                         Applicability::MachineApplicable,
78                                     );
79                                 }
80                             },
81                         );
82                     }
83                 }
84             }
85         }
86     }
87     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
88         if in_macro(pat.span) || self.derived_item.is_some() {
89             return;
90         }
91         if_chain! {
92             if let PatKind::Binding(BindingAnnotation::Ref, _, name, _) = pat.node;
93             if let ty::Ref(_, tam, mutbl) = cx.tables.pat_ty(pat).sty;
94             if mutbl == MutImmutable;
95             if let ty::Ref(_, _, mutbl) = tam.sty;
96             // only lint immutable refs, because borrowed `&mut T` cannot be moved out
97             if mutbl == MutImmutable;
98             then {
99                 span_lint_and_then(
100                     cx,
101                     NEEDLESS_BORROW,
102                     pat.span,
103                     "this pattern creates a reference to a reference",
104                     |db| {
105                         if let Some(snippet) = snippet_opt(cx, name.span) {
106                             db.span_suggestion(
107                                 pat.span,
108                                 "change this to",
109                                 snippet,
110                                 Applicability::MachineApplicable,
111                             );
112                         }
113                     }
114                 )
115             }
116         }
117     }
118
119     fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
120         if item.attrs.iter().any(|a| a.check_name("automatically_derived")) {
121             debug_assert!(self.derived_item.is_none());
122             self.derived_item = Some(item.id);
123         }
124     }
125
126     fn check_item_post(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
127         if let Some(id) = self.derived_item {
128             if item.id == id {
129                 self.derived_item = None;
130             }
131         }
132     }
133 }