]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_borrowed_ref.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / tests / ui / needless_borrowed_ref.rs
1 // run-rustfix
2
3 #![warn(clippy::needless_borrowed_reference)]
4 #![allow(
5     unused,
6     irrefutable_let_patterns,
7     non_shorthand_field_patterns,
8     clippy::needless_borrow
9 )]
10
11 fn main() {}
12
13 struct Struct {
14     a: usize,
15     b: usize,
16     c: usize,
17 }
18
19 struct TupleStruct(u8, u8, u8);
20
21 fn should_lint(
22     array: [u8; 4],
23     slice: &[u8],
24     slice_of_refs: &[&u8],
25     vec: Vec<u8>,
26     tuple: (u8, u8, u8),
27     tuple_struct: TupleStruct,
28     s: Struct,
29 ) {
30     let mut v = Vec::<String>::new();
31     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
32
33     let var = 3;
34     let thingy = Some(&var);
35     if let Some(&ref v) = thingy {}
36
37     if let &[&ref a, ref b] = slice_of_refs {}
38
39     let &[ref a, ..] = &array;
40     let &[ref a, ref b, ..] = &array;
41
42     if let &[ref a, ref b] = slice {}
43     if let &[ref a, ref b] = &vec[..] {}
44
45     if let &[ref a, ref b, ..] = slice {}
46     if let &[ref a, .., ref b] = slice {}
47     if let &[.., ref a, ref b] = slice {}
48
49     if let &[ref a, _] = slice {}
50
51     if let &(ref a, ref b, ref c) = &tuple {}
52     if let &(ref a, _, ref c) = &tuple {}
53     if let &(ref a, ..) = &tuple {}
54
55     if let &TupleStruct(ref a, ..) = &tuple_struct {}
56
57     if let &Struct {
58         ref a,
59         b: ref b,
60         c: ref renamed,
61     } = &s
62     {}
63
64     if let &Struct { ref a, b: _, .. } = &s {}
65 }
66
67 fn should_not_lint(
68     array: [u8; 4],
69     slice: &[u8],
70     slice_of_refs: &[&u8],
71     vec: Vec<u8>,
72     tuple: (u8, u8, u8),
73     tuple_struct: TupleStruct,
74     s: Struct,
75 ) {
76     if let [ref a] = slice {}
77     if let &[ref a, b] = slice {}
78     if let &[ref a, .., b] = slice {}
79
80     if let &(ref a, b, ..) = &tuple {}
81     if let &TupleStruct(ref a, b, ..) = &tuple_struct {}
82     if let &Struct { ref a, b, .. } = &s {}
83
84     // must not be removed as variables must be bound consistently across | patterns
85     if let (&[ref a], _) | ([], ref a) = (slice_of_refs, &1u8) {}
86
87     // the `&`s here technically could be removed, but it'd be noisy and without a `ref` doesn't match
88     // the lint name
89     if let &[] = slice {}
90     if let &[_] = slice {}
91     if let &[..] = slice {}
92     if let &(..) = &tuple {}
93     if let &TupleStruct(..) = &tuple_struct {}
94     if let &Struct { .. } = &s {}
95
96     let mut var2 = 5;
97     let thingy2 = Some(&mut var2);
98     if let Some(&mut ref mut v) = thingy2 {
99         //          ^ should **not** be linted
100         // v is borrowed as mutable.
101         *v = 10;
102     }
103     if let Some(&mut ref v) = thingy2 {
104         //          ^ should **not** be linted
105         // here, v is borrowed as immutable.
106         // can't do that:
107         //*v = 15;
108     }
109 }
110
111 enum Animal {
112     Cat(u64),
113     Dog(u64),
114 }
115
116 fn foo(a: &Animal, b: &Animal) {
117     match (a, b) {
118         // lifetime mismatch error if there is no '&ref' before `feature(nll)` stabilization in 1.63
119         (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (),
120         //                  ^    and   ^ should **not** be linted
121         (Animal::Dog(a), &Animal::Dog(_)) => (),
122     }
123 }