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