about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/single_match.rs
blob: a367b94c4ca6b96f21e746f138cddfac2c5acae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//@require-annotations-for-level: WARN
#![warn(clippy::single_match)]
#![allow(
    unused,
    clippy::uninlined_format_args,
    clippy::needless_if,
    clippy::redundant_guards,
    clippy::redundant_pattern_matching,
    clippy::manual_unwrap_or_default
)]
fn dummy() {}

fn single_match() {
    let x = Some(1u8);

    match x {
        Some(y) => {
            println!("{:?}", y);
        },
        _ => (),
    };
    //~^^^^^^ single_match

    let x = Some(1u8);
    match x {
        // Note the missing block braces.
        // We suggest `if let Some(y) = x { .. }` because the macro
        // is expanded before we can do anything.
        Some(y) => println!("{:?}", y),
        _ => (),
    }
    //~^^^^^^^ single_match
    //~| NOTE: you might want to preserve the comments from inside the `match`

    let z = (1u8, 1u8);
    match z {
        (2..=3, 7..=9) => dummy(),
        _ => {},
    };
    //~^^^^ single_match

    // Not linted (pattern guards used)
    match x {
        Some(y) if y == 0 => println!("{:?}", y),
        _ => (),
    }

    // Not linted (no block with statements in the single arm)
    match z {
        (2..=3, 7..=9) => println!("{:?}", z),
        _ => println!("nope"),
    }
}

enum Foo {
    Bar,
    Baz(u8),
}
use Foo::*;
use std::borrow::Cow;

fn single_match_know_enum() {
    let x = Some(1u8);
    let y: Result<_, i8> = Ok(1i8);

    match x {
        Some(y) => dummy(),
        None => (),
    };
    //~^^^^ single_match

    match y {
        Ok(y) => dummy(),
        Err(..) => (),
    };
    //~^^^^ single_match

    let c = Cow::Borrowed("");

    match c {
        Cow::Borrowed(..) => dummy(),
        Cow::Owned(..) => (),
    };
    //~^^^^ single_match

    let z = Foo::Bar;
    // no warning
    match z {
        Bar => println!("42"),
        Baz(_) => (),
    }

    match z {
        Baz(_) => println!("42"),
        Bar => (),
    }
}

// issue #173
fn if_suggestion() {
    let x = "test";
    match x {
        "test" => println!(),
        _ => (),
    }
    //~^^^^ single_match

    #[derive(PartialEq, Eq)]
    enum Foo {
        A,
        B,
        C(u32),
    }

    let x = Foo::A;
    match x {
        Foo::A => println!(),
        _ => (),
    }
    //~^^^^ single_match

    const FOO_C: Foo = Foo::C(0);
    match x {
        FOO_C => println!(),
        _ => (),
    }
    //~^^^^ single_match

    match &&x {
        Foo::A => println!(),
        _ => (),
    }
    //~^^^^ single_match

    let x = &x;
    match &x {
        Foo::A => println!(),
        _ => (),
    }
    //~^^^^ single_match

    enum Bar {
        A,
        B,
    }
    impl PartialEq for Bar {
        fn eq(&self, rhs: &Self) -> bool {
            matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B))
        }
    }
    impl Eq for Bar {}

    let x = Bar::A;
    match x {
        Bar::A => println!(),
        _ => (),
    }
    //~^^^^ single_match

    // issue #7038
    struct X;
    let x = Some(X);
    match x {
        None => println!(),
        _ => (),
    };
    //~^^^^ single_match
}

// See: issue #8282
fn ranges() {
    enum E {
        V,
    }
    let x = (Some(E::V), Some(42));

    // Don't lint, because the `E` enum can be extended with additional fields later. Thus, the
    // proposed replacement to `if let Some(E::V)` may hide non-exhaustive warnings that appeared
    // because of `match` construction.
    match x {
        (Some(E::V), _) => {},
        (None, _) => {},
    }

    // lint
    match x {
        (Some(_), _) => {},
        (None, _) => {},
    }
    //~^^^^ single_match

    // lint
    match x {
        (Some(E::V), _) => todo!(),
        (_, _) => {},
    }
    //~^^^^ single_match

    // lint
    match (Some(42), Some(E::V), Some(42)) {
        (.., Some(E::V), _) => {},
        (..) => {},
    }
    //~^^^^ single_match

    // Don't lint, see above.
    match (Some(E::V), Some(E::V), Some(E::V)) {
        (.., Some(E::V), _) => {},
        (.., None, _) => {},
    }

    // Don't lint, see above.
    match (Some(E::V), Some(E::V), Some(E::V)) {
        (Some(E::V), ..) => {},
        (None, ..) => {},
    }

    // Don't lint, see above.
    match (Some(E::V), Some(E::V), Some(E::V)) {
        (_, Some(E::V), ..) => {},
        (_, None, ..) => {},
    }
}

fn skip_type_aliases() {
    enum OptionEx {
        Some(i32),
        None,
    }
    enum ResultEx {
        Err(i32),
        Ok(i32),
    }

    use OptionEx::{None, Some};
    use ResultEx::{Err, Ok};

    // don't lint
    match Err(42) {
        Ok(_) => dummy(),
        Err(_) => (),
    };

    // don't lint
    match Some(1i32) {
        Some(_) => dummy(),
        None => (),
    };
}

macro_rules! single_match {
    ($num:literal) => {
        match $num {
            15 => println!("15"),
            _ => (),
        }
    };
}

fn main() {
    single_match!(5);

    // Don't lint
    let _ = match Some(0) {
        #[cfg(feature = "foo")]
        Some(10) => 11,
        Some(x) => x,
        _ => 0,
    };
}

fn issue_10808(bar: Option<i32>) {
    match bar {
        Some(v) => unsafe {
            let r = &v as *const i32;
            println!("{}", *r);
        },
        _ => {},
    }
    //~^^^^^^^ single_match

    match bar {
        #[rustfmt::skip]
        Some(v) => {
            unsafe {
                let r = &v as *const i32;
                println!("{}", *r);
            }
        },
        _ => {},
    }
    //~^^^^^^^^^^ single_match
}

mod issue8634 {
    struct SomeError(i32, i32);

    fn foo(x: Result<i32, ()>) {
        match x {
            Ok(y) => {
                println!("Yay! {y}");
            },
            Err(()) => {
                // Ignore this error because blah blah blah.
            },
        }
    }

    fn bar(x: Result<i32, SomeError>) {
        match x {
            Ok(y) => {
                println!("Yay! {y}");
            },
            Err(_) => {
                // TODO: Process the error properly.
            },
        }
    }

    fn block_comment(x: Result<i32, SomeError>) {
        match x {
            Ok(y) => {
                println!("Yay! {y}");
            },
            Err(_) => {
                /*
                let's make sure that this also
                does not lint block comments.
                */
            },
        }
    }
}

fn issue11365() {
    enum Foo {
        A,
        B,
        C,
    }
    use Foo::{A, B, C};

    match Some(A) {
        Some(A | B | C) => println!(),
        None => {},
    }

    match Some(A) {
        Some(A | B) => println!(),
        Some { 0: C } | None => {},
    }

    match [A, A] {
        [A, _] => println!(),
        [_, A | B | C] => {},
    }

    match Ok::<_, u32>(Some(A)) {
        Ok(Some(A)) => println!(),
        Err(_) | Ok(None | Some(B | C)) => {},
    }

    match Ok::<_, u32>(Some(A)) {
        Ok(Some(A)) => println!(),
        Err(_) | Ok(None | Some(_)) => {},
    }
    //~^^^^ single_match

    match &Some(A) {
        Some(A | B | C) => println!(),
        None => {},
    }

    match &Some(A) {
        &Some(A | B | C) => println!(),
        None => {},
    }

    match &Some(A) {
        Some(A | B) => println!(),
        None | Some(_) => {},
    }
    //~^^^^ single_match
}

fn issue12758(s: &[u8]) {
    match &s[0..3] {
        b"foo" => println!(),
        _ => {},
    }
    //~^^^^ single_match
}

#[derive(Eq, PartialEq)]
pub struct Data([u8; 4]);

const DATA: Data = Data([1, 2, 3, 4]);
const CONST_I32: i32 = 1;

fn irrefutable_match() {
    match DATA {
        DATA => println!(),
        _ => {},
    }
    //~^^^^ single_match

    match CONST_I32 {
        CONST_I32 => println!(),
        _ => {},
    }
    //~^^^^ single_match

    let i = 0;
    match i {
        i => {
            let a = 1;
            let b = 2;
        },
        _ => {},
    }
    //~^^^^^^^ single_match

    match i {
        i => {},
        _ => {},
    }
    //~^^^^ single_match

    match i {
        i => (),
        _ => (),
    }
    //~^^^^ single_match

    match CONST_I32 {
        CONST_I32 => println!(),
        _ => {},
    }
    //~^^^^ single_match

    let mut x = vec![1i8];

    match x.pop() {
        // bla
        Some(u) => println!("{u}"),
        // more comments!
        None => {},
    }
    //~^^^^^^ single_match
    //~| NOTE: you might want to preserve the comments from inside the `match`

    match x.pop() {
        // bla
        Some(u) => {
            // bla
            println!("{u}");
        },
        // bla
        None => {},
    }
    //~^^^^^^^^^ single_match
    //~| NOTE: you might want to preserve the comments from inside the `match`
}

fn issue_14493() {
    macro_rules! mac {
        (some) => {
            Some(42)
        };
        (any) => {
            _
        };
        (str) => {
            "foo"
        };
    }

    match mac!(some) {
        Some(u) => println!("{u}"),
        _ => (),
    }
    //~^^^^ single_match

    // When scrutinee comes from macro, do not tell that arm will always match
    // and suggest an equality check instead.
    match mac!(str) {
        "foo" => println!("eq"),
        _ => (),
    }
    //~^^^^ ERROR: for an equality check

    // Do not lint if any match arm come from expansion
    match Some(0) {
        mac!(some) => println!("eq"),
        mac!(any) => println!("neq"),
    }
    match Some(0) {
        Some(42) => println!("eq"),
        mac!(any) => println!("neq"),
    }
    match Some(0) {
        mac!(some) => println!("eq"),
        _ => println!("neq"),
    }
}