|
1 |
| -#![warn(clippy::wildcard_enum_match_arm)] |
2 |
| - |
3 |
| -#[derive(Debug)] |
4 |
| -enum Maybe<T> { |
5 |
| - Some(T), |
6 |
| - Probably(T), |
7 |
| - None, |
8 |
| -} |
9 |
| - |
10 |
| -fn is_it_wildcard<T>(m: Maybe<T>) -> &'static str { |
11 |
| - match m { |
12 |
| - Maybe::Some(_) => "Some", |
13 |
| - _ => "Could be", |
14 |
| - } |
15 |
| -} |
16 |
| - |
17 |
| -fn is_it_bound<T>(m: Maybe<T>) -> &'static str { |
18 |
| - match m { |
19 |
| - Maybe::None => "None", |
20 |
| - _other => "Could be", |
21 |
| - } |
22 |
| -} |
23 |
| - |
24 |
| -fn is_it_binding(m: Maybe<u32>) -> String { |
25 |
| - match m { |
26 |
| - Maybe::Some(v) => "Large".to_string(), |
27 |
| - n => format!("{:?}", n), |
28 |
| - } |
29 |
| -} |
30 |
| - |
31 |
| -fn is_it_binding_exhaustive(m: Maybe<u32>) -> String { |
32 |
| - match m { |
33 |
| - Maybe::Some(v) => "Large".to_string(), |
34 |
| - n @ Maybe::Probably(_) | n @ Maybe::None => format!("{:?}", n), |
35 |
| - } |
36 |
| -} |
37 |
| - |
38 |
| -fn is_it_with_guard(m: Maybe<u32>) -> &'static str { |
39 |
| - match m { |
40 |
| - Maybe::Some(v) if v > 100 => "Large", |
41 |
| - _ => "Who knows", |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -fn is_it_exhaustive<T>(m: Maybe<T>) -> &'static str { |
46 |
| - match m { |
47 |
| - Maybe::None => "None", |
48 |
| - Maybe::Some(_) | Maybe::Probably(..) => "Could be", |
49 |
| - } |
50 |
| -} |
51 |
| - |
52 |
| -fn is_one_or_three(i: i32) -> bool { |
53 |
| - match i { |
54 |
| - 1 | 3 => true, |
55 |
| - _ => false, |
| 1 | +#![deny(clippy::wildcard_enum_match_arm)] |
| 2 | + |
| 3 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 4 | +enum Color { |
| 5 | + Red, |
| 6 | + Green, |
| 7 | + Blue, |
| 8 | + Rgb(u8, u8, u8), |
| 9 | + Cyan, |
| 10 | +} |
| 11 | + |
| 12 | +impl Color { |
| 13 | + fn is_monochrome(self) -> bool { |
| 14 | + match self { |
| 15 | + Color::Red | Color::Green | Color::Blue => true, |
| 16 | + Color::Rgb(r, g, b) => r | g == 0 || r | b == 0 || g | b == 0, |
| 17 | + Color::Cyan => false, |
| 18 | + } |
56 | 19 | }
|
57 | 20 | }
|
58 | 21 |
|
59 | 22 | fn main() {
|
60 |
| - println!("{}", is_it_wildcard(Maybe::Some("foo"))); |
61 |
| - |
62 |
| - println!("{}", is_it_bound(Maybe::Some("foo"))); |
63 |
| - |
64 |
| - println!("{}", is_it_binding(Maybe::Some(1))); |
65 |
| - |
66 |
| - println!("{}", is_it_binding_exhaustive(Maybe::Some(1))); |
67 |
| - |
68 |
| - println!("{}", is_it_with_guard(Maybe::Some(1))); |
69 |
| - |
70 |
| - println!("{}", is_it_exhaustive(Maybe::Some("foo"))); |
71 |
| - |
72 |
| - println!("{}", is_one_or_three(2)); |
| 23 | + let color = Color::Rgb(0, 0, 127); |
| 24 | + match color { |
| 25 | + Color::Red => println!("Red"), |
| 26 | + _ => eprintln!("Not red"), |
| 27 | + }; |
| 28 | + match color { |
| 29 | + Color::Red => println!("Red"), |
| 30 | + _not_red => eprintln!("Not red"), |
| 31 | + }; |
| 32 | + let _str = match color { |
| 33 | + Color::Red => "Red".to_owned(), |
| 34 | + not_red => format!("{:?}", not_red), |
| 35 | + }; |
| 36 | + match color { |
| 37 | + Color::Red => {}, |
| 38 | + Color::Green => {}, |
| 39 | + Color::Blue => {}, |
| 40 | + Color::Cyan => {}, |
| 41 | + c if c.is_monochrome() => {}, |
| 42 | + Color::Rgb(_, _, _) => {}, |
| 43 | + }; |
| 44 | + let _str = match color { |
| 45 | + Color::Red => "Red", |
| 46 | + c @ Color::Green | c @ Color::Blue | c @ Color::Rgb(_, _, _) | c @ Color::Cyan => "Not red", |
| 47 | + }; |
| 48 | + match color { |
| 49 | + Color::Rgb(r, _, _) if r > 0 => "Some red", |
| 50 | + _ => "No red", |
| 51 | + }; |
| 52 | + match color { |
| 53 | + Color::Red | Color::Green | Color::Blue | Color::Cyan => {}, |
| 54 | + Color::Rgb(..) => {}, |
| 55 | + }; |
| 56 | + let x: u8 = unimplemented!(); |
| 57 | + match x { |
| 58 | + 0 => {}, |
| 59 | + 140 => {}, |
| 60 | + _ => {}, |
| 61 | + }; |
73 | 62 | }
|
0 commit comments