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