From fe57fdd15e6046720463491359d32c5833ffad9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Sat, 1 Jul 2017 12:02:00 +0200 Subject: [PATCH] Improve needless_borrowed_ref and update its stderr. --- .../examples/needless_borrowed_ref.rs | 21 ++++++--- .../examples/needless_borrowed_ref.stderr | 43 ++++++++++++++++--- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/clippy_tests/examples/needless_borrowed_ref.rs b/clippy_tests/examples/needless_borrowed_ref.rs index e463567ee6f..4e9986561bc 100644 --- a/clippy_tests/examples/needless_borrowed_ref.rs +++ b/clippy_tests/examples/needless_borrowed_ref.rs @@ -8,13 +8,24 @@ fn main() { let _ = v.iter_mut().filter(|&ref a| a.is_empty()); // ^ should be linted - let mut var = 5; - let thingy = Some(&mut var); - if let Some(&mut ref v) = thingy { + let var = 3; + let thingy = Some(&var); + if let Some(&ref v) = thingy { + // ^ should be linted + } + + let mut var2 = 5; + let thingy2 = Some(&mut var2); + if let Some(&mut ref mut v) = thingy2 { // ^ should *not* be linted - // here, var is borrowed as immutable. + // v is borrowed as mutable. + *v = 10; + } + if let Some(&mut ref v) = thingy2 { + // ^ should *not* be linted + // here, v is borrowed as immutable. // can't do that: - //*v = 10; + //*v = 15; } } diff --git a/clippy_tests/examples/needless_borrowed_ref.stderr b/clippy_tests/examples/needless_borrowed_ref.stderr index 658318a3c6a..a9befd5767c 100644 --- a/clippy_tests/examples/needless_borrowed_ref.stderr +++ b/clippy_tests/examples/needless_borrowed_ref.stderr @@ -1,8 +1,41 @@ -warning: this pattern takes a reference on something that is being de-referenced - --> needless_borrowed_ref.rs:7:35 +error: this pattern takes a reference on something that is being de-referenced + --> examples/needless_borrowed_ref.rs:8:34 | -7 | let _ = v.iter_mut().filter(|&ref a| a.is_empty()); - | ^^^^^ +8 | let _ = v.iter_mut().filter(|&ref a| a.is_empty()); + | ^^^^^^ help: try removing the `&ref` part and just keep `a` | - = note: #[warn(needless_borrowed_reference)] on by default + = note: `-D needless-borrowed-reference` implied by `-D warnings` = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference + +error: this pattern takes a reference on something that is being de-referenced + --> examples/needless_borrowed_ref.rs:13:17 + | +13 | if let Some(&ref v) = thingy { + | ^^^^^^ help: try removing the `&ref` part and just keep `v` + | + = note: `-D needless-borrowed-reference` implied by `-D warnings` + = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference + +error: this pattern takes a reference on something that is being de-referenced + --> examples/needless_borrowed_ref.rs:42:27 + | +42 | (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref' + | ^^^^^^ help: try removing the `&ref` part and just keep `k` + | + = note: `-D needless-borrowed-reference` implied by `-D warnings` + = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference + +error: this pattern takes a reference on something that is being de-referenced + --> examples/needless_borrowed_ref.rs:42:38 + | +42 | (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref' + | ^^^^^^ help: try removing the `&ref` part and just keep `k` + | + = note: `-D needless-borrowed-reference` implied by `-D warnings` + = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference + +error: aborting due to previous error(s) + +error: Could not compile `clippy_tests`. + +To learn more, run the command again with --verbose.