@@ -1765,22 +1765,22 @@ where
1765
1765
mod redundant_pattern_match {
1766
1766
use super :: REDUNDANT_PATTERN_MATCHING ;
1767
1767
use clippy_utils:: diagnostics:: span_lint_and_then;
1768
- use clippy_utils:: higher;
1769
1768
use clippy_utils:: source:: snippet;
1770
1769
use clippy_utils:: sugg:: Sugg ;
1771
1770
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} ;
1773
1773
use if_chain:: if_chain;
1774
1774
use rustc_ast:: ast:: LitKind ;
1775
1775
use rustc_data_structures:: fx:: FxHashSet ;
1776
1776
use rustc_errors:: Applicability ;
1777
- use rustc_hir:: LangItem :: { OptionNone , OptionSome , PollPending , PollReady , ResultErr , ResultOk } ;
1777
+ use rustc_hir:: LangItem :: { OptionNone , PollPending } ;
1778
1778
use rustc_hir:: {
1779
1779
intravisit:: { walk_expr, Visitor } ,
1780
1780
Arm , Block , Expr , ExprKind , LangItem , MatchSource , Node , Pat , PatKind , QPath , UnOp ,
1781
1781
} ;
1782
1782
use rustc_lint:: LateContext ;
1783
- use rustc_middle:: ty:: { self , subst:: GenericArgKind , Ty } ;
1783
+ use rustc_middle:: ty:: { self , subst:: GenericArgKind , DefIdTree , Ty } ;
1784
1784
use rustc_span:: sym;
1785
1785
1786
1786
pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
@@ -1956,28 +1956,31 @@ mod redundant_pattern_match {
1956
1956
has_else : bool ,
1957
1957
) {
1958
1958
// also look inside refs
1959
- let mut kind = & let_pat. kind ;
1960
1959
// 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
+ } ;
1964
1964
let op_ty = cx. typeck_results ( ) . expr_ty ( let_expr) ;
1965
1965
// Determine which function should be used, and the type contained by the corresponding
1966
1966
// 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] , _) => {
1969
1969
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 ( ) {
1971
1974
( "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 ( ) {
1973
1976
( "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 ( ) {
1975
1978
( "is_some()" , op_ty)
1976
- } else if is_lang_ctor ( cx , path , PollReady ) {
1979
+ } else if Some ( id ) == lang_items . poll_ready_variant ( ) {
1977
1980
( "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 ) {
1979
1982
( "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 ) {
1981
1984
( "is_ipv6()" , op_ty)
1982
1985
} else {
1983
1986
return ;
@@ -2177,17 +2180,22 @@ mod redundant_pattern_match {
2177
2180
should_be_left : & ' a str ,
2178
2181
should_be_right : & ' a str ,
2179
2182
) -> 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
+ } ;
2191
2199
2192
2200
match body_node_pair {
2193
2201
( ExprKind :: Lit ( ref lit_left) , ExprKind :: Lit ( ref lit_right) ) => match ( & lit_left. node , & lit_right. node ) {
0 commit comments