diff options
| author | bors <bors@rust-lang.org> | 2022-12-22 14:54:50 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-12-22 14:54:50 +0000 |
| commit | 8a6e6fd623a896a37c1d6defed56b7d98273d4fa (patch) | |
| tree | 9a113d61593b81eef00c09bcb7690d744a5b2c28 | |
| parent | f0d331ab36969b129648c8307622bbd746016feb (diff) | |
| parent | ebb0759bb3a9341ff662551366efb1844c91d3ca (diff) | |
| download | rust-8a6e6fd623a896a37c1d6defed56b7d98273d4fa.tar.gz rust-8a6e6fd623a896a37c1d6defed56b7d98273d4fa.zip | |
Auto merge of #10056 - koka831:fix/9993, r=Jarcho
Avoid `match_wildcard_for_single_variants` on guarded wild matches fix #9993 changelog: FP: [`match_wildcard_for_single_variants`]: No longer lints on wildcards with a guard [#10056](https://github.com/rust-lang/rust-clippy/pull/10056) <!-- changelog_checked --> r? `@Jarcho`
| -rw-r--r-- | clippy_lints/src/matches/match_wild_enum.rs | 2 | ||||
| -rw-r--r-- | clippy_utils/src/sugg.rs | 1 | ||||
| -rw-r--r-- | tests/ui/match_wildcard_for_single_variants.fixed | 22 | ||||
| -rw-r--r-- | tests/ui/match_wildcard_for_single_variants.rs | 22 | ||||
| -rw-r--r-- | tests/ui/match_wildcard_for_single_variants.stderr | 8 |
5 files changed, 52 insertions, 3 deletions
diff --git a/clippy_lints/src/matches/match_wild_enum.rs b/clippy_lints/src/matches/match_wild_enum.rs index 7f8d124838c..59de8c0384b 100644 --- a/clippy_lints/src/matches/match_wild_enum.rs +++ b/clippy_lints/src/matches/match_wild_enum.rs @@ -30,7 +30,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) { let mut has_non_wild = false; for arm in arms { match peel_hir_pat_refs(arm.pat).0.kind { - PatKind::Wild => wildcard_span = Some(arm.pat.span), + PatKind::Wild if arm.guard.is_none() => wildcard_span = Some(arm.pat.span), PatKind::Binding(_, _, ident, None) => { wildcard_span = Some(arm.pat.span); wildcard_ident = Some(ident); diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs index b66604f33db..4c4c077d771 100644 --- a/clippy_utils/src/sugg.rs +++ b/clippy_utils/src/sugg.rs @@ -185,7 +185,6 @@ impl<'a> Sugg<'a> { ) -> Self { use rustc_ast::ast::RangeLimits; - #[expect(clippy::match_wildcard_for_single_variants)] match expr.kind { _ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), ast::ExprKind::AddrOf(..) diff --git a/tests/ui/match_wildcard_for_single_variants.fixed b/tests/ui/match_wildcard_for_single_variants.fixed index e675c183ea7..fc252cdd352 100644 --- a/tests/ui/match_wildcard_for_single_variants.fixed +++ b/tests/ui/match_wildcard_for_single_variants.fixed @@ -132,3 +132,25 @@ fn main() { } } } + +mod issue9993 { + enum Foo { + A(bool), + B, + } + + fn test() { + let _ = match Foo::A(true) { + _ if false => 0, + Foo::A(true) => 1, + Foo::A(false) => 2, + Foo::B => 3, + }; + + let _ = match Foo::B { + _ if false => 0, + Foo::A(_) => 1, + Foo::B => 2, + }; + } +} diff --git a/tests/ui/match_wildcard_for_single_variants.rs b/tests/ui/match_wildcard_for_single_variants.rs index 38c3ffc00c7..9a5c849e6ec 100644 --- a/tests/ui/match_wildcard_for_single_variants.rs +++ b/tests/ui/match_wildcard_for_single_variants.rs @@ -132,3 +132,25 @@ fn main() { } } } + +mod issue9993 { + enum Foo { + A(bool), + B, + } + + fn test() { + let _ = match Foo::A(true) { + _ if false => 0, + Foo::A(true) => 1, + Foo::A(false) => 2, + Foo::B => 3, + }; + + let _ = match Foo::B { + _ if false => 0, + Foo::A(_) => 1, + _ => 2, + }; + } +} diff --git a/tests/ui/match_wildcard_for_single_variants.stderr b/tests/ui/match_wildcard_for_single_variants.stderr index 34538dea8e5..6fa313dc911 100644 --- a/tests/ui/match_wildcard_for_single_variants.stderr +++ b/tests/ui/match_wildcard_for_single_variants.stderr @@ -48,5 +48,11 @@ error: wildcard matches only a single variant and will also match any future add LL | _ => (), | ^ help: try this: `Color::Blue` -error: aborting due to 8 previous errors +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 |
