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