]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_borrowed_ref.rs
Fix ICE for issues 2767, 2499, 1782
[rust.git] / clippy_lints / src / needless_borrowed_ref.rs
1 //! Checks for useless borrowed references.
2 //!
3 //! This lint is **warn** by default
4
5 use rustc::lint::*;
6 use rustc::hir::{BindingAnnotation, MutImmutable, Pat, PatKind};
7 use utils::{in_macro, snippet, span_lint_and_then};
8
9 /// **What it does:** Checks for useless borrowed references.
10 ///
11 /// **Why is this bad?** It is mostly useless and make the code look more
12 /// complex than it
13 /// actually is.
14 ///
15 /// **Known problems:** It seems that the `&ref` pattern is sometimes useful.
16 /// For instance in the following snippet:
17 /// ```rust
18 /// enum Animal {
19 ///     Cat(u64),
20 ///     Dog(u64),
21 /// }
22 ///
23 /// fn foo(a: &Animal, b: &Animal) {
24 ///     match (a, b) {
25 /// (&Animal::Cat(v), k) | (k, &Animal::Cat(v)) => (), // lifetime
26 /// mismatch error
27 ///         (&Animal::Dog(ref c), &Animal::Dog(_)) => ()
28 ///     }
29 /// }
30 /// ```
31 /// There is a lifetime mismatch error for `k` (indeed a and b have distinct
32 /// lifetime).
33 /// This can be fixed by using the `&ref` pattern.
34 /// However, the code can also be fixed by much cleaner ways
35 ///
36 /// **Example:**
37 /// ```rust
38 ///     let mut v = Vec::<String>::new();
39 ///     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
40 /// ```
41 /// This closure takes a reference on something that has been matched as a
42 /// reference and
43 /// de-referenced.
44 /// As such, it could just be |a| a.is_empty()
45 declare_clippy_lint! {
46     pub NEEDLESS_BORROWED_REFERENCE,
47     complexity,
48     "taking a needless borrowed reference"
49 }
50
51 #[derive(Copy, Clone)]
52 pub struct NeedlessBorrowedRef;
53
54 impl LintPass for NeedlessBorrowedRef {
55     fn get_lints(&self) -> LintArray {
56         lint_array!(NEEDLESS_BORROWED_REFERENCE)
57     }
58 }
59
60 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
61     fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
62         if in_macro(pat.span) {
63             // OK, simple enough, lints doesn't check in macro.
64             return;
65         }
66
67         if_chain! {
68             // Only lint immutable refs, because `&mut ref T` may be useful.
69             if let PatKind::Ref(ref sub_pat, MutImmutable) = pat.node;
70
71             // Check sub_pat got a `ref` keyword (excluding `ref mut`).
72             if let PatKind::Binding(BindingAnnotation::Ref, _, spanned_name, ..) = sub_pat.node;
73             then {
74                 span_lint_and_then(cx, NEEDLESS_BORROWED_REFERENCE, pat.span,
75                                    "this pattern takes a reference on something that is being de-referenced",
76                                    |db| {
77                                        let hint = snippet(cx, spanned_name.span, "..").into_owned();
78                                        db.span_suggestion(pat.span, "try removing the `&ref` part and just keep", hint);
79                                    });
80             }
81         }
82     }
83 }