Skip to content

Commit ee2f547

Browse files
CBenoitoli-obk
authored andcommitted
Finalize needless_borrowed_ref lint doc.
Make sure the needless_borrowed_ref.stderr in examples is up to date too.
1 parent c003931 commit ee2f547

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

clippy_lints/src/needless_borrowed_ref.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@
33
//! This lint is **warn** by default
44
55
use rustc::lint::*;
6-
<<<<<<< HEAD
7-
use rustc::hir::{MutImmutable, Pat, PatKind, BindingAnnotation};
8-
=======
9-
use rustc::hir::{MutImmutable, Pat, PatKind};
10-
<<<<<<< HEAD
11-
>>>>>>> e30bf721... Improve needless_borrowed_ref and add suggestion to it.
6+
use rustc::hir::{MutImmutable, Pat, PatKind, BindByRef};
127
use rustc::ty;
13-
=======
14-
>>>>>>> 4ae45c87... Improve needless_borrowed_ref lint: remove the hand rolled span part.
158
use utils::{span_lint_and_then, in_macro, snippet};
16-
use rustc::hir::BindingMode::BindByRef;
179

1810
/// **What it does:** Checks for useless borrowed references.
1911
///
20-
/// **Why is this bad?** It is completely useless and make the code look more
21-
/// complex than it
12+
/// **Why is this bad?** It is mostly useless and make the code look more complex than it
2213
/// actually is.
2314
///
24-
/// **Known problems:** None.
15+
/// **Known problems:** It seems that the `&ref` pattern is sometimes useful.
16+
/// For instance in the following snippet:
17+
/// ```rust
18+
/// enum Animal {
19+
/// Cat(u64),
20+
/// Dog(u64),
21+
/// }
22+
///
23+
/// fn foo(a: &Animal, b: &Animal) {
24+
/// match (a, b) {
25+
/// (&Animal::Cat(v), k) | (k, &Animal::Cat(v)) => (), // lifetime mismatch error
26+
/// (&Animal::Dog(ref c), &Animal::Dog(_)) => ()
27+
/// }
28+
/// }
29+
/// ```
30+
/// There is a lifetime mismatch error for `k` (indeed a and b have distinct lifetime).
31+
/// This can be fixed by using the `&ref` pattern.
32+
/// However, the code can also be fixed by much cleaner ways
2533
///
2634
/// **Example:**
2735
/// ```rust
@@ -75,3 +83,4 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
7583
}}
7684
}
7785
}
86+

clippy_tests/examples/needless_borrowed_ref.stderr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this pattern takes a reference on something that is being de-referenced
2-
--> examples/needless_borrowed_ref.rs:8:34
2+
--> needless_borrowed_ref.rs:8:34
33
|
44
8 | let _ = v.iter_mut().filter(|&ref a| a.is_empty());
55
| ^^^^^^ help: try removing the `&ref` part and just keep `a`
@@ -8,30 +8,27 @@ error: this pattern takes a reference on something that is being de-referenced
88
= help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference
99

1010
error: this pattern takes a reference on something that is being de-referenced
11-
--> examples/needless_borrowed_ref.rs:13:17
11+
--> needless_borrowed_ref.rs:13:17
1212
|
1313
13 | if let Some(&ref v) = thingy {
1414
| ^^^^^^ help: try removing the `&ref` part and just keep `v`
1515
|
16-
= note: `-D needless-borrowed-reference` implied by `-D warnings`
1716
= help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference
1817

1918
error: this pattern takes a reference on something that is being de-referenced
20-
--> examples/needless_borrowed_ref.rs:42:27
19+
--> needless_borrowed_ref.rs:42:27
2120
|
2221
42 | (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref'
2322
| ^^^^^^ help: try removing the `&ref` part and just keep `k`
2423
|
25-
= note: `-D needless-borrowed-reference` implied by `-D warnings`
2624
= help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference
2725

2826
error: this pattern takes a reference on something that is being de-referenced
29-
--> examples/needless_borrowed_ref.rs:42:38
27+
--> needless_borrowed_ref.rs:42:38
3028
|
3129
42 | (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref'
3230
| ^^^^^^ help: try removing the `&ref` part and just keep `k`
3331
|
34-
= note: `-D needless-borrowed-reference` implied by `-D warnings`
3532
= help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference
3633

3734
error: aborting due to previous error(s)

0 commit comments

Comments
 (0)