From afd7b180e5b1f3e152a057ad942454bc4ef31219 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sat, 21 Sep 2019 07:26:18 +0200 Subject: [PATCH] Add run-rustfix for wildcard_enum_match_arm lint --- tests/ui/wildcard_enum_match_arm.fixed | 65 +++++++++++++++++++++++++ tests/ui/wildcard_enum_match_arm.rs | 3 ++ tests/ui/wildcard_enum_match_arm.stderr | 10 ++-- 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 tests/ui/wildcard_enum_match_arm.fixed diff --git a/tests/ui/wildcard_enum_match_arm.fixed b/tests/ui/wildcard_enum_match_arm.fixed new file mode 100644 index 000000000..01c861282 --- /dev/null +++ b/tests/ui/wildcard_enum_match_arm.fixed @@ -0,0 +1,65 @@ +// run-rustfix + +#![deny(clippy::wildcard_enum_match_arm)] +#![allow(unreachable_code, unused_variables)] + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +enum Color { + Red, + Green, + Blue, + Rgb(u8, u8, u8), + Cyan, +} + +impl Color { + fn is_monochrome(self) -> bool { + match self { + Color::Red | Color::Green | Color::Blue => true, + Color::Rgb(r, g, b) => r | g == 0 || r | b == 0 || g | b == 0, + Color::Cyan => false, + } + } +} + +fn main() { + let color = Color::Rgb(0, 0, 127); + match color { + Color::Red => println!("Red"), + Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan => eprintln!("Not red"), + }; + match color { + Color::Red => println!("Red"), + _not_red @ Color::Green | _not_red @ Color::Blue | _not_red @ Color::Rgb(..) | _not_red @ Color::Cyan => eprintln!("Not red"), + }; + let _str = match color { + Color::Red => "Red".to_owned(), + not_red @ Color::Green | not_red @ Color::Blue | not_red @ Color::Rgb(..) | not_red @ Color::Cyan => format!("{:?}", not_red), + }; + match color { + Color::Red => {}, + Color::Green => {}, + Color::Blue => {}, + Color::Cyan => {}, + c if c.is_monochrome() => {}, + Color::Rgb(_, _, _) => {}, + }; + let _str = match color { + Color::Red => "Red", + c @ Color::Green | c @ Color::Blue | c @ Color::Rgb(_, _, _) | c @ Color::Cyan => "Not red", + }; + match color { + Color::Rgb(r, _, _) if r > 0 => "Some red", + Color::Red | Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan => "No red", + }; + match color { + Color::Red | Color::Green | Color::Blue | Color::Cyan => {}, + Color::Rgb(..) => {}, + }; + let x: u8 = unimplemented!(); + match x { + 0 => {}, + 140 => {}, + _ => {}, + }; +} diff --git a/tests/ui/wildcard_enum_match_arm.rs b/tests/ui/wildcard_enum_match_arm.rs index 94d69d3c8..d33c68a6c 100644 --- a/tests/ui/wildcard_enum_match_arm.rs +++ b/tests/ui/wildcard_enum_match_arm.rs @@ -1,4 +1,7 @@ +// run-rustfix + #![deny(clippy::wildcard_enum_match_arm)] +#![allow(unreachable_code, unused_variables)] #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum Color { diff --git a/tests/ui/wildcard_enum_match_arm.stderr b/tests/ui/wildcard_enum_match_arm.stderr index 999c16933..df90bad15 100644 --- a/tests/ui/wildcard_enum_match_arm.stderr +++ b/tests/ui/wildcard_enum_match_arm.stderr @@ -1,29 +1,29 @@ error: wildcard match will miss any future added variants. - --> $DIR/wildcard_enum_match_arm.rs:26:9 + --> $DIR/wildcard_enum_match_arm.rs:29:9 | LL | _ => eprintln!("Not red"), | ^ help: try this: `Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan` | note: lint level defined here - --> $DIR/wildcard_enum_match_arm.rs:1:9 + --> $DIR/wildcard_enum_match_arm.rs:3:9 | LL | #![deny(clippy::wildcard_enum_match_arm)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: wildcard match will miss any future added variants. - --> $DIR/wildcard_enum_match_arm.rs:30:9 + --> $DIR/wildcard_enum_match_arm.rs:33:9 | LL | _not_red => eprintln!("Not red"), | ^^^^^^^^ help: try this: `_not_red @ Color::Green | _not_red @ Color::Blue | _not_red @ Color::Rgb(..) | _not_red @ Color::Cyan` error: wildcard match will miss any future added variants. - --> $DIR/wildcard_enum_match_arm.rs:34:9 + --> $DIR/wildcard_enum_match_arm.rs:37:9 | LL | not_red => format!("{:?}", not_red), | ^^^^^^^ help: try this: `not_red @ Color::Green | not_red @ Color::Blue | not_red @ Color::Rgb(..) | not_red @ Color::Cyan` error: wildcard match will miss any future added variants. - --> $DIR/wildcard_enum_match_arm.rs:50:9 + --> $DIR/wildcard_enum_match_arm.rs:53:9 | LL | _ => "No red", | ^ help: try this: `Color::Red | Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan`