]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_borrowed_ref.rs
Auto merge of #104673 - matthiaskrgr:rollup-85f65ov, r=matthiaskrgr
[rust.git] / src / tools / clippy / tests / ui / needless_borrowed_ref.rs
1 // run-rustfix
2
3 #![warn(clippy::needless_borrowed_reference)]
4 #![allow(unused, clippy::needless_borrow)]
5
6 fn main() {}
7
8 fn should_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec<u8>) {
9     let mut v = Vec::<String>::new();
10     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
11
12     let var = 3;
13     let thingy = Some(&var);
14     if let Some(&ref v) = thingy {}
15
16     if let &[&ref a, ref b] = slice_of_refs {}
17
18     let &[ref a, ..] = &array;
19     let &[ref a, ref b, ..] = &array;
20
21     if let &[ref a, ref b] = slice {}
22     if let &[ref a, ref b] = &vec[..] {}
23
24     if let &[ref a, ref b, ..] = slice {}
25     if let &[ref a, .., ref b] = slice {}
26     if let &[.., ref a, ref b] = slice {}
27 }
28
29 fn should_not_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec<u8>) {
30     if let [ref a] = slice {}
31     if let &[ref a, b] = slice {}
32     if let &[ref a, .., b] = slice {}
33
34     // must not be removed as variables must be bound consistently across | patterns
35     if let (&[ref a], _) | ([], ref a) = (slice_of_refs, &1u8) {}
36
37     let mut var2 = 5;
38     let thingy2 = Some(&mut var2);
39     if let Some(&mut ref mut v) = thingy2 {
40         //          ^ should **not** be linted
41         // v is borrowed as mutable.
42         *v = 10;
43     }
44     if let Some(&mut ref v) = thingy2 {
45         //          ^ should **not** be linted
46         // here, v is borrowed as immutable.
47         // can't do that:
48         //*v = 15;
49     }
50 }
51
52 enum Animal {
53     Cat(u64),
54     Dog(u64),
55 }
56
57 fn foo(a: &Animal, b: &Animal) {
58     match (a, b) {
59         // lifetime mismatch error if there is no '&ref' before `feature(nll)` stabilization in 1.63
60         (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (),
61         //                  ^    and   ^ should **not** be linted
62         (&Animal::Dog(ref a), &Animal::Dog(_)) => (), //              ^ should **not** be linted
63     }
64 }