]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrow.rs
Auto merge of #3824 - phansch:adding_lints, 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, HirId, 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
14 declare_clippy_lint! {
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     pub NEEDLESS_BORROW,
28     nursery,
29     "taking a reference that is going to be automatically dereferenced"
30 }
31
32 #[derive(Default)]
33 pub struct NeedlessBorrow {
34     derived_item: Option<HirId>,
35 }
36
37 impl LintPass for NeedlessBorrow {
38     fn get_lints(&self) -> LintArray {
39         lint_array!(NEEDLESS_BORROW)
40     }
41
42     fn name(&self) -> &'static str {
43         "NeedlessBorrow"
44     }
45 }
46
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
48     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
49         if in_macro(e.span) || self.derived_item.is_some() {
50             return;
51         }
52         if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node {
53             if let ty::Ref(..) = cx.tables.expr_ty(inner).sty {
54                 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
55                     if let [Adjustment {
56                         kind: Adjust::Deref(_), ..
57                     }, Adjustment {
58                         kind: Adjust::Deref(_), ..
59                     }, Adjustment {
60                         kind: Adjust::Borrow(_),
61                         ..
62                     }] = *adj3
63                     {
64                         span_lint_and_then(
65                             cx,
66                             NEEDLESS_BORROW,
67                             e.span,
68                             "this expression borrows a reference that is immediately dereferenced \
69                              by the compiler",
70                             |db| {
71                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
72                                     db.span_suggestion(
73                                         e.span,
74                                         "change this to",
75                                         snippet,
76                                         Applicability::MachineApplicable,
77                                     );
78                                 }
79                             },
80                         );
81                     }
82                 }
83             }
84         }
85     }
86     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
87         if in_macro(pat.span) || self.derived_item.is_some() {
88             return;
89         }
90         if_chain! {
91             if let PatKind::Binding(BindingAnnotation::Ref, .., name, _) = pat.node;
92             if let ty::Ref(_, tam, mutbl) = cx.tables.pat_ty(pat).sty;
93             if mutbl == MutImmutable;
94             if let ty::Ref(_, _, mutbl) = tam.sty;
95             // only lint immutable refs, because borrowed `&mut T` cannot be moved out
96             if mutbl == MutImmutable;
97             then {
98                 span_lint_and_then(
99                     cx,
100                     NEEDLESS_BORROW,
101                     pat.span,
102                     "this pattern creates a reference to a reference",
103                     |db| {
104                         if let Some(snippet) = snippet_opt(cx, name.span) {
105                             db.span_suggestion(
106                                 pat.span,
107                                 "change this to",
108                                 snippet,
109                                 Applicability::MachineApplicable,
110                             );
111                         }
112                     }
113                 )
114             }
115         }
116     }
117
118     fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
119         if item.attrs.iter().any(|a| a.check_name("automatically_derived")) {
120             debug_assert!(self.derived_item.is_none());
121             self.derived_item = Some(item.hir_id);
122         }
123     }
124
125     fn check_item_post(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
126         if let Some(id) = self.derived_item {
127             if item.hir_id == id {
128                 self.derived_item = None;
129             }
130         }
131     }
132 }