@@ -1765,22 +1765,22 @@ where
17651765mod redundant_pattern_match {
17661766 use super :: REDUNDANT_PATTERN_MATCHING ;
17671767 use clippy_utils:: diagnostics:: span_lint_and_then;
1768- use clippy_utils:: higher;
17691768 use clippy_utils:: source:: snippet;
17701769 use clippy_utils:: sugg:: Sugg ;
17711770 use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type} ;
1772- use clippy_utils:: { is_lang_ctor, is_qpath_def_path, is_trait_method, paths} ;
1771+ use clippy_utils:: { higher, match_def_path} ;
1772+ use clippy_utils:: { is_lang_ctor, is_trait_method, paths} ;
17731773 use if_chain:: if_chain;
17741774 use rustc_ast:: ast:: LitKind ;
17751775 use rustc_data_structures:: fx:: FxHashSet ;
17761776 use rustc_errors:: Applicability ;
1777- use rustc_hir:: LangItem :: { OptionNone , OptionSome , PollPending , PollReady , ResultErr , ResultOk } ;
1777+ use rustc_hir:: LangItem :: { OptionNone , PollPending } ;
17781778 use rustc_hir:: {
17791779 intravisit:: { walk_expr, Visitor } ,
17801780 Arm , Block , Expr , ExprKind , LangItem , MatchSource , Node , Pat , PatKind , QPath , UnOp ,
17811781 } ;
17821782 use rustc_lint:: LateContext ;
1783- use rustc_middle:: ty:: { self , subst:: GenericArgKind , Ty } ;
1783+ use rustc_middle:: ty:: { self , subst:: GenericArgKind , DefIdTree , Ty } ;
17841784 use rustc_span:: sym;
17851785
17861786 pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
@@ -1956,28 +1956,31 @@ mod redundant_pattern_match {
19561956 has_else : bool ,
19571957 ) {
19581958 // also look inside refs
1959- let mut kind = & let_pat. kind ;
19601959 // if we have &None for example, peel it so we can detect "if let None = x"
1961- if let PatKind :: Ref ( inner, _mutability) = kind {
1962- kind = & inner. kind ;
1963- }
1960+ let check_pat = match let_pat. kind {
1961+ PatKind :: Ref ( inner, _mutability) => inner,
1962+ _ => let_pat,
1963+ } ;
19641964 let op_ty = cx. typeck_results ( ) . expr_ty ( let_expr) ;
19651965 // Determine which function should be used, and the type contained by the corresponding
19661966 // variant.
1967- let ( good_method, inner_ty) = match kind {
1968- PatKind :: TupleStruct ( ref path , [ sub_pat] , _) => {
1967+ let ( good_method, inner_ty) = match check_pat . kind {
1968+ PatKind :: TupleStruct ( ref qpath , [ sub_pat] , _) => {
19691969 if let PatKind :: Wild = sub_pat. kind {
1970- if is_lang_ctor ( cx, path, ResultOk ) {
1970+ let res = cx. typeck_results ( ) . qpath_res ( qpath, check_pat. hir_id ) ;
1971+ let Some ( id) = res. opt_def_id ( ) . and_then ( |ctor_id| cx. tcx . parent ( ctor_id) ) else { return } ;
1972+ let lang_items = cx. tcx . lang_items ( ) ;
1973+ if Some ( id) == lang_items. result_ok_variant ( ) {
19711974 ( "is_ok()" , try_get_generic_ty ( op_ty, 0 ) . unwrap_or ( op_ty) )
1972- } else if is_lang_ctor ( cx , path , ResultErr ) {
1975+ } else if Some ( id ) == lang_items . result_err_variant ( ) {
19731976 ( "is_err()" , try_get_generic_ty ( op_ty, 1 ) . unwrap_or ( op_ty) )
1974- } else if is_lang_ctor ( cx , path , OptionSome ) {
1977+ } else if Some ( id ) == lang_items . option_some_variant ( ) {
19751978 ( "is_some()" , op_ty)
1976- } else if is_lang_ctor ( cx , path , PollReady ) {
1979+ } else if Some ( id ) == lang_items . poll_ready_variant ( ) {
19771980 ( "is_ready()" , op_ty)
1978- } else if is_qpath_def_path ( cx, path , sub_pat . hir_id , & paths:: IPADDR_V4 ) {
1981+ } else if match_def_path ( cx, id , & paths:: IPADDR_V4 ) {
19791982 ( "is_ipv4()" , op_ty)
1980- } else if is_qpath_def_path ( cx, path , sub_pat . hir_id , & paths:: IPADDR_V6 ) {
1983+ } else if match_def_path ( cx, id , & paths:: IPADDR_V6 ) {
19811984 ( "is_ipv6()" , op_ty)
19821985 } else {
19831986 return ;
@@ -2177,17 +2180,22 @@ mod redundant_pattern_match {
21772180 should_be_left : & ' a str ,
21782181 should_be_right : & ' a str ,
21792182 ) -> Option < & ' a str > {
2180- let body_node_pair = if is_qpath_def_path ( cx, path_left, arms[ 0 ] . pat . hir_id , expected_left)
2181- && is_qpath_def_path ( cx, path_right, arms[ 1 ] . pat . hir_id , expected_right)
2182- {
2183- ( & ( * arms[ 0 ] . body ) . kind , & ( * arms[ 1 ] . body ) . kind )
2184- } else if is_qpath_def_path ( cx, path_right, arms[ 1 ] . pat . hir_id , expected_left)
2185- && is_qpath_def_path ( cx, path_left, arms[ 0 ] . pat . hir_id , expected_right)
2186- {
2187- ( & ( * arms[ 1 ] . body ) . kind , & ( * arms[ 0 ] . body ) . kind )
2188- } else {
2189- return None ;
2190- } ;
2183+ let left_id = cx
2184+ . typeck_results ( )
2185+ . qpath_res ( path_left, arms[ 0 ] . pat . hir_id )
2186+ . opt_def_id ( ) ?;
2187+ let right_id = cx
2188+ . typeck_results ( )
2189+ . qpath_res ( path_right, arms[ 1 ] . pat . hir_id )
2190+ . opt_def_id ( ) ?;
2191+ let body_node_pair =
2192+ if match_def_path ( cx, left_id, expected_left) && match_def_path ( cx, right_id, expected_right) {
2193+ ( & ( * arms[ 0 ] . body ) . kind , & ( * arms[ 1 ] . body ) . kind )
2194+ } else if match_def_path ( cx, right_id, expected_left) && match_def_path ( cx, right_id, expected_right) {
2195+ ( & ( * arms[ 1 ] . body ) . kind , & ( * arms[ 0 ] . body ) . kind )
2196+ } else {
2197+ return None ;
2198+ } ;
21912199
21922200 match body_node_pair {
21932201 ( ExprKind :: Lit ( ref lit_left) , ExprKind :: Lit ( ref lit_right) ) => match ( & lit_left. node , & lit_right. node ) {
0 commit comments