]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/wildcard_enum_match_arm.fixed
Rollup merge of #102258 - cjgillot:core-kappa, r=m-ou-se
[rust.git] / src / tools / clippy / tests / ui / wildcard_enum_match_arm.fixed
1 // run-rustfix
2 // aux-build:non-exhaustive-enum.rs
3 #![deny(clippy::wildcard_enum_match_arm)]
4 #![allow(dead_code, unreachable_code, unused_variables)]
5 #![allow(
6     clippy::diverging_sub_expression,
7     clippy::single_match,
8     clippy::uninlined_format_args,
9     clippy::unnested_or_patterns,
10     clippy::wildcard_in_or_patterns
11 )]
12
13 extern crate non_exhaustive_enum;
14
15 use non_exhaustive_enum::ErrorKind;
16
17 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
18 enum Color {
19     Red,
20     Green,
21     Blue,
22     Rgb(u8, u8, u8),
23     Cyan,
24 }
25
26 impl Color {
27     fn is_monochrome(self) -> bool {
28         match self {
29             Color::Red | Color::Green | Color::Blue => true,
30             Color::Rgb(r, g, b) => r | g == 0 || r | b == 0 || g | b == 0,
31             Color::Cyan => false,
32         }
33     }
34 }
35
36 fn main() {
37     let color = Color::Rgb(0, 0, 127);
38     match color {
39         Color::Red => println!("Red"),
40         Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan => eprintln!("Not red"),
41     };
42     match color {
43         Color::Red => println!("Red"),
44         _not_red @ Color::Green | _not_red @ Color::Blue | _not_red @ Color::Rgb(..) | _not_red @ Color::Cyan => eprintln!("Not red"),
45     };
46     let _str = match color {
47         Color::Red => "Red".to_owned(),
48         not_red @ Color::Green | not_red @ Color::Blue | not_red @ Color::Rgb(..) | not_red @ Color::Cyan => format!("{:?}", not_red),
49     };
50     match color {
51         Color::Red => {},
52         Color::Green => {},
53         Color::Blue => {},
54         Color::Cyan => {},
55         c if c.is_monochrome() => {},
56         Color::Rgb(_, _, _) => {},
57     };
58     let _str = match color {
59         Color::Red => "Red",
60         c @ Color::Green | c @ Color::Blue | c @ Color::Rgb(_, _, _) | c @ Color::Cyan => "Not red",
61     };
62     match color {
63         Color::Rgb(r, _, _) if r > 0 => "Some red",
64         Color::Red | Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan => "No red",
65     };
66     match color {
67         Color::Red | Color::Green | Color::Blue | Color::Cyan => {},
68         Color::Rgb(..) => {},
69     };
70     let x: u8 = unimplemented!();
71     match x {
72         0 => {},
73         140 => {},
74         _ => {},
75     };
76     // We need to use an enum not defined in this test because non_exhaustive is ignored for the
77     // purposes of dead code analysis within a crate.
78     let error_kind = ErrorKind::NotFound;
79     match error_kind {
80         ErrorKind::NotFound => {},
81         ErrorKind::PermissionDenied | _ => {},
82     }
83     match error_kind {
84         ErrorKind::NotFound => {},
85         ErrorKind::PermissionDenied => {},
86         _ => {},
87     }
88
89     {
90         #![allow(clippy::manual_non_exhaustive)]
91         pub enum Enum {
92             A,
93             B,
94             #[doc(hidden)]
95             __Private,
96         }
97         match Enum::A {
98             Enum::A => (),
99             Enum::B | _ => (),
100         }
101     }
102 }