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