Skip to content

Commit 055f349

Browse files
committed
Avoid match_wildcard_for_single_variants on guarded wild matches
fix rust-lang#9993 changlog: [`match_wildcard_for_single_variants`] avoid suggestion on wildcard with guard
1 parent ef2018c commit 055f349

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,19 @@ 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+
}

tests/ui/match_wildcard_for_single_variants.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,19 @@ 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+
}

0 commit comments

Comments
 (0)