2022-10-06 07:44:38 +00:00
|
|
|
#![warn(clippy::needless_borrowed_reference)]
|
2022-11-21 19:34:47 +00:00
|
|
|
#![allow(
|
|
|
|
unused,
|
|
|
|
irrefutable_let_patterns,
|
|
|
|
non_shorthand_field_patterns,
|
2023-07-02 12:35:19 +00:00
|
|
|
clippy::needless_borrow,
|
|
|
|
clippy::needless_if
|
2022-11-21 19:34:47 +00:00
|
|
|
)]
|
2022-10-06 07:44:38 +00:00
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
|
2022-11-21 19:34:47 +00:00
|
|
|
struct Struct {
|
|
|
|
a: usize,
|
|
|
|
b: usize,
|
|
|
|
c: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TupleStruct(u8, u8, u8);
|
|
|
|
|
|
|
|
fn should_lint(
|
|
|
|
array: [u8; 4],
|
|
|
|
slice: &[u8],
|
|
|
|
slice_of_refs: &[&u8],
|
|
|
|
vec: Vec<u8>,
|
|
|
|
tuple: (u8, u8, u8),
|
|
|
|
tuple_struct: TupleStruct,
|
|
|
|
s: Struct,
|
|
|
|
) {
|
2017-06-29 11:46:07 +00:00
|
|
|
let mut v = Vec::<String>::new();
|
|
|
|
let _ = v.iter_mut().filter(|&ref a| a.is_empty());
|
2017-06-30 14:07:29 +00:00
|
|
|
|
2017-07-01 10:02:00 +00:00
|
|
|
let var = 3;
|
|
|
|
let thingy = Some(&var);
|
2022-10-06 07:44:38 +00:00
|
|
|
if let Some(&ref v) = thingy {}
|
|
|
|
|
|
|
|
if let &[&ref a, ref b] = slice_of_refs {}
|
|
|
|
|
|
|
|
let &[ref a, ..] = &array;
|
|
|
|
let &[ref a, ref b, ..] = &array;
|
|
|
|
|
|
|
|
if let &[ref a, ref b] = slice {}
|
|
|
|
if let &[ref a, ref b] = &vec[..] {}
|
|
|
|
|
|
|
|
if let &[ref a, ref b, ..] = slice {}
|
|
|
|
if let &[ref a, .., ref b] = slice {}
|
|
|
|
if let &[.., ref a, ref b] = slice {}
|
2022-11-21 19:34:47 +00:00
|
|
|
|
|
|
|
if let &[ref a, _] = slice {}
|
|
|
|
|
|
|
|
if let &(ref a, ref b, ref c) = &tuple {}
|
|
|
|
if let &(ref a, _, ref c) = &tuple {}
|
|
|
|
if let &(ref a, ..) = &tuple {}
|
|
|
|
|
|
|
|
if let &TupleStruct(ref a, ..) = &tuple_struct {}
|
|
|
|
|
|
|
|
if let &Struct {
|
|
|
|
ref a,
|
|
|
|
b: ref b,
|
|
|
|
c: ref renamed,
|
|
|
|
} = &s
|
|
|
|
{}
|
|
|
|
|
|
|
|
if let &Struct { ref a, b: _, .. } = &s {}
|
2022-10-06 07:44:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 19:34:47 +00:00
|
|
|
fn should_not_lint(
|
|
|
|
array: [u8; 4],
|
|
|
|
slice: &[u8],
|
|
|
|
slice_of_refs: &[&u8],
|
|
|
|
vec: Vec<u8>,
|
|
|
|
tuple: (u8, u8, u8),
|
|
|
|
tuple_struct: TupleStruct,
|
|
|
|
s: Struct,
|
|
|
|
) {
|
2022-10-06 07:44:38 +00:00
|
|
|
if let [ref a] = slice {}
|
|
|
|
if let &[ref a, b] = slice {}
|
|
|
|
if let &[ref a, .., b] = slice {}
|
|
|
|
|
2022-11-21 19:34:47 +00:00
|
|
|
if let &(ref a, b, ..) = &tuple {}
|
|
|
|
if let &TupleStruct(ref a, b, ..) = &tuple_struct {}
|
|
|
|
if let &Struct { ref a, b, .. } = &s {}
|
|
|
|
|
2022-10-06 07:44:38 +00:00
|
|
|
// must not be removed as variables must be bound consistently across | patterns
|
|
|
|
if let (&[ref a], _) | ([], ref a) = (slice_of_refs, &1u8) {}
|
2017-07-01 10:02:00 +00:00
|
|
|
|
2022-11-21 19:34:47 +00:00
|
|
|
// the `&`s here technically could be removed, but it'd be noisy and without a `ref` doesn't match
|
|
|
|
// the lint name
|
|
|
|
if let &[] = slice {}
|
|
|
|
if let &[_] = slice {}
|
|
|
|
if let &[..] = slice {}
|
|
|
|
if let &(..) = &tuple {}
|
|
|
|
if let &TupleStruct(..) = &tuple_struct {}
|
|
|
|
if let &Struct { .. } = &s {}
|
|
|
|
|
2017-07-01 10:02:00 +00:00
|
|
|
let mut var2 = 5;
|
|
|
|
let thingy2 = Some(&mut var2);
|
|
|
|
if let Some(&mut ref mut v) = thingy2 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// ^ should **not** be linted
|
2017-07-01 10:02:00 +00:00
|
|
|
// v is borrowed as mutable.
|
|
|
|
*v = 10;
|
|
|
|
}
|
|
|
|
if let Some(&mut ref v) = thingy2 {
|
2019-01-31 01:15:29 +00:00
|
|
|
// ^ should **not** be linted
|
2017-07-01 10:02:00 +00:00
|
|
|
// here, v is borrowed as immutable.
|
2017-06-30 14:07:29 +00:00
|
|
|
// can't do that:
|
2017-07-01 10:02:00 +00:00
|
|
|
//*v = 15;
|
2017-06-30 14:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Animal {
|
|
|
|
Cat(u64),
|
|
|
|
Dog(u64),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(a: &Animal, b: &Animal) {
|
|
|
|
match (a, b) {
|
2022-10-06 07:44:38 +00:00
|
|
|
// lifetime mismatch error if there is no '&ref' before `feature(nll)` stabilization in 1.63
|
|
|
|
(&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (),
|
2019-01-31 01:15:29 +00:00
|
|
|
// ^ and ^ should **not** be linted
|
2022-11-21 19:34:47 +00:00
|
|
|
(Animal::Dog(a), &Animal::Dog(_)) => (),
|
2017-06-30 14:07:29 +00:00
|
|
|
}
|
2017-06-29 11:46:07 +00:00
|
|
|
}
|