]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrow.rs
Auto merge of #4808 - euclio:string-lit-as-bytes, 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::{snippet_opt, span_lint_and_then};
6 use if_chain::if_chain;
7 use rustc::hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, HirId, Item, Mutability, 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, impl_lint_pass};
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_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]);
38
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
40     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
41         if e.span.from_expansion() || self.derived_item.is_some() {
42             return;
43         }
44         if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Immutable, ref inner) = e.kind {
45             if let ty::Ref(..) = cx.tables.expr_ty(inner).kind {
46                 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
47                     if let [Adjustment {
48                         kind: Adjust::Deref(_), ..
49                     }, Adjustment {
50                         kind: Adjust::Deref(_), ..
51                     }, Adjustment {
52                         kind: Adjust::Borrow(_),
53                         ..
54                     }] = *adj3
55                     {
56                         span_lint_and_then(
57                             cx,
58                             NEEDLESS_BORROW,
59                             e.span,
60                             "this expression borrows a reference that is immediately dereferenced \
61                              by the compiler",
62                             |db| {
63                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
64                                     db.span_suggestion(
65                                         e.span,
66                                         "change this to",
67                                         snippet,
68                                         Applicability::MachineApplicable,
69                                     );
70                                 }
71                             },
72                         );
73                     }
74                 }
75             }
76         }
77     }
78     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
79         if pat.span.from_expansion() || self.derived_item.is_some() {
80             return;
81         }
82         if_chain! {
83             if let PatKind::Binding(BindingAnnotation::Ref, .., name, _) = pat.kind;
84             if let ty::Ref(_, tam, mutbl) = cx.tables.pat_ty(pat).kind;
85             if mutbl == Mutability::Immutable;
86             if let ty::Ref(_, _, mutbl) = tam.kind;
87             // only lint immutable refs, because borrowed `&mut T` cannot be moved out
88             if mutbl == Mutability::Immutable;
89             then {
90                 span_lint_and_then(
91                     cx,
92                     NEEDLESS_BORROW,
93                     pat.span,
94                     "this pattern creates a reference to a reference",
95                     |db| {
96                         if let Some(snippet) = snippet_opt(cx, name.span) {
97                             db.span_suggestion(
98                                 pat.span,
99                                 "change this to",
100                                 snippet,
101                                 Applicability::MachineApplicable,
102                             );
103                         }
104                     }
105                 )
106             }
107         }
108     }
109
110     fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
111         if item.attrs.iter().any(|a| a.check_name(sym!(automatically_derived))) {
112             debug_assert!(self.derived_item.is_none());
113             self.derived_item = Some(item.hir_id);
114         }
115     }
116
117     fn check_item_post(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
118         if let Some(id) = self.derived_item {
119             if item.hir_id == id {
120                 self.derived_item = None;
121             }
122         }
123     }
124 }