@@ -1440,15 +1440,12 @@ where
14401440
14411441mod redundant_pattern_match {
14421442 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} ;
14441444 use if_chain:: if_chain;
14451445 use rustc_ast:: ast:: LitKind ;
14461446 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 } ;
14481448 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 ;
14521449
14531450 pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
14541451 if let ExprKind :: Match ( op, arms, ref match_source) = & expr. kind {
@@ -1468,37 +1465,24 @@ mod redundant_pattern_match {
14681465 arms : & [ Arm < ' _ > ] ,
14691466 keyword : & ' static str ,
14701467 ) {
1471- fn find_suggestion ( cx : & LateContext < ' _ > , hir_id : HirId , path : & QPath < ' _ > ) -> Option < & ' static str > {
1472- if match_qpath ( path, & paths:: RESULT_OK ) {
1473- return Some ( "is_ok()" ) ;
1474- }
1475- if match_qpath ( path, & paths:: RESULT_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 ;
14881468 let good_method = match arms[ 0 ] . pat . kind {
14891469 PatKind :: TupleStruct ( ref path, ref patterns, _) if patterns. len ( ) == 1 => {
14901470 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+ }
14921480 } else {
1493- None
1481+ return ;
14941482 }
14951483 } ,
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 ,
15021486 } ;
15031487
15041488 // check that `while_let_on_iterator` lint does not trigger
@@ -1547,7 +1531,6 @@ mod redundant_pattern_match {
15471531 if arms. len ( ) == 2 {
15481532 let node_pair = ( & arms[ 0 ] . pat . kind , & arms[ 1 ] . pat . kind ) ;
15491533
1550- let hir_id = expr. hir_id ;
15511534 let found_good_method = match node_pair {
15521535 (
15531536 PatKind :: TupleStruct ( ref path_left, ref patterns_left, _) ,
@@ -1562,8 +1545,6 @@ mod redundant_pattern_match {
15621545 & paths:: RESULT_ERR ,
15631546 "is_ok()" ,
15641547 "is_err()" ,
1565- || true ,
1566- || true ,
15671548 )
15681549 } else {
15691550 None
@@ -1582,8 +1563,6 @@ mod redundant_pattern_match {
15821563 & paths:: OPTION_NONE ,
15831564 "is_some()" ,
15841565 "is_none()" ,
1585- || can_suggest ( cx, hir_id, sym ! ( option_type) , "is_some" ) ,
1586- || can_suggest ( cx, hir_id, sym ! ( option_type) , "is_none" ) ,
15871566 )
15881567 } else {
15891568 None
@@ -1616,7 +1595,6 @@ mod redundant_pattern_match {
16161595 }
16171596 }
16181597
1619- #[ allow( clippy:: too_many_arguments) ]
16201598 fn find_good_method_for_match < ' a > (
16211599 arms : & [ Arm < ' _ > ] ,
16221600 path_left : & QPath < ' _ > ,
@@ -1625,8 +1603,6 @@ mod redundant_pattern_match {
16251603 expected_right : & [ & str ] ,
16261604 should_be_left : & ' a str ,
16271605 should_be_right : & ' a str ,
1628- can_suggest_left : impl Fn ( ) -> bool ,
1629- can_suggest_right : impl Fn ( ) -> bool ,
16301606 ) -> Option < & ' a str > {
16311607 let body_node_pair = if match_qpath ( path_left, expected_left) && match_qpath ( path_right, expected_right) {
16321608 ( & ( * arms[ 0 ] . body ) . kind , & ( * arms[ 1 ] . body ) . kind )
@@ -1638,35 +1614,13 @@ mod redundant_pattern_match {
16381614
16391615 match body_node_pair {
16401616 ( 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) ,
16431619 _ => None ,
16441620 } ,
16451621 _ => None ,
16461622 }
16471623 }
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- }
16701624}
16711625
16721626#[ test]
0 commit comments