Skip to content

wildcard_enum_match_arm lint takes the enum origin into account #10250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions clippy_lints/src/matches/match_wild_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {

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

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

match missing_variants.as_slice() {
[] => (),
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_external_hidden => span_lint_and_sugg(
cx,
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
wildcard_span,
Expand All @@ -144,7 +149,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
),
variants => {
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
let message = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
suggestions.push("_".into());
"wildcard matches known variants and will also match future added variants"
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/match_wildcard_for_single_variants.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn main() {
Enum::A => (),
Enum::B => (),
Enum::C => (),
_ => (),
Enum::__Private => (),
}
match Enum::A {
Enum::A => (),
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/match_wildcard_for_single_variants.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ error: wildcard matches only a single variant and will also match any future add
LL | _ => (),
| ^ help: try this: `Color::Blue`

error: wildcard matches only a single variant and will also match any future added variants
--> $DIR/match_wildcard_for_single_variants.rs:126:13
|
LL | _ => (),
| ^ help: try this: `Enum::__Private`

error: wildcard matches only a single variant and will also match any future added variants
--> $DIR/match_wildcard_for_single_variants.rs:153:13
|
LL | _ => 2,
| ^ help: try this: `Foo::B`

error: aborting due to 9 previous errors
error: aborting due to 10 previous errors

2 changes: 1 addition & 1 deletion tests/ui/wildcard_enum_match_arm.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn main() {
}
match Enum::A {
Enum::A => (),
Enum::B | _ => (),
Enum::B | Enum::__Private => (),
}
}
}
4 changes: 2 additions & 2 deletions tests/ui/wildcard_enum_match_arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ error: wildcard matches known variants and will also match future added variants
LL | _ => {},
| ^ help: try this: `ErrorKind::PermissionDenied | _`

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

error: aborting due to 6 previous errors