@@ -446,11 +446,12 @@ declare_clippy_lint! {
446
446
}
447
447
448
448
declare_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!`
450
451
///
451
452
/// **Why is this bad?** Readability and needless complexity.
452
453
///
453
- /// **Known problems:** This can turn an intentionally exhaustive match into a non-exhaustive one.
454
+ /// **Known problems:** None
454
455
///
455
456
/// **Example:**
456
457
/// ```rust
@@ -462,8 +463,14 @@ declare_clippy_lint! {
462
463
/// _ => false,
463
464
/// };
464
465
///
466
+ /// let a = if let Some(0) = x {
467
+ /// true
468
+ /// } else {
469
+ /// false
470
+ /// };
471
+ ///
465
472
/// // Good
466
- /// let a = matches!(x, Some(5 ));
473
+ /// let a = matches!(x, Some(0 ));
467
474
/// ```
468
475
pub MATCH_LIKE_MATCHES_MACRO ,
469
476
style,
@@ -499,9 +506,8 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
499
506
return ;
500
507
}
501
508
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) ;
505
511
506
512
if let ExprKind :: Match ( ref ex, ref arms, MatchSource :: Normal ) = expr. kind {
507
513
check_single_match ( cx, ex, arms, expr) ;
@@ -1068,6 +1074,7 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
1068
1074
if_chain ! {
1069
1075
if arms. len( ) == 2 ;
1070
1076
if cx. tables( ) . expr_ty( expr) . is_bool( ) ;
1077
+ if is_wild( & arms[ 1 ] . pat) ;
1071
1078
if let Some ( first) = find_bool_lit( & arms[ 0 ] . body. kind, desugared) ;
1072
1079
if let Some ( second) = find_bool_lit( & arms[ 1 ] . body. kind, desugared) ;
1073
1080
if first != second;
@@ -1437,16 +1444,14 @@ mod redundant_pattern_match {
1437
1444
use rustc_mir:: const_eval:: is_const_fn;
1438
1445
use rustc_span:: source_map:: Symbol ;
1439
1446
1440
- pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) -> bool {
1447
+ pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
1441
1448
if let ExprKind :: Match ( op, arms, ref match_source) = & expr. kind {
1442
1449
match match_source {
1443
1450
MatchSource :: Normal => find_sugg_for_match ( cx, expr, op, arms) ,
1444
1451
MatchSource :: IfLetDesugar { .. } => find_sugg_for_if_let ( cx, expr, op, arms, "if" ) ,
1445
1452
MatchSource :: WhileLetDesugar => find_sugg_for_if_let ( cx, expr, op, arms, "while" ) ,
1446
- _ => false ,
1453
+ _ => { } ,
1447
1454
}
1448
- } else {
1449
- false
1450
1455
}
1451
1456
}
1452
1457
@@ -1456,7 +1461,7 @@ mod redundant_pattern_match {
1456
1461
op : & Expr < ' _ > ,
1457
1462
arms : & [ Arm < ' _ > ] ,
1458
1463
keyword : & ' static str ,
1459
- ) -> bool {
1464
+ ) {
1460
1465
fn find_suggestion ( cx : & LateContext < ' _ > , hir_id : HirId , path : & QPath < ' _ > ) -> Option < & ' static str > {
1461
1466
if match_qpath ( path, & paths:: RESULT_OK ) && can_suggest ( cx, hir_id, sym ! ( result_type) , "is_ok" ) {
1462
1467
return Some ( "is_ok()" ) ;
@@ -1487,7 +1492,7 @@ mod redundant_pattern_match {
1487
1492
} ;
1488
1493
let good_method = match good_method {
1489
1494
Some ( method) => method,
1490
- None => return false ,
1495
+ None => return ,
1491
1496
} ;
1492
1497
1493
1498
// check that `while_let_on_iterator` lint does not trigger
@@ -1497,7 +1502,7 @@ mod redundant_pattern_match {
1497
1502
if method_path. ident. name == sym!( next) ;
1498
1503
if match_trait_method( cx, op, & paths:: ITERATOR ) ;
1499
1504
then {
1500
- return false ;
1505
+ return ;
1501
1506
}
1502
1507
}
1503
1508
@@ -1526,15 +1531,9 @@ mod redundant_pattern_match {
1526
1531
) ;
1527
1532
} ,
1528
1533
) ;
1529
- true
1530
1534
}
1531
1535
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 < ' _ > ] ) {
1538
1537
if arms. len ( ) == 2 {
1539
1538
let node_pair = ( & arms[ 0 ] . pat . kind , & arms[ 1 ] . pat . kind ) ;
1540
1539
@@ -1599,10 +1598,8 @@ mod redundant_pattern_match {
1599
1598
) ;
1600
1599
} ,
1601
1600
) ;
1602
- return true ;
1603
1601
}
1604
1602
}
1605
- false
1606
1603
}
1607
1604
1608
1605
#[ allow( clippy:: too_many_arguments) ]
0 commit comments