@@ -446,11 +446,12 @@ declare_clippy_lint! {
446446}
447447
448448declare_clippy_lint ! {
449- /// **What it does:** Checks for `match` expressions producing a `bool` that could be written using `matches!`
449+ /// **What it does:** Checks for `match` or `if let` expressions producing a
450+ /// `bool` that could be written using `matches!`
450451 ///
451452 /// **Why is this bad?** Readability and needless complexity.
452453 ///
453- /// **Known problems:** This can turn an intentionally exhaustive match into a non-exhaustive one.
454+ /// **Known problems:** None
454455 ///
455456 /// **Example:**
456457 /// ```rust
@@ -462,8 +463,14 @@ declare_clippy_lint! {
462463 /// _ => false,
463464 /// };
464465 ///
466+ /// let a = if let Some(0) = x {
467+ /// true
468+ /// } else {
469+ /// false
470+ /// };
471+ ///
465472 /// // Good
466- /// let a = matches!(x, Some(5 ));
473+ /// let a = matches!(x, Some(0 ));
467474 /// ```
468475 pub MATCH_LIKE_MATCHES_MACRO ,
469476 style,
@@ -499,9 +506,8 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
499506 return ;
500507 }
501508
502- if !redundant_pattern_match:: check ( cx, expr) {
503- check_match_like_matches ( cx, expr) ;
504- }
509+ redundant_pattern_match:: check ( cx, expr) ;
510+ check_match_like_matches ( cx, expr) ;
505511
506512 if let ExprKind :: Match ( ref ex, ref arms, MatchSource :: Normal ) = expr. kind {
507513 check_single_match ( cx, ex, arms, expr) ;
@@ -1068,6 +1074,7 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
10681074 if_chain ! {
10691075 if arms. len( ) == 2 ;
10701076 if cx. tables( ) . expr_ty( expr) . is_bool( ) ;
1077+ if is_wild( & arms[ 1 ] . pat) ;
10711078 if let Some ( first) = find_bool_lit( & arms[ 0 ] . body. kind, desugared) ;
10721079 if let Some ( second) = find_bool_lit( & arms[ 1 ] . body. kind, desugared) ;
10731080 if first != second;
@@ -1437,16 +1444,14 @@ mod redundant_pattern_match {
14371444 use rustc_mir:: const_eval:: is_const_fn;
14381445 use rustc_span:: source_map:: Symbol ;
14391446
1440- pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) -> bool {
1447+ pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
14411448 if let ExprKind :: Match ( op, arms, ref match_source) = & expr. kind {
14421449 match match_source {
14431450 MatchSource :: Normal => find_sugg_for_match ( cx, expr, op, arms) ,
14441451 MatchSource :: IfLetDesugar { .. } => find_sugg_for_if_let ( cx, expr, op, arms, "if" ) ,
14451452 MatchSource :: WhileLetDesugar => find_sugg_for_if_let ( cx, expr, op, arms, "while" ) ,
1446- _ => false ,
1453+ _ => { } ,
14471454 }
1448- } else {
1449- false
14501455 }
14511456 }
14521457
@@ -1456,7 +1461,7 @@ mod redundant_pattern_match {
14561461 op : & Expr < ' _ > ,
14571462 arms : & [ Arm < ' _ > ] ,
14581463 keyword : & ' static str ,
1459- ) -> bool {
1464+ ) {
14601465 fn find_suggestion ( cx : & LateContext < ' _ > , hir_id : HirId , path : & QPath < ' _ > ) -> Option < & ' static str > {
14611466 if match_qpath ( path, & paths:: RESULT_OK ) && can_suggest ( cx, hir_id, sym ! ( result_type) , "is_ok" ) {
14621467 return Some ( "is_ok()" ) ;
@@ -1487,7 +1492,7 @@ mod redundant_pattern_match {
14871492 } ;
14881493 let good_method = match good_method {
14891494 Some ( method) => method,
1490- None => return false ,
1495+ None => return ,
14911496 } ;
14921497
14931498 // check that `while_let_on_iterator` lint does not trigger
@@ -1497,7 +1502,7 @@ mod redundant_pattern_match {
14971502 if method_path. ident. name == sym!( next) ;
14981503 if match_trait_method( cx, op, & paths:: ITERATOR ) ;
14991504 then {
1500- return false ;
1505+ return ;
15011506 }
15021507 }
15031508
@@ -1526,15 +1531,9 @@ mod redundant_pattern_match {
15261531 ) ;
15271532 } ,
15281533 ) ;
1529- true
15301534 }
15311535
1532- fn find_sugg_for_match < ' tcx > (
1533- cx : & LateContext < ' tcx > ,
1534- expr : & ' tcx Expr < ' _ > ,
1535- op : & Expr < ' _ > ,
1536- arms : & [ Arm < ' _ > ] ,
1537- ) -> bool {
1536+ fn find_sugg_for_match < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > , op : & Expr < ' _ > , arms : & [ Arm < ' _ > ] ) {
15381537 if arms. len ( ) == 2 {
15391538 let node_pair = ( & arms[ 0 ] . pat . kind , & arms[ 1 ] . pat . kind ) ;
15401539
@@ -1599,10 +1598,8 @@ mod redundant_pattern_match {
15991598 ) ;
16001599 } ,
16011600 ) ;
1602- return true ;
16031601 }
16041602 }
1605- false
16061603 }
16071604
16081605 #[ allow( clippy:: too_many_arguments) ]
0 commit comments