@@ -21,6 +21,7 @@ mod redundant_pattern_match;
2121mod rest_pat_in_fully_bound_struct;
2222mod single_match;
2323mod wild_in_or_pats;
24+ mod nop_match;
2425
2526declare_clippy_lint ! {
2627 /// ### What it does
@@ -566,6 +567,49 @@ declare_clippy_lint! {
566567 "`match` with identical arm bodies"
567568}
568569
570+ declare_clippy_lint ! {
571+ /// ### What it does
572+ /// Checks for unnecessary `match` or match-like `if let` returns for `Option` and `Result`
573+ /// when function signatures are the same.
574+ ///
575+ /// ### Why is this bad?
576+ /// This `match` block does nothing and might not be what the coder intended.
577+ ///
578+ /// ### Example
579+ /// ```rust,ignore
580+ /// fn foo() -> Result<(), i32> {
581+ /// match result {
582+ /// Ok(val) => Ok(val),
583+ /// Err(err) => Err(err),
584+ /// }
585+ /// }
586+ ///
587+ /// fn bar() -> Option<i32> {
588+ /// if let Some(val) = option {
589+ /// Some(val)
590+ /// } else {
591+ /// None
592+ /// }
593+ /// }
594+ /// ```
595+ ///
596+ /// Could be replaced as
597+ ///
598+ /// ```rust,ignore
599+ /// fn foo() -> Result<(), i32> {
600+ /// result
601+ /// }
602+ ///
603+ /// fn bar() -> Option<i32> {
604+ /// option
605+ /// }
606+ /// ```
607+ #[ clippy:: version = "1.61.0" ]
608+ pub NOP_MATCH ,
609+ correctness,
610+ "`match` or match-like `if let` that are unnecessary"
611+ }
612+
569613#[ derive( Default ) ]
570614pub struct Matches {
571615 msrv : Option < RustcVersion > ,
@@ -599,6 +643,7 @@ impl_lint_pass!(Matches => [
599643 REDUNDANT_PATTERN_MATCHING ,
600644 MATCH_LIKE_MATCHES_MACRO ,
601645 MATCH_SAME_ARMS ,
646+ NOP_MATCH ,
602647] ) ;
603648
604649impl < ' tcx > LateLintPass < ' tcx > for Matches {
@@ -622,6 +667,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
622667 overlapping_arms:: check ( cx, ex, arms) ;
623668 match_wild_enum:: check ( cx, ex, arms) ;
624669 match_as_ref:: check ( cx, ex, arms, expr) ;
670+ nop_match:: check_match ( cx, ex, arms) ;
625671
626672 if self . infallible_destructuring_match_linted {
627673 self . infallible_destructuring_match_linted = false ;
@@ -640,6 +686,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
640686 match_like_matches:: check ( cx, expr) ;
641687 }
642688 redundant_pattern_match:: check ( cx, expr) ;
689+ nop_match:: check ( cx, expr) ;
643690 }
644691 }
645692
0 commit comments