]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/match_expr_like_matches_macro.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / match_expr_like_matches_macro.rs
1 //@run-rustfix
2
3 #![warn(clippy::match_like_matches_macro)]
4 #![allow(
5 unreachable_patterns,
6 dead_code,
7 clippy::equatable_if_let,
8 clippy::needless_borrowed_reference
9 )]
10
11 fn main() {
12 let x = Some(5);
13
14 // Lint
15 let _y = match x {
16 Some(0) => true,
17 _ => false,
18 };
19
20 // Lint
21 let _w = match x {
22 Some(_) => true,
23 _ => false,
24 };
25
26 // Turn into is_none
27 let _z = match x {
28 Some(_) => false,
29 None => true,
30 };
31
32 // Lint
33 let _zz = match x {
34 Some(r) if r == 0 => false,
35 _ => true,
36 };
37
38 // Lint
39 let _zzz = if let Some(5) = x { true } else { false };
40
41 // No lint
42 let _a = match x {
43 Some(_) => false,
44 _ => false,
45 };
46
47 // No lint
48 let _ab = match x {
49 Some(0) => false,
50 _ => true,
51 None => false,
52 };
53
54 enum E {
55 A(u32),
56 B(i32),
57 C,
58 D,
59 }
60 let x = E::A(2);
61 {
62 // lint
63 let _ans = match x {
64 E::A(_) => true,
65 E::B(_) => true,
66 _ => false,
67 };
68 }
69 {
70 // lint
71 // skip rustfmt to prevent removing block for first pattern
72 #[rustfmt::skip]
73 let _ans = match x {
74 E::A(_) => {
75 true
76 }
77 E::B(_) => true,
78 _ => false,
79 };
80 }
81 {
82 // lint
83 let _ans = match x {
84 E::B(_) => false,
85 E::C => false,
86 _ => true,
87 };
88 }
89 {
90 // no lint
91 let _ans = match x {
92 E::A(_) => false,
93 E::B(_) => false,
94 E::C => true,
95 _ => true,
96 };
97 }
98 {
99 // no lint
100 let _ans = match x {
101 E::A(_) => true,
102 E::B(_) => false,
103 E::C => false,
104 _ => true,
105 };
106 }
107 {
108 // no lint
109 let _ans = match x {
110 E::A(a) if a < 10 => false,
111 E::B(a) if a < 10 => false,
112 _ => true,
113 };
114 }
115 {
116 // no lint
117 let _ans = match x {
118 E::A(_) => false,
119 E::B(a) if a < 10 => false,
120 _ => true,
121 };
122 }
123 {
124 // no lint
125 let _ans = match x {
126 E::A(a) => a == 10,
127 E::B(_) => false,
128 _ => true,
129 };
130 }
131 {
132 // no lint
133 let _ans = match x {
134 E::A(_) => false,
135 E::B(_) => true,
136 _ => false,
137 };
138 }
139
140 {
141 // should print "z" in suggestion (#6503)
142 let z = &Some(3);
143 let _z = match &z {
144 Some(3) => true,
145 _ => false,
146 };
147 }
148
149 {
150 // this could also print "z" in suggestion..?
151 let z = Some(3);
152 let _z = match &z {
153 Some(3) => true,
154 _ => false,
155 };
156 }
157
158 {
159 enum AnEnum {
160 X,
161 Y,
162 }
163
164 fn foo(_x: AnEnum) {}
165
166 fn main() {
167 let z = AnEnum::X;
168 // we can't remove the reference here!
169 let _ = match &z {
170 AnEnum::X => true,
171 _ => false,
172 };
173 foo(z);
174 }
175 }
176
177 {
178 struct S(i32);
179
180 fn fun(_val: Option<S>) {}
181 let val = Some(S(42));
182 // we need the reference here because later val is consumed by fun()
183 let _res = match &val {
184 &Some(ref _a) => true,
185 _ => false,
186 };
187 fun(val);
188 }
189
190 {
191 struct S(i32);
192
193 fn fun(_val: Option<S>) {}
194 let val = Some(S(42));
195 let _res = match &val {
196 &Some(ref _a) => true,
197 _ => false,
198 };
199 fun(val);
200 }
201
202 {
203 enum E {
204 A,
205 B,
206 C,
207 }
208
209 let _ = match E::A {
210 E::B => true,
211 #[cfg(feature = "foo")]
212 E::A => true,
213 _ => false,
214 };
215 }
216
217 let x = ' ';
218 // ignore if match block contains comment
219 let _line_comments = match x {
220 // numbers are bad!
221 '1' | '2' | '3' => true,
222 // spaces are very important to be true.
223 ' ' => true,
224 // as are dots
225 '.' => true,
226 _ => false,
227 };
228
229 let _block_comments = match x {
230 /* numbers are bad!
231 */
232 '1' | '2' | '3' => true,
233 /* spaces are very important to be true.
234 */
235 ' ' => true,
236 /* as are dots
237 */
238 '.' => true,
239 _ => false,
240 };
241 }
242
243 #[clippy::msrv = "1.41"]
244 fn msrv_1_41() {
245 let _y = match Some(5) {
246 Some(0) => true,
247 _ => false,
248 };
249 }
250
251 #[clippy::msrv = "1.42"]
252 fn msrv_1_42() {
253 let _y = match Some(5) {
254 Some(0) => true,
255 _ => false,
256 };
257 }