mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 22:50:56 +00:00
needless_borrow: allow other lints, make fixable
This commit is contained in:
parent
0d8e4d7c37
commit
622b167eb8
3 changed files with 70 additions and 20 deletions
62
tests/ui/needless_borrow.fixed
Normal file
62
tests/ui/needless_borrow.fixed
Normal file
|
@ -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<T>` derefs to `&[T]`
|
||||
let vec = Vec::new();
|
||||
let vec_val = g(&vec); // should not error, because `&Vec<T>` 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<T: Copy>(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::<String>::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),
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue