]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrow.rs
Merge branch 'master' into issue2894
[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::*;
6 use rustc::{declare_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 /// **Known problems:** None.
20 ///
21 /// **Example:**
22 /// ```rust
23 /// let x: &i32 = &&&&&&5;
24 /// ```
25 declare_clippy_lint! {
26     pub NEEDLESS_BORROW,
27     nursery,
28     "taking a reference that is going to be automatically dereferenced"
29 }
30
31 #[derive(Copy, Clone)]
32 pub struct NeedlessBorrow;
33
34 impl LintPass for NeedlessBorrow {
35     fn get_lints(&self) -> LintArray {
36         lint_array!(NEEDLESS_BORROW)
37     }
38 }
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow {
41     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
42         if in_macro(e.span) {
43             return;
44         }
45         if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node {
46             if let ty::TyRef(..) = cx.tables.expr_ty(inner).sty {
47                 for adj3 in cx.tables.expr_adjustments(e).windows(3) {
48                     if let [Adjustment {
49                         kind: Adjust::Deref(_),
50                         ..
51                     }, Adjustment {
52                         kind: Adjust::Deref(_),
53                         ..
54                     }, Adjustment {
55                         kind: Adjust::Borrow(_),
56                         ..
57                     }] = *adj3
58                     {
59                         span_lint_and_then(
60                             cx,
61                             NEEDLESS_BORROW,
62                             e.span,
63                             "this expression borrows a reference that is immediately dereferenced \
64                              by the compiler",
65                             |db| {
66                                 if let Some(snippet) = snippet_opt(cx, inner.span) {
67                                     db.span_suggestion(e.span, "change this to", snippet);
68                                 }
69                             },
70                         );
71                     }
72                 }
73             }
74         }
75     }
76     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
77         if in_macro(pat.span) {
78             return;
79         }
80         if_chain! {
81             if let PatKind::Binding(BindingAnnotation::Ref, _, name, _) = pat.node;
82             if let ty::TyRef(_, tam, mutbl) = cx.tables.pat_ty(pat).sty;
83             if mutbl == MutImmutable;
84             if let ty::TyRef(_, _, mutbl) = tam.sty;
85             // only lint immutable refs, because borrowed `&mut T` cannot be moved out
86             if mutbl == MutImmutable;
87             then {
88                 span_lint_and_then(
89                     cx,
90                     NEEDLESS_BORROW,
91                     pat.span,
92                     "this pattern creates a reference to a reference",
93                     |db| {
94                         if let Some(snippet) = snippet_opt(cx, name.span) {
95                             db.span_suggestion(pat.span, "change this to", snippet);
96                         }
97                     }
98                 )
99             }
100         }
101     }
102 }