@@ -1440,15 +1440,12 @@ where
1440
1440
1441
1441
mod redundant_pattern_match {
1442
1442
use super :: REDUNDANT_PATTERN_MATCHING ;
1443
- use crate :: utils:: { in_constant , match_qpath, match_trait_method, paths, snippet, span_lint_and_then} ;
1443
+ use crate :: utils:: { match_qpath, match_trait_method, paths, snippet, span_lint_and_then} ;
1444
1444
use if_chain:: if_chain;
1445
1445
use rustc_ast:: ast:: LitKind ;
1446
1446
use rustc_errors:: Applicability ;
1447
- use rustc_hir:: { Arm , Expr , ExprKind , HirId , MatchSource , PatKind , QPath } ;
1447
+ use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , PatKind , QPath } ;
1448
1448
use rustc_lint:: LateContext ;
1449
- use rustc_middle:: ty;
1450
- use rustc_mir:: const_eval:: is_const_fn;
1451
- use rustc_span:: source_map:: Symbol ;
1452
1449
1453
1450
pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
1454
1451
if let ExprKind :: Match ( op, arms, ref match_source) = & expr. kind {
@@ -1468,37 +1465,24 @@ mod redundant_pattern_match {
1468
1465
arms : & [ Arm < ' _ > ] ,
1469
1466
keyword : & ' static str ,
1470
1467
) {
1471
- fn find_suggestion ( cx : & LateContext < ' _ > , hir_id : HirId , path : & QPath < ' _ > ) -> Option < & ' static str > {
1472
- if match_qpath ( path, & paths:: RESULT_OK ) && can_suggest ( cx, hir_id, sym ! ( result_type) , "is_ok" ) {
1473
- return Some ( "is_ok()" ) ;
1474
- }
1475
- if match_qpath ( path, & paths:: RESULT_ERR ) && can_suggest ( cx, hir_id, sym ! ( result_type) , "is_err" ) {
1476
- return Some ( "is_err()" ) ;
1477
- }
1478
- if match_qpath ( path, & paths:: OPTION_SOME ) && can_suggest ( cx, hir_id, sym ! ( option_type) , "is_some" ) {
1479
- return Some ( "is_some()" ) ;
1480
- }
1481
- if match_qpath ( path, & paths:: OPTION_NONE ) && can_suggest ( cx, hir_id, sym ! ( option_type) , "is_none" ) {
1482
- return Some ( "is_none()" ) ;
1483
- }
1484
- None
1485
- }
1486
-
1487
- let hir_id = expr. hir_id ;
1488
1468
let good_method = match arms[ 0 ] . pat . kind {
1489
1469
PatKind :: TupleStruct ( ref path, ref patterns, _) if patterns. len ( ) == 1 => {
1490
1470
if let PatKind :: Wild = patterns[ 0 ] . kind {
1491
- find_suggestion ( cx, hir_id, path)
1471
+ if match_qpath ( path, & paths:: RESULT_OK ) {
1472
+ "is_ok()"
1473
+ } else if match_qpath ( path, & paths:: RESULT_ERR ) {
1474
+ "is_err()"
1475
+ } else if match_qpath ( path, & paths:: OPTION_SOME ) {
1476
+ "is_some()"
1477
+ } else {
1478
+ return ;
1479
+ }
1492
1480
} else {
1493
- None
1481
+ return ;
1494
1482
}
1495
1483
} ,
1496
- PatKind :: Path ( ref path) => find_suggestion ( cx, hir_id, path) ,
1497
- _ => None ,
1498
- } ;
1499
- let good_method = match good_method {
1500
- Some ( method) => method,
1501
- None => return ,
1484
+ PatKind :: Path ( ref path) if match_qpath ( path, & paths:: OPTION_NONE ) => "is_none()" ,
1485
+ _ => return ,
1502
1486
} ;
1503
1487
1504
1488
// check that `while_let_on_iterator` lint does not trigger
@@ -1547,7 +1531,6 @@ mod redundant_pattern_match {
1547
1531
if arms. len ( ) == 2 {
1548
1532
let node_pair = ( & arms[ 0 ] . pat . kind , & arms[ 1 ] . pat . kind ) ;
1549
1533
1550
- let hir_id = expr. hir_id ;
1551
1534
let found_good_method = match node_pair {
1552
1535
(
1553
1536
PatKind :: TupleStruct ( ref path_left, ref patterns_left, _) ,
@@ -1562,8 +1545,6 @@ mod redundant_pattern_match {
1562
1545
& paths:: RESULT_ERR ,
1563
1546
"is_ok()" ,
1564
1547
"is_err()" ,
1565
- || can_suggest ( cx, hir_id, sym ! ( result_type) , "is_ok" ) ,
1566
- || can_suggest ( cx, hir_id, sym ! ( result_type) , "is_err" ) ,
1567
1548
)
1568
1549
} else {
1569
1550
None
@@ -1582,8 +1563,6 @@ mod redundant_pattern_match {
1582
1563
& paths:: OPTION_NONE ,
1583
1564
"is_some()" ,
1584
1565
"is_none()" ,
1585
- || can_suggest ( cx, hir_id, sym ! ( option_type) , "is_some" ) ,
1586
- || can_suggest ( cx, hir_id, sym ! ( option_type) , "is_none" ) ,
1587
1566
)
1588
1567
} else {
1589
1568
None
@@ -1616,7 +1595,6 @@ mod redundant_pattern_match {
1616
1595
}
1617
1596
}
1618
1597
1619
- #[ allow( clippy:: too_many_arguments) ]
1620
1598
fn find_good_method_for_match < ' a > (
1621
1599
arms : & [ Arm < ' _ > ] ,
1622
1600
path_left : & QPath < ' _ > ,
@@ -1625,8 +1603,6 @@ mod redundant_pattern_match {
1625
1603
expected_right : & [ & str ] ,
1626
1604
should_be_left : & ' a str ,
1627
1605
should_be_right : & ' a str ,
1628
- can_suggest_left : impl Fn ( ) -> bool ,
1629
- can_suggest_right : impl Fn ( ) -> bool ,
1630
1606
) -> Option < & ' a str > {
1631
1607
let body_node_pair = if match_qpath ( path_left, expected_left) && match_qpath ( path_right, expected_right) {
1632
1608
( & ( * arms[ 0 ] . body ) . kind , & ( * arms[ 1 ] . body ) . kind )
@@ -1638,35 +1614,13 @@ mod redundant_pattern_match {
1638
1614
1639
1615
match body_node_pair {
1640
1616
( ExprKind :: Lit ( ref lit_left) , ExprKind :: Lit ( ref lit_right) ) => match ( & lit_left. node , & lit_right. node ) {
1641
- ( LitKind :: Bool ( true ) , LitKind :: Bool ( false ) ) if can_suggest_left ( ) => Some ( should_be_left) ,
1642
- ( LitKind :: Bool ( false ) , LitKind :: Bool ( true ) ) if can_suggest_right ( ) => Some ( should_be_right) ,
1617
+ ( LitKind :: Bool ( true ) , LitKind :: Bool ( false ) ) => Some ( should_be_left) ,
1618
+ ( LitKind :: Bool ( false ) , LitKind :: Bool ( true ) ) => Some ( should_be_right) ,
1643
1619
_ => None ,
1644
1620
} ,
1645
1621
_ => None ,
1646
1622
}
1647
1623
}
1648
-
1649
- fn can_suggest ( cx : & LateContext < ' _ > , hir_id : HirId , diag_item : Symbol , name : & str ) -> bool {
1650
- if !in_constant ( cx, hir_id) {
1651
- return true ;
1652
- }
1653
-
1654
- // Avoid suggesting calls to non-`const fn`s in const contexts, see #5697.
1655
- cx. tcx
1656
- . get_diagnostic_item ( diag_item)
1657
- . and_then ( |def_id| {
1658
- cx. tcx . inherent_impls ( def_id) . iter ( ) . find_map ( |imp| {
1659
- cx. tcx
1660
- . associated_items ( * imp)
1661
- . in_definition_order ( )
1662
- . find_map ( |item| match item. kind {
1663
- ty:: AssocKind :: Fn if item. ident . name . as_str ( ) == name => Some ( item. def_id ) ,
1664
- _ => None ,
1665
- } )
1666
- } )
1667
- } )
1668
- . map_or ( false , |def_id| is_const_fn ( cx. tcx , def_id) )
1669
- }
1670
1624
}
1671
1625
1672
1626
#[ test]
0 commit comments