Skip to content

Commit a1e25a9

Browse files
committed
Auto merge of #6794 - camsteffen:needless-borrowed-ref, r=flip1995
Improve needless_borrowed_ref docs changelog: none I think "borrowed ref" is a confusing description for this lint. Destructuring a reference is the opposite of borrowing. So I updated the wording throughout the docs. Unfortunately this nit applies to the name of the lint itself, but I won't bother changing that. One motivation for these changes is to clarify the difference between this lint and `needless_borrow` (they are actually quite different). Let me know if I need to clarify anything or if you disagree with any changes.
2 parents fb27517 + 5dbd45c commit a1e25a9

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

clippy_lints/src/needless_borrowed_ref.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//! Checks for useless borrowed references.
2-
//!
3-
//! This lint is **warn** by default
4-
51
use crate::utils::{snippet_with_applicability, span_lint_and_then};
62
use if_chain::if_chain;
73
use rustc_errors::Applicability;
@@ -10,44 +6,37 @@ use rustc_lint::{LateContext, LateLintPass};
106
use rustc_session::{declare_lint_pass, declare_tool_lint};
117

128
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`.
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

5342
declare_lint_pass!(NeedlessBorrowedRef => [NEEDLESS_BORROWED_REFERENCE]);

0 commit comments

Comments
 (0)