diff options
| -rw-r--r-- | clippy_lints/src/matches.rs | 4 | ||||
| -rw-r--r-- | tests/ui/wildcard_enum_match_arm.rs | 125 | ||||
| -rw-r--r-- | tests/ui/wildcard_enum_match_arm.stderr | 30 |
3 files changed, 78 insertions, 81 deletions
diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index a7c66c22968..3de1d01285c 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -537,6 +537,10 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { }) .collect(); + if suggestion.is_empty() { + return; + } + span_lint_and_sugg( cx, WILDCARD_ENUM_MATCH_ARM, diff --git a/tests/ui/wildcard_enum_match_arm.rs b/tests/ui/wildcard_enum_match_arm.rs index 86d4c7f28c4..94d69d3c8a4 100644 --- a/tests/ui/wildcard_enum_match_arm.rs +++ b/tests/ui/wildcard_enum_match_arm.rs @@ -1,73 +1,62 @@ -#![warn(clippy::wildcard_enum_match_arm)] - -#[derive(Debug)] -enum Maybe<T> { - Some(T), - Probably(T), - None, -} - -fn is_it_wildcard<T>(m: Maybe<T>) -> &'static str { - match m { - Maybe::Some(_) => "Some", - _ => "Could be", - } -} - -fn is_it_bound<T>(m: Maybe<T>) -> &'static str { - match m { - Maybe::None => "None", - _other => "Could be", - } -} - -fn is_it_binding(m: Maybe<u32>) -> String { - match m { - Maybe::Some(v) => "Large".to_string(), - n => format!("{:?}", n), - } -} - -fn is_it_binding_exhaustive(m: Maybe<u32>) -> String { - match m { - Maybe::Some(v) => "Large".to_string(), - n @ Maybe::Probably(_) | n @ Maybe::None => format!("{:?}", n), - } -} - -fn is_it_with_guard(m: Maybe<u32>) -> &'static str { - match m { - Maybe::Some(v) if v > 100 => "Large", - _ => "Who knows", - } -} - -fn is_it_exhaustive<T>(m: Maybe<T>) -> &'static str { - match m { - Maybe::None => "None", - Maybe::Some(_) | Maybe::Probably(..) => "Could be", - } -} - -fn is_one_or_three(i: i32) -> bool { - match i { - 1 | 3 => true, - _ => false, +#![deny(clippy::wildcard_enum_match_arm)] + +#[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() { - println!("{}", is_it_wildcard(Maybe::Some("foo"))); - - println!("{}", is_it_bound(Maybe::Some("foo"))); - - println!("{}", is_it_binding(Maybe::Some(1))); - - println!("{}", is_it_binding_exhaustive(Maybe::Some(1))); - - println!("{}", is_it_with_guard(Maybe::Some(1))); - - println!("{}", is_it_exhaustive(Maybe::Some("foo"))); - - println!("{}", is_one_or_three(2)); + let color = Color::Rgb(0, 0, 127); + match color { + Color::Red => println!("Red"), + _ => eprintln!("Not red"), + }; + match color { + Color::Red => println!("Red"), + _not_red => eprintln!("Not red"), + }; + let _str = match color { + Color::Red => "Red".to_owned(), + not_red => 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", + _ => "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.stderr b/tests/ui/wildcard_enum_match_arm.stderr index 1d6f3f662a3..999c1693301 100644 --- a/tests/ui/wildcard_enum_match_arm.stderr +++ b/tests/ui/wildcard_enum_match_arm.stderr @@ -1,28 +1,32 @@ error: wildcard match will miss any future added variants. - --> $DIR/wildcard_enum_match_arm.rs:13:9 + --> $DIR/wildcard_enum_match_arm.rs:26:9 | -LL | _ => "Could be", - | ^ help: try this: `Maybe::Probably(..) | Maybe::None` +LL | _ => eprintln!("Not red"), + | ^ help: try this: `Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan` | - = note: `-D clippy::wildcard-enum-match-arm` implied by `-D warnings` +note: lint level defined here + --> $DIR/wildcard_enum_match_arm.rs:1:9 + | +LL | #![deny(clippy::wildcard_enum_match_arm)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: wildcard match will miss any future added variants. - --> $DIR/wildcard_enum_match_arm.rs:20:9 + --> $DIR/wildcard_enum_match_arm.rs:30:9 | -LL | _other => "Could be", - | ^^^^^^ help: try this: `_other @ Maybe::Some(..) | _other @ Maybe::Probably(..)` +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:27:9 + --> $DIR/wildcard_enum_match_arm.rs:34:9 | -LL | n => format!("{:?}", n), - | ^ help: try this: `n @ Maybe::Probably(..) | n @ Maybe::None` +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:41:9 + --> $DIR/wildcard_enum_match_arm.rs:50:9 | -LL | _ => "Who knows", - | ^ help: try this: `Maybe::Some(..) | Maybe::Probably(..) | Maybe::None` +LL | _ => "No red", + | ^ help: try this: `Color::Red | Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan` error: aborting due to 4 previous errors |
