]> git.lizzy.rs Git - rust.git/blob - tests/ui/single_match.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / single_match.rs
1 #![warn(clippy::single_match)]
2
3 fn dummy() {}
4
5 fn single_match() {
6     let x = Some(1u8);
7
8     match x {
9         Some(y) => {
10             println!("{:?}", y);
11         },
12         _ => (),
13     };
14
15     let x = Some(1u8);
16     match x {
17         // Note the missing block braces.
18         // We suggest `if let Some(y) = x { .. }` because the macro
19         // is expanded before we can do anything.
20         Some(y) => println!("{:?}", y),
21         _ => (),
22     }
23
24     let z = (1u8, 1u8);
25     match z {
26         (2..=3, 7..=9) => dummy(),
27         _ => {},
28     };
29
30     // Not linted (pattern guards used)
31     match x {
32         Some(y) if y == 0 => println!("{:?}", y),
33         _ => (),
34     }
35
36     // Not linted (no block with statements in the single arm)
37     match z {
38         (2..=3, 7..=9) => println!("{:?}", z),
39         _ => println!("nope"),
40     }
41 }
42
43 enum Foo {
44     Bar,
45     Baz(u8),
46 }
47 use std::borrow::Cow;
48 use Foo::*;
49
50 fn single_match_know_enum() {
51     let x = Some(1u8);
52     let y: Result<_, i8> = Ok(1i8);
53
54     match x {
55         Some(y) => dummy(),
56         None => (),
57     };
58
59     match y {
60         Ok(y) => dummy(),
61         Err(..) => (),
62     };
63
64     let c = Cow::Borrowed("");
65
66     match c {
67         Cow::Borrowed(..) => dummy(),
68         Cow::Owned(..) => (),
69     };
70
71     let z = Foo::Bar;
72     // no warning
73     match z {
74         Bar => println!("42"),
75         Baz(_) => (),
76     }
77
78     match z {
79         Baz(_) => println!("42"),
80         Bar => (),
81     }
82 }
83
84 fn main() {}