Skip to content

Commit 8a6e6fd

Browse files
committed
Auto merge of rust-lang#10056 - koka831:fix/9993, r=Jarcho
Avoid `match_wildcard_for_single_variants` on guarded wild matches fix rust-lang#9993 changelog: FP: [`match_wildcard_for_single_variants`]: No longer lints on wildcards with a guard [rust-lang#10056](rust-lang/rust-clippy#10056) <!-- changelog_checked --> r? `@Jarcho`
2 parents f0d331a + ebb0759 commit 8a6e6fd

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

clippy_lints/src/matches/match_wild_enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
3030
let mut has_non_wild = false;
3131
for arm in arms {
3232
match peel_hir_pat_refs(arm.pat).0.kind {
33-
PatKind::Wild => wildcard_span = Some(arm.pat.span),
33+
PatKind::Wild if arm.guard.is_none() => wildcard_span = Some(arm.pat.span),
3434
PatKind::Binding(_, _, ident, None) => {
3535
wildcard_span = Some(arm.pat.span);
3636
wildcard_ident = Some(ident);

clippy_utils/src/sugg.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ impl<'a> Sugg<'a> {
185185
) -> Self {
186186
use rustc_ast::ast::RangeLimits;
187187

188-
#[expect(clippy::match_wildcard_for_single_variants)]
189188
match expr.kind {
190189
_ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
191190
ast::ExprKind::AddrOf(..)

tests/ui/match_wildcard_for_single_variants.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,25 @@ fn main() {
132132
}
133133
}
134134
}
135+
136+
mod issue9993 {
137+
enum Foo {
138+
A(bool),
139+
B,
140+
}
141+
142+
fn test() {
143+
let _ = match Foo::A(true) {
144+
_ if false => 0,
145+
Foo::A(true) => 1,
146+
Foo::A(false) => 2,
147+
Foo::B => 3,
148+
};
149+
150+
let _ = match Foo::B {
151+
_ if false => 0,
152+
Foo::A(_) => 1,
153+
Foo::B => 2,
154+
};
155+
}
156+
}

tests/ui/match_wildcard_for_single_variants.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,25 @@ fn main() {
132132
}
133133
}
134134
}
135+
136+
mod issue9993 {
137+
enum Foo {
138+
A(bool),
139+
B,
140+
}
141+
142+
fn test() {
143+
let _ = match Foo::A(true) {
144+
_ if false => 0,
145+
Foo::A(true) => 1,
146+
Foo::A(false) => 2,
147+
Foo::B => 3,
148+
};
149+
150+
let _ = match Foo::B {
151+
_ if false => 0,
152+
Foo::A(_) => 1,
153+
_ => 2,
154+
};
155+
}
156+
}

tests/ui/match_wildcard_for_single_variants.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ 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: aborting due to 8 previous errors
51+
error: wildcard matches only a single variant and will also match any future added variants
52+
--> $DIR/match_wildcard_for_single_variants.rs:153:13
53+
|
54+
LL | _ => 2,
55+
| ^ help: try this: `Foo::B`
56+
57+
error: aborting due to 9 previous errors
5258

0 commit comments

Comments
 (0)