]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrow.rs
Switch to declare_tool_lint macro
[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 rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
7 use if_chain::if_chain;
8 use rustc::hir::{BindingAnnotation, Expr, ExprKind, MutImmutable, Pat, PatKind};
9 use rustc::ty;
10 use rustc::ty::adjustment::{Adjust, Adjustment};
11 use crate::utils::{in_macro, snippet_opt, span_lint_and_then};
12
13 /// **What it does:** Checks for address of operations (`&`) that are going to
14 /// be dereferenced immediately by the compiler.
15 ///
16 /// **Why is this bad?** Suggests that the receiver of the expression borrows
17 /// the expression.
18 ///
19 /// **Example:**
20 /// ```rust
21 /// let x: &i32 = &&&&&&5;
22 /// ```
23 ///
24 /// **Known problems:** This will cause false positives in code generated by `derive`.
25 /// For instance in the following snippet:
26 /// ```rust
27 /// #[derive(Debug)]
28 /// pub enum Error {
29 ///     Type(
30 ///         &'static str,
31 ///     ),
32 /// }
33 /// ```
34 /// A warning will be emitted that `&'static str` should be replaced with `&'static str`,
35 /// however there is nothing that can or should be done to fix this.
36 declare_clippy_lint! {
37     pub NEEDLESS_BORROW,
38     nursery,
39     "taking a reference that is going to be automatically dereferenced"
40 }
41
42 #[derive(Copy, Clone)]
43 pub struct NeedlessBorrow;
44
45 impl LintPass for NeedlessBorrow {
46     fn get_lints(&self) -> LintArray {
47         lint_array!(NEEDLESS_BORROW)
48     }
49 }
50
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
52     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
53         if in_macro(e.span) {
54             return;
55         }
56         if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node {
57             if let ty::Ref(..) = cx.tables.expr_ty(inner).sty {
58                 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
59                     if let [Adjustment {
60                         kind: Adjust::Deref(_),
61                         ..
62                     }, Adjustment {
63                         kind: Adjust::Deref(_),
64                         ..
65                     }, Adjustment {
66                         kind: Adjust::Borrow(_),
67                         ..
68                     }] = *adj3
69                     {
70                         span_lint_and_then(
71                             cx,
72                             NEEDLESS_BORROW,
73                             e.span,
74                             "this expression borrows a reference that is immediately dereferenced \
75                              by the compiler",
76                             |db| {
77                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
78                                     db.span_suggestion(e.span, "change this to", snippet);
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) {
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(pat.span, "change this to", snippet);
107                         }
108                     }
109                 )
110             }
111         }
112     }
113 }