]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrow.rs
Merge pull request #3191 from vi/suggest_with_applicability
[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::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use crate::rustc::{declare_tool_lint, lint_array};
7 use if_chain::if_chain;
8 use crate::rustc::hir::{BindingAnnotation, Expr, ExprKind, MutImmutable, Pat, PatKind};
9 use crate::rustc::ty;
10 use crate::rustc::ty::adjustment::{Adjust, Adjustment};
11 use crate::utils::{in_macro, snippet_opt, span_lint_and_then};
12 use crate::rustc_errors::Applicability;
13
14 /// **What it does:** Checks for address of operations (`&`) that are going to
15 /// be dereferenced immediately by the compiler.
16 ///
17 /// **Why is this bad?** Suggests that the receiver of the expression borrows
18 /// the expression.
19 ///
20 /// **Example:**
21 /// ```rust
22 /// let x: &i32 = &&&&&&5;
23 /// ```
24 ///
25 /// **Known problems:** This will cause false positives in code generated by `derive`.
26 /// For instance in the following snippet:
27 /// ```rust
28 /// #[derive(Debug)]
29 /// pub enum Error {
30 ///     Type(
31 ///         &'static str,
32 ///     ),
33 /// }
34 /// ```
35 /// A warning will be emitted that `&'static str` should be replaced with `&'static str`,
36 /// however there is nothing that can or should be done to fix this.
37 declare_clippy_lint! {
38     pub NEEDLESS_BORROW,
39     nursery,
40     "taking a reference that is going to be automatically dereferenced"
41 }
42
43 #[derive(Copy, Clone)]
44 pub struct NeedlessBorrow;
45
46 impl LintPass for NeedlessBorrow {
47     fn get_lints(&self) -> LintArray {
48         lint_array!(NEEDLESS_BORROW)
49     }
50 }
51
52 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
53     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
54         if in_macro(e.span) {
55             return;
56         }
57         if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node {
58             if let ty::Ref(..) = cx.tables.expr_ty(inner).sty {
59                 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
60                     if let [Adjustment {
61                         kind: Adjust::Deref(_),
62                         ..
63                     }, Adjustment {
64                         kind: Adjust::Deref(_),
65                         ..
66                     }, Adjustment {
67                         kind: Adjust::Borrow(_),
68                         ..
69                     }] = *adj3
70                     {
71                         span_lint_and_then(
72                             cx,
73                             NEEDLESS_BORROW,
74                             e.span,
75                             "this expression borrows a reference that is immediately dereferenced \
76                              by the compiler",
77                             |db| {
78                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
79                                     db.span_suggestion_with_applicability(
80                                         e.span, 
81                                         "change this to",
82                                         snippet,
83                                         Applicability::MachineApplicable,
84                                     );
85                                 }
86                             },
87                         );
88                     }
89                 }
90             }
91         }
92     }
93     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
94         if in_macro(pat.span) {
95             return;
96         }
97         if_chain! {
98             if let PatKind::Binding(BindingAnnotation::Ref, _, name, _) = pat.node;
99             if let ty::Ref(_, tam, mutbl) = cx.tables.pat_ty(pat).sty;
100             if mutbl == MutImmutable;
101             if let ty::Ref(_, _, mutbl) = tam.sty;
102             // only lint immutable refs, because borrowed `&mut T` cannot be moved out
103             if mutbl == MutImmutable;
104             then {
105                 span_lint_and_then(
106                     cx,
107                     NEEDLESS_BORROW,
108                     pat.span,
109                     "this pattern creates a reference to a reference",
110                     |db| {
111                         if let Some(snippet) = snippet_opt(cx, name.span) {
112                             db.span_suggestion_with_applicability(
113                                 pat.span,
114                                 "change this to",
115                                 snippet,
116                                 Applicability::MachineApplicable,
117                             );
118                         }
119                     }
120                 )
121             }
122         }
123     }
124 }