1- //! Checks for useless borrowed references.
2- //!
3- //! This lint is **warn** by default
4-
51use crate :: utils:: { snippet_with_applicability, span_lint_and_then} ;
62use if_chain:: if_chain;
73use rustc_errors:: Applicability ;
@@ -10,44 +6,37 @@ use rustc_lint::{LateContext, LateLintPass};
106use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
117
128declare_clippy_lint ! {
13- /// **What it does:** Checks for useless borrowed references.
14- ///
15- /// **Why is this bad?** It is mostly useless and make the code look more
16- /// complex than it
17- /// actually is.
9+ /// **What it does:** Checks for bindings that destructure a reference and borrow the inner
10+ /// value with `&ref`.
1811 ///
19- /// **Known problems:** It seems that the `&ref` pattern is sometimes useful.
20- /// For instance in the following snippet:
21- /// ```rust,ignore
22- /// enum Animal {
23- /// Cat(u64),
24- /// Dog(u64),
25- /// }
12+ /// **Why is this bad?** This pattern has no effect in almost all cases.
2613 ///
27- /// fn foo(a: &Animal, b: &Animal) {
14+ /// **Known problems:** In some cases, `&ref` is needed to avoid a lifetime mismatch error.
15+ /// Example:
16+ /// ```rust
17+ /// fn foo(a: &Option<String>, b: &Option<String>) {
2818 /// match (a, b) {
29- /// (&Animal::Cat(v), k ) | (k, &Animal::Cat(v)) => (), // lifetime mismatch error
30- /// (&Animal::Dog (ref c), &Animal::Dog(_)) => ()
31- /// }
19+ /// (None, &ref c ) | (&ref c, None) => (),
20+ /// (&Some (ref c), _) => (),
21+ /// };
3222 /// }
3323 /// ```
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
3824 ///
3925 /// **Example:**
26+ /// Bad:
4027 /// ```rust
4128 /// let mut v = Vec::<String>::new();
4229 /// let _ = v.iter_mut().filter(|&ref a| a.is_empty());
4330 /// ```
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()
31+ ///
32+ /// Good:
33+ /// ```rust
34+ /// let mut v = Vec::<String>::new();
35+ /// let _ = v.iter_mut().filter(|a| a.is_empty());
36+ /// ```
4837 pub NEEDLESS_BORROWED_REFERENCE ,
4938 complexity,
50- "taking a needless borrowed reference "
39+ "destructuring a reference and borrowing the inner value "
5140}
5241
5342declare_lint_pass ! ( NeedlessBorrowedRef => [ NEEDLESS_BORROWED_REFERENCE ] ) ;
0 commit comments