From 622b167eb86b197c7e7085558c29f4637f34ff23 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 25 Sep 2019 05:03:28 -0700 Subject: [PATCH] needless_borrow: allow other lints, make fixable --- tests/ui/needless_borrow.fixed | 62 +++++++++++++++++++++++++++++++++ tests/ui/needless_borrow.rs | 4 ++- tests/ui/needless_borrow.stderr | 24 +++---------- 3 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 tests/ui/needless_borrow.fixed diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed new file mode 100644 index 000000000..50f9b7c7b --- /dev/null +++ b/tests/ui/needless_borrow.fixed @@ -0,0 +1,62 @@ +// run-rustfix + +#![allow(clippy::needless_borrowed_reference)] + +#[allow(clippy::trivially_copy_pass_by_ref)] +fn x(y: &i32) -> i32 { + *y +} + +#[warn(clippy::all, clippy::needless_borrow)] +#[allow(unused_variables)] +fn main() { + let a = 5; + let b = x(&a); + let c = x(&a); + let s = &String::from("hi"); + let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not + let g_val = g(&Vec::new()); // should not error, because `&Vec` derefs to `&[T]` + let vec = Vec::new(); + let vec_val = g(&vec); // should not error, because `&Vec` derefs to `&[T]` + h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait` + if let Some(cake) = Some(&5) {} + let garbl = match 42 { + 44 => &a, + 45 => { + println!("foo"); + &&a // FIXME: this should lint, too + }, + 46 => &a, + _ => panic!(), + }; +} + +fn f(y: &T) -> T { + *y +} + +fn g(y: &[u8]) -> u8 { + y[0] +} + +trait Trait {} + +impl<'a> Trait for &'a str {} + +fn h(_: &dyn Trait) {} +#[warn(clippy::needless_borrow)] +#[allow(dead_code)] +fn issue_1432() { + let mut v = Vec::::new(); + let _ = v.iter_mut().filter(|&ref a| a.is_empty()); + let _ = v.iter().filter(|&a| a.is_empty()); + + let _ = v.iter().filter(|&a| a.is_empty()); +} + +#[allow(dead_code)] +#[warn(clippy::needless_borrow)] +#[derive(Debug)] +enum Foo<'a> { + Str(&'a str), +} diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs index d4ac2b898..8677b957e 100644 --- a/tests/ui/needless_borrow.rs +++ b/tests/ui/needless_borrow.rs @@ -1,4 +1,6 @@ -use std::borrow::Cow; +// run-rustfix + +#![allow(clippy::needless_borrowed_reference)] #[allow(clippy::trivially_copy_pass_by_ref)] fn x(y: &i32) -> i32 { diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr index 40744160f..49df9cd07 100644 --- a/tests/ui/needless_borrow.stderr +++ b/tests/ui/needless_borrow.stderr @@ -1,5 +1,5 @@ error: this expression borrows a reference that is immediately dereferenced by the compiler - --> $DIR/needless_borrow.rs:13:15 + --> $DIR/needless_borrow.rs:15:15 | LL | let c = x(&&a); | ^^^ help: change this to: `&a` @@ -7,36 +7,22 @@ LL | let c = x(&&a); = note: `-D clippy::needless-borrow` implied by `-D warnings` error: this pattern creates a reference to a reference - --> $DIR/needless_borrow.rs:20:17 + --> $DIR/needless_borrow.rs:22:17 | LL | if let Some(ref cake) = Some(&5) {} | ^^^^^^^^ help: change this to: `cake` error: this expression borrows a reference that is immediately dereferenced by the compiler - --> $DIR/needless_borrow.rs:27:15 + --> $DIR/needless_borrow.rs:29:15 | LL | 46 => &&a, | ^^^ help: change this to: `&a` -error: this pattern takes a reference on something that is being de-referenced - --> $DIR/needless_borrow.rs:49:34 - | -LL | let _ = v.iter_mut().filter(|&ref a| a.is_empty()); - | ^^^^^^ help: try removing the `&ref` part and just keep: `a` - | - = note: `-D clippy::needless-borrowed-reference` implied by `-D warnings` - -error: this pattern takes a reference on something that is being de-referenced - --> $DIR/needless_borrow.rs:50:30 - | -LL | let _ = v.iter().filter(|&ref a| a.is_empty()); - | ^^^^^^ help: try removing the `&ref` part and just keep: `a` - error: this pattern creates a reference to a reference - --> $DIR/needless_borrow.rs:50:31 + --> $DIR/needless_borrow.rs:52:31 | LL | let _ = v.iter().filter(|&ref a| a.is_empty()); | ^^^^^ help: change this to: `a` -error: aborting due to 6 previous errors +error: aborting due to 4 previous errors