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