Skip to content

Commit bcefd68

Browse files
committed
Restore tests
Also, fix existing test
1 parent 422c9a0 commit bcefd68

File tree

3 files changed

+78
-81
lines changed

3 files changed

+78
-81
lines changed

clippy_lints/src/matches.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
537537
})
538538
.collect();
539539

540+
if suggestion.is_empty() {
541+
return;
542+
}
543+
540544
span_lint_and_sugg(
541545
cx,
542546
WILDCARD_ENUM_MATCH_ARM,

tests/ui/wildcard_enum_match_arm.rs

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,62 @@
1-
#![warn(clippy::wildcard_enum_match_arm)]
2-
3-
#[derive(Debug)]
4-
enum Maybe<T> {
5-
Some(T),
6-
Probably(T),
7-
None,
8-
}
9-
10-
fn is_it_wildcard<T>(m: Maybe<T>) -> &'static str {
11-
match m {
12-
Maybe::Some(_) => "Some",
13-
_ => "Could be",
14-
}
15-
}
16-
17-
fn is_it_bound<T>(m: Maybe<T>) -> &'static str {
18-
match m {
19-
Maybe::None => "None",
20-
_other => "Could be",
21-
}
22-
}
23-
24-
fn is_it_binding(m: Maybe<u32>) -> String {
25-
match m {
26-
Maybe::Some(v) => "Large".to_string(),
27-
n => format!("{:?}", n),
28-
}
29-
}
30-
31-
fn is_it_binding_exhaustive(m: Maybe<u32>) -> String {
32-
match m {
33-
Maybe::Some(v) => "Large".to_string(),
34-
n @ Maybe::Probably(_) | n @ Maybe::None => format!("{:?}", n),
35-
}
36-
}
37-
38-
fn is_it_with_guard(m: Maybe<u32>) -> &'static str {
39-
match m {
40-
Maybe::Some(v) if v > 100 => "Large",
41-
_ => "Who knows",
42-
}
43-
}
44-
45-
fn is_it_exhaustive<T>(m: Maybe<T>) -> &'static str {
46-
match m {
47-
Maybe::None => "None",
48-
Maybe::Some(_) | Maybe::Probably(..) => "Could be",
49-
}
50-
}
51-
52-
fn is_one_or_three(i: i32) -> bool {
53-
match i {
54-
1 | 3 => true,
55-
_ => false,
1+
#![deny(clippy::wildcard_enum_match_arm)]
2+
3+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4+
enum Color {
5+
Red,
6+
Green,
7+
Blue,
8+
Rgb(u8, u8, u8),
9+
Cyan,
10+
}
11+
12+
impl Color {
13+
fn is_monochrome(self) -> bool {
14+
match self {
15+
Color::Red | Color::Green | Color::Blue => true,
16+
Color::Rgb(r, g, b) => r | g == 0 || r | b == 0 || g | b == 0,
17+
Color::Cyan => false,
18+
}
5619
}
5720
}
5821

5922
fn main() {
60-
println!("{}", is_it_wildcard(Maybe::Some("foo")));
61-
62-
println!("{}", is_it_bound(Maybe::Some("foo")));
63-
64-
println!("{}", is_it_binding(Maybe::Some(1)));
65-
66-
println!("{}", is_it_binding_exhaustive(Maybe::Some(1)));
67-
68-
println!("{}", is_it_with_guard(Maybe::Some(1)));
69-
70-
println!("{}", is_it_exhaustive(Maybe::Some("foo")));
71-
72-
println!("{}", is_one_or_three(2));
23+
let color = Color::Rgb(0, 0, 127);
24+
match color {
25+
Color::Red => println!("Red"),
26+
_ => eprintln!("Not red"),
27+
};
28+
match color {
29+
Color::Red => println!("Red"),
30+
_not_red => eprintln!("Not red"),
31+
};
32+
let _str = match color {
33+
Color::Red => "Red".to_owned(),
34+
not_red => format!("{:?}", not_red),
35+
};
36+
match color {
37+
Color::Red => {},
38+
Color::Green => {},
39+
Color::Blue => {},
40+
Color::Cyan => {},
41+
c if c.is_monochrome() => {},
42+
Color::Rgb(_, _, _) => {},
43+
};
44+
let _str = match color {
45+
Color::Red => "Red",
46+
c @ Color::Green | c @ Color::Blue | c @ Color::Rgb(_, _, _) | c @ Color::Cyan => "Not red",
47+
};
48+
match color {
49+
Color::Rgb(r, _, _) if r > 0 => "Some red",
50+
_ => "No red",
51+
};
52+
match color {
53+
Color::Red | Color::Green | Color::Blue | Color::Cyan => {},
54+
Color::Rgb(..) => {},
55+
};
56+
let x: u8 = unimplemented!();
57+
match x {
58+
0 => {},
59+
140 => {},
60+
_ => {},
61+
};
7362
}
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
error: wildcard match will miss any future added variants.
2-
--> $DIR/wildcard_enum_match_arm.rs:13:9
2+
--> $DIR/wildcard_enum_match_arm.rs:26:9
33
|
4-
LL | _ => "Could be",
5-
| ^ help: try this: `Maybe::Probably(..) | Maybe::None`
4+
LL | _ => eprintln!("Not red"),
5+
| ^ help: try this: `Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan`
66
|
7-
= note: `-D clippy::wildcard-enum-match-arm` implied by `-D warnings`
7+
note: lint level defined here
8+
--> $DIR/wildcard_enum_match_arm.rs:1:9
9+
|
10+
LL | #![deny(clippy::wildcard_enum_match_arm)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
812

913
error: wildcard match will miss any future added variants.
10-
--> $DIR/wildcard_enum_match_arm.rs:20:9
14+
--> $DIR/wildcard_enum_match_arm.rs:30:9
1115
|
12-
LL | _other => "Could be",
13-
| ^^^^^^ help: try this: `_other @ Maybe::Some(..) | _other @ Maybe::Probably(..)`
16+
LL | _not_red => eprintln!("Not red"),
17+
| ^^^^^^^^ help: try this: `_not_red @ Color::Green | _not_red @ Color::Blue | _not_red @ Color::Rgb(..) | _not_red @ Color::Cyan`
1418

1519
error: wildcard match will miss any future added variants.
16-
--> $DIR/wildcard_enum_match_arm.rs:27:9
20+
--> $DIR/wildcard_enum_match_arm.rs:34:9
1721
|
18-
LL | n => format!("{:?}", n),
19-
| ^ help: try this: `n @ Maybe::Probably(..) | n @ Maybe::None`
22+
LL | not_red => format!("{:?}", not_red),
23+
| ^^^^^^^ help: try this: `not_red @ Color::Green | not_red @ Color::Blue | not_red @ Color::Rgb(..) | not_red @ Color::Cyan`
2024

2125
error: wildcard match will miss any future added variants.
22-
--> $DIR/wildcard_enum_match_arm.rs:41:9
26+
--> $DIR/wildcard_enum_match_arm.rs:50:9
2327
|
24-
LL | _ => "Who knows",
25-
| ^ help: try this: `Maybe::Some(..) | Maybe::Probably(..) | Maybe::None`
28+
LL | _ => "No red",
29+
| ^ help: try this: `Color::Red | Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan`
2630

2731
error: aborting due to 4 previous errors
2832

0 commit comments

Comments
 (0)