Skip to content

Commit 2432e97

Browse files
committed
wildcard_enum_match_arm lint takes the enum origin into account
Signed-off-by: Tyler Weaver <[email protected]>
1 parent d020fd7 commit 2432e97

5 files changed

+22
-11
lines changed

clippy_lints/src/matches/match_wild_enum.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ 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 has_hidden_external = adt_def.variants().iter().any(|x| is_hidden_and_external(cx, x));
49+
let mut missing_variants: Vec<_> = adt_def
50+
.variants()
51+
.iter()
52+
.filter(|x| !is_hidden_and_external(cx, x))
53+
.collect();
5054

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

134138
match missing_variants.as_slice() {
135139
[] => (),
136-
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
140+
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden_external => span_lint_and_sugg(
137141
cx,
138142
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
139143
wildcard_span,
@@ -144,7 +148,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
144148
),
145149
variants => {
146150
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
147-
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
151+
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden_external {
148152
suggestions.push("_".into());
149153
"wildcard matches known variants and will also match future added variants"
150154
} else {
@@ -191,6 +195,7 @@ impl<'a> CommonPrefixSearcher<'a> {
191195
}
192196
}
193197

194-
fn is_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
195-
cx.tcx.is_doc_hidden(variant_def.def_id) || cx.tcx.has_attr(variant_def.def_id, sym::unstable)
198+
fn is_hidden_and_external(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
199+
(cx.tcx.is_doc_hidden(variant_def.def_id) || cx.tcx.has_attr(variant_def.def_id, sym::unstable))
200+
&& variant_def.def_id.as_local().is_none()
196201
}

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)