1
- //! Checks for useless borrowed references.
2
- //!
3
- //! This lint is **warn** by default
4
-
5
1
use crate :: utils:: { snippet_with_applicability, span_lint_and_then} ;
6
2
use if_chain:: if_chain;
7
3
use rustc_errors:: Applicability ;
@@ -10,44 +6,37 @@ use rustc_lint::{LateContext, LateLintPass};
10
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
11
7
12
8
declare_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`.
18
11
///
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.
26
13
///
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>) {
28
18
/// 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
+ /// };
32
22
/// }
33
23
/// ```
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
24
///
39
25
/// **Example:**
26
+ /// Bad:
40
27
/// ```rust
41
28
/// let mut v = Vec::<String>::new();
42
29
/// let _ = v.iter_mut().filter(|&ref a| a.is_empty());
43
30
/// ```
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
+ /// ```
48
37
pub NEEDLESS_BORROWED_REFERENCE ,
49
38
complexity,
50
- "taking a needless borrowed reference "
39
+ "destructuring a reference and borrowing the inner value "
51
40
}
52
41
53
42
declare_lint_pass ! ( NeedlessBorrowedRef => [ NEEDLESS_BORROWED_REFERENCE ] ) ;
0 commit comments