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