Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wildcard_enum_match_arm gives suggestions
And is also more robust
- Loading branch information
1 parent
2139fbd
commit 422c9a0
Showing
3 changed files
with
171 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,73 @@ | ||
#![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, | ||
} | ||
#![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, | ||
} | ||
} | ||
|
||
fn main() { | ||
let color = Color::Rgb(0, 0, 127); | ||
match color { | ||
Color::Red => println!("Red"), | ||
_ => eprintln!("Not red"), | ||
}; | ||
match color { | ||
Color::Red => {}, | ||
Color::Green => {}, | ||
Color::Blue => {}, | ||
Color::Cyan => {}, | ||
c if c.is_monochrome() => {}, | ||
Color::Rgb(_, _, _) => {}, | ||
}; | ||
let x: u8 = unimplemented!(); | ||
match x { | ||
0 => {}, | ||
140 => {}, | ||
_ => {}, | ||
}; | ||
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
error: wildcard match will miss any future added variants. | ||
--> $DIR/wildcard_enum_match_arm.rs:26:9 | ||
--> $DIR/wildcard_enum_match_arm.rs:13:9 | ||
| | ||
LL | _ => eprintln!("Not red"), | ||
| ^ | ||
LL | _ => "Could be", | ||
| ^ help: try this: `Maybe::Probably(..) | Maybe::None` | ||
| | ||
note: lint level defined here | ||
--> $DIR/wildcard_enum_match_arm.rs:1:9 | ||
= note: `-D clippy::wildcard-enum-match-arm` implied by `-D warnings` | ||
|
||
error: wildcard match will miss any future added variants. | ||
--> $DIR/wildcard_enum_match_arm.rs:20:9 | ||
| | ||
LL | _other => "Could be", | ||
| ^^^^^^ help: try this: `_other @ Maybe::Some(..) | _other @ Maybe::Probably(..)` | ||
|
||
error: wildcard match will miss any future added variants. | ||
--> $DIR/wildcard_enum_match_arm.rs:27:9 | ||
| | ||
LL | n => format!("{:?}", n), | ||
| ^ help: try this: `n @ Maybe::Probably(..) | n @ Maybe::None` | ||
|
||
error: wildcard match will miss any future added variants. | ||
--> $DIR/wildcard_enum_match_arm.rs:41:9 | ||
| | ||
LL | #![deny(clippy::wildcard_enum_match_arm)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: to resolve, match each variant explicitly | ||
LL | _ => "Who knows", | ||
| ^ help: try this: `Maybe::Some(..) | Maybe::Probably(..) | Maybe::None` | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 4 previous errors | ||
|