rust/tests/ui/needless_borrowed_ref.rs

49 lines
1.2 KiB
Rust
Raw Normal View History

2018-07-28 17:34:52 +02:00
#![feature(tool_lints)]
2017-09-18 12:47:33 +02:00
2018-07-28 17:34:52 +02:00
#[warn(clippy::needless_borrowed_reference)]
#[allow(unused_variables)]
2017-06-29 13:46:07 +02:00
fn main() {
let mut v = Vec::<String>::new();
let _ = v.iter_mut().filter(|&ref a| a.is_empty());
// ^ should be linted
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
// 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 = 15;
}
}
#[allow(dead_code)]
enum Animal {
Cat(u64),
Dog(u64),
}
#[allow(unused_variables)]
#[allow(dead_code)]
fn foo(a: &Animal, b: &Animal) {
match (a, b) {
(&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref'
// ^ and ^ should *not* be linted
(&Animal::Dog(ref a), &Animal::Dog(_)) => ()
// ^ should *not* be linted
}
2017-06-29 13:46:07 +02:00
}