Skip to content

Commit e50ef68

Browse files
committed
fix: wildcard_enum_match_arm suggests wrongly with raw identifiers
1 parent 62fd159 commit e50ef68

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

clippy_lints/src/matches/match_wild_enum.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2+
use clippy_utils::source::SpanRangeExt;
23
use clippy_utils::ty::is_type_diagnostic_item;
34
use clippy_utils::{is_refutable, peel_hir_pat_refs, recurse_or_patterns};
45
use rustc_errors::Applicability;
@@ -116,11 +117,12 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
116117
let format_suggestion = |variant: &VariantDef| {
117118
format!(
118119
"{}{}{}{}",
119-
if let Some(ident) = wildcard_ident {
120-
format!("{} @ ", ident.name)
121-
} else {
122-
String::new()
123-
},
120+
wildcard_ident.map_or(String::new(), |ident| {
121+
ident
122+
.span
123+
.get_source_text(cx)
124+
.map_or_else(|| format!("{} @ ", ident.name), |s| format!("{s} @ "))
125+
}),
124126
if let CommonPrefixSearcher::Path(path_prefix) = path_prefix {
125127
let mut s = String::new();
126128
for seg in path_prefix {

tests/ui/wildcard_enum_match_arm.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,17 @@ fn main() {
105105
}
106106
}
107107
}
108+
109+
fn issue15091() {
110+
enum Foo {
111+
A,
112+
B,
113+
C,
114+
}
115+
116+
match Foo::A {
117+
Foo::A => {},
118+
r#type @ Foo::B | r#type @ Foo::C => {},
119+
//~^ wildcard_enum_match_arm
120+
}
121+
}

tests/ui/wildcard_enum_match_arm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,17 @@ fn main() {
105105
}
106106
}
107107
}
108+
109+
fn issue15091() {
110+
enum Foo {
111+
A,
112+
B,
113+
C,
114+
}
115+
116+
match Foo::A {
117+
Foo::A => {},
118+
r#type => {},
119+
//~^ wildcard_enum_match_arm
120+
}
121+
}

tests/ui/wildcard_enum_match_arm.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ error: wildcard match will also match any future added variants
4040
LL | _ => (),
4141
| ^ help: try: `Enum::B | Enum::__Private`
4242

43-
error: aborting due to 6 previous errors
43+
error: wildcard match will also match any future added variants
44+
--> tests/ui/wildcard_enum_match_arm.rs:118:9
45+
|
46+
LL | r#type => {},
47+
| ^^^^^^ help: try: `r#type @ Foo::B | r#type @ Foo::C`
48+
49+
error: aborting due to 7 previous errors
4450

0 commit comments

Comments
 (0)