]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/single_match.rs
New upstream version 1.73.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / single_match.rs
1 //@run-rustfix
2 #![warn(clippy::single_match)]
3 #![allow(
4 unused,
5 clippy::uninlined_format_args,
6 clippy::needless_if,
7 clippy::redundant_guards,
8 clippy::redundant_pattern_matching
9 )]
10 fn dummy() {}
11
12 fn single_match() {
13 let x = Some(1u8);
14
15 match x {
16 Some(y) => {
17 println!("{:?}", y);
18 },
19 _ => (),
20 };
21
22 let x = Some(1u8);
23 match x {
24 // Note the missing block braces.
25 // We suggest `if let Some(y) = x { .. }` because the macro
26 // is expanded before we can do anything.
27 Some(y) => println!("{:?}", y),
28 _ => (),
29 }
30
31 let z = (1u8, 1u8);
32 match z {
33 (2..=3, 7..=9) => dummy(),
34 _ => {},
35 };
36
37 // Not linted (pattern guards used)
38 match x {
39 Some(y) if y == 0 => println!("{:?}", y),
40 _ => (),
41 }
42
43 // Not linted (no block with statements in the single arm)
44 match z {
45 (2..=3, 7..=9) => println!("{:?}", z),
46 _ => println!("nope"),
47 }
48 }
49
50 enum Foo {
51 Bar,
52 Baz(u8),
53 }
54 use std::borrow::Cow;
55 use Foo::*;
56
57 fn single_match_know_enum() {
58 let x = Some(1u8);
59 let y: Result<_, i8> = Ok(1i8);
60
61 match x {
62 Some(y) => dummy(),
63 None => (),
64 };
65
66 match y {
67 Ok(y) => dummy(),
68 Err(..) => (),
69 };
70
71 let c = Cow::Borrowed("");
72
73 match c {
74 Cow::Borrowed(..) => dummy(),
75 Cow::Owned(..) => (),
76 };
77
78 let z = Foo::Bar;
79 // no warning
80 match z {
81 Bar => println!("42"),
82 Baz(_) => (),
83 }
84
85 match z {
86 Baz(_) => println!("42"),
87 Bar => (),
88 }
89 }
90
91 // issue #173
92 fn if_suggestion() {
93 let x = "test";
94 match x {
95 "test" => println!(),
96 _ => (),
97 }
98
99 #[derive(PartialEq, Eq)]
100 enum Foo {
101 A,
102 B,
103 C(u32),
104 }
105
106 let x = Foo::A;
107 match x {
108 Foo::A => println!(),
109 _ => (),
110 }
111
112 const FOO_C: Foo = Foo::C(0);
113 match x {
114 FOO_C => println!(),
115 _ => (),
116 }
117
118 match &&x {
119 Foo::A => println!(),
120 _ => (),
121 }
122
123 let x = &x;
124 match &x {
125 Foo::A => println!(),
126 _ => (),
127 }
128
129 enum Bar {
130 A,
131 B,
132 }
133 impl PartialEq for Bar {
134 fn eq(&self, rhs: &Self) -> bool {
135 matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B))
136 }
137 }
138 impl Eq for Bar {}
139
140 let x = Bar::A;
141 match x {
142 Bar::A => println!(),
143 _ => (),
144 }
145
146 // issue #7038
147 struct X;
148 let x = Some(X);
149 match x {
150 None => println!(),
151 _ => (),
152 };
153 }
154
155 // See: issue #8282
156 fn ranges() {
157 enum E {
158 V,
159 }
160 let x = (Some(E::V), Some(42));
161
162 // Don't lint, because the `E` enum can be extended with additional fields later. Thus, the
163 // proposed replacement to `if let Some(E::V)` may hide non-exhaustive warnings that appeared
164 // because of `match` construction.
165 match x {
166 (Some(E::V), _) => {},
167 (None, _) => {},
168 }
169
170 // lint
171 match x {
172 (Some(_), _) => {},
173 (None, _) => {},
174 }
175
176 // lint
177 match x {
178 (Some(E::V), _) => todo!(),
179 (_, _) => {},
180 }
181
182 // lint
183 match (Some(42), Some(E::V), Some(42)) {
184 (.., Some(E::V), _) => {},
185 (..) => {},
186 }
187
188 // Don't lint, see above.
189 match (Some(E::V), Some(E::V), Some(E::V)) {
190 (.., Some(E::V), _) => {},
191 (.., None, _) => {},
192 }
193
194 // Don't lint, see above.
195 match (Some(E::V), Some(E::V), Some(E::V)) {
196 (Some(E::V), ..) => {},
197 (None, ..) => {},
198 }
199
200 // Don't lint, see above.
201 match (Some(E::V), Some(E::V), Some(E::V)) {
202 (_, Some(E::V), ..) => {},
203 (_, None, ..) => {},
204 }
205 }
206
207 fn skip_type_aliases() {
208 enum OptionEx {
209 Some(i32),
210 None,
211 }
212 enum ResultEx {
213 Err(i32),
214 Ok(i32),
215 }
216
217 use OptionEx::{None, Some};
218 use ResultEx::{Err, Ok};
219
220 // don't lint
221 match Err(42) {
222 Ok(_) => dummy(),
223 Err(_) => (),
224 };
225
226 // don't lint
227 match Some(1i32) {
228 Some(_) => dummy(),
229 None => (),
230 };
231 }
232
233 macro_rules! single_match {
234 ($num:literal) => {
235 match $num {
236 15 => println!("15"),
237 _ => (),
238 }
239 };
240 }
241
242 fn main() {
243 single_match!(5);
244
245 // Don't lint
246 let _ = match Some(0) {
247 #[cfg(feature = "foo")]
248 Some(10) => 11,
249 Some(x) => x,
250 _ => 0,
251 };
252 }
253
254 fn issue_10808(bar: Option<i32>) {
255 match bar {
256 Some(v) => unsafe {
257 let r = &v as *const i32;
258 println!("{}", *r);
259 },
260 _ => {},
261 }
262
263 match bar {
264 #[rustfmt::skip]
265 Some(v) => {
266 unsafe {
267 let r = &v as *const i32;
268 println!("{}", *r);
269 }
270 },
271 _ => {},
272 }
273 }
274
275 mod issue8634 {
276 struct SomeError(i32, i32);
277
278 fn foo(x: Result<i32, ()>) {
279 match x {
280 Ok(y) => {
281 println!("Yay! {y}");
282 },
283 Err(()) => {
284 // Ignore this error because blah blah blah.
285 },
286 }
287 }
288
289 fn bar(x: Result<i32, SomeError>) {
290 match x {
291 Ok(y) => {
292 println!("Yay! {y}");
293 },
294 Err(_) => {
295 // TODO: Process the error properly.
296 },
297 }
298 }
299
300 fn block_comment(x: Result<i32, SomeError>) {
301 match x {
302 Ok(y) => {
303 println!("Yay! {y}");
304 },
305 Err(_) => {
306 /*
307 let's make sure that this also
308 does not lint block comments.
309 */
310 },
311 }
312 }
313 }