@@ -229,6 +229,40 @@ declare_clippy_lint! {
229
229
"a wildcard enum match arm using `_`"
230
230
}
231
231
232
+ declare_clippy_lint ! {
233
+ /// **What it does:** Checks for wildcard enum matches for a single variant.
234
+ ///
235
+ /// **Why is this bad?** New enum variants added by library updates can be missed.
236
+ ///
237
+ /// **Known problems:** Suggested replacements may not use correct path to enum
238
+ /// if it's not present in the current scope.
239
+ ///
240
+ /// **Example:**
241
+ ///
242
+ /// ```rust
243
+ /// # enum Foo { A, B, C }
244
+ /// # let x = Foo::B;
245
+ /// match x {
246
+ /// Foo::A => {},
247
+ /// Foo::B => {},
248
+ /// _ => {},
249
+ /// }
250
+ /// ```
251
+ /// Use instead:
252
+ /// ```rust
253
+ /// # enum Foo { A, B, C }
254
+ /// # let x = Foo::B;
255
+ /// match x {
256
+ /// Foo::A => {},
257
+ /// Foo::B => {},
258
+ /// Foo::C => {},
259
+ /// }
260
+ /// ```
261
+ pub MATCH_WILDCARD_FOR_SINGLE_VARIANTS ,
262
+ pedantic,
263
+ "a wildcard enum match for a single variant"
264
+ }
265
+
232
266
declare_clippy_lint ! {
233
267
/// **What it does:** Checks for wildcard pattern used with others patterns in same match arm.
234
268
///
@@ -356,6 +390,7 @@ impl_lint_pass!(Matches => [
356
390
MATCH_WILD_ERR_ARM ,
357
391
MATCH_AS_REF ,
358
392
WILDCARD_ENUM_MATCH_ARM ,
393
+ MATCH_WILDCARD_FOR_SINGLE_VARIANTS ,
359
394
WILDCARD_IN_OR_PATTERNS ,
360
395
MATCH_SINGLE_BINDING ,
361
396
INFALLIBLE_DESTRUCTURING_MATCH ,
@@ -766,6 +801,19 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_
766
801
}
767
802
}
768
803
804
+ if suggestion. len ( ) == 1 {
805
+ // No need to check for non-exhaustive enum as in that case len would be greater than 1
806
+ span_lint_and_sugg (
807
+ cx,
808
+ MATCH_WILDCARD_FOR_SINGLE_VARIANTS ,
809
+ wildcard_span,
810
+ message,
811
+ "try this" ,
812
+ suggestion[ 0 ] . clone ( ) ,
813
+ Applicability :: MachineApplicable ,
814
+ )
815
+ } ;
816
+
769
817
span_lint_and_sugg (
770
818
cx,
771
819
WILDCARD_ENUM_MATCH_ARM ,
0 commit comments