Skip to content

Commit a2f85de

Browse files
committed
Auto merge of #10250 - tylerjw:issue_7419, r=xFrednet
wildcard_enum_match_arm lint takes the enum origin into account fixes #7419 --- changelog: Enhancement: [`wildcard_enum_match_arm`]: Now lints missing private variants, for local enums [#10250](#10250) <!-- changelog_checked -->
2 parents 588c1ba + df7cdf7 commit a2f85de

5 files changed

+20
-9
lines changed

clippy_lints/src/matches/match_wild_enum.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
4545

4646
// Accumulate the variants which should be put in place of the wildcard because they're not
4747
// already covered.
48-
let has_hidden = adt_def.variants().iter().any(|x| is_hidden(cx, x));
49-
let mut missing_variants: Vec<_> = adt_def.variants().iter().filter(|x| !is_hidden(cx, x)).collect();
48+
let is_external = adt_def.did().as_local().is_none();
49+
let has_external_hidden = is_external && adt_def.variants().iter().any(|x| is_hidden(cx, x));
50+
let mut missing_variants: Vec<_> = adt_def
51+
.variants()
52+
.iter()
53+
.filter(|x| !(is_external && is_hidden(cx, x)))
54+
.collect();
5055

5156
let mut path_prefix = CommonPrefixSearcher::None;
5257
for arm in arms {
@@ -133,7 +138,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
133138

134139
match missing_variants.as_slice() {
135140
[] => (),
136-
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
141+
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_external_hidden => span_lint_and_sugg(
137142
cx,
138143
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
139144
wildcard_span,
@@ -144,7 +149,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
144149
),
145150
variants => {
146151
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
147-
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
152+
let message = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
148153
suggestions.push("_".into());
149154
"wildcard matches known variants and will also match future added variants"
150155
} else {

tests/ui/match_wildcard_for_single_variants.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn main() {
123123
Enum::A => (),
124124
Enum::B => (),
125125
Enum::C => (),
126-
_ => (),
126+
Enum::__Private => (),
127127
}
128128
match Enum::A {
129129
Enum::A => (),

tests/ui/match_wildcard_for_single_variants.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ error: wildcard matches only a single variant and will also match any future add
4848
LL | _ => (),
4949
| ^ help: try this: `Color::Blue`
5050

51+
error: wildcard matches only a single variant and will also match any future added variants
52+
--> $DIR/match_wildcard_for_single_variants.rs:126:13
53+
|
54+
LL | _ => (),
55+
| ^ help: try this: `Enum::__Private`
56+
5157
error: wildcard matches only a single variant and will also match any future added variants
5258
--> $DIR/match_wildcard_for_single_variants.rs:153:13
5359
|
5460
LL | _ => 2,
5561
| ^ help: try this: `Foo::B`
5662

57-
error: aborting due to 9 previous errors
63+
error: aborting due to 10 previous errors
5864

tests/ui/wildcard_enum_match_arm.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn main() {
9696
}
9797
match Enum::A {
9898
Enum::A => (),
99-
Enum::B | _ => (),
99+
Enum::B | Enum::__Private => (),
100100
}
101101
}
102102
}

tests/ui/wildcard_enum_match_arm.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ error: wildcard matches known variants and will also match future added variants
3434
LL | _ => {},
3535
| ^ help: try this: `ErrorKind::PermissionDenied | _`
3636

37-
error: wildcard matches known variants and will also match future added variants
37+
error: wildcard match will also match any future added variants
3838
--> $DIR/wildcard_enum_match_arm.rs:99:13
3939
|
4040
LL | _ => (),
41-
| ^ help: try this: `Enum::B | _`
41+
| ^ help: try this: `Enum::B | Enum::__Private`
4242

4343
error: aborting due to 6 previous errors
4444

0 commit comments

Comments
 (0)