@@ -7,14 +7,15 @@ use clippy_utils::sugg::Sugg;
77use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item, match_type, peel_mid_ty_refs} ;
88use clippy_utils:: visitors:: LocalUsedVisitor ;
99use clippy_utils:: {
10- get_parent_expr, in_macro, is_allowed, is_expn_of, is_refutable , is_wild , match_qpath , meets_msrv, path_to_local,
10+ get_parent_expr, in_macro, is_allowed, is_expn_of, is_lang_ctor , is_refutable , is_wild , meets_msrv, path_to_local,
1111 path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns, remove_blocks, strip_pat_refs,
1212} ;
1313use clippy_utils:: { paths, search_same, SpanlessEq , SpanlessHash } ;
1414use if_chain:: if_chain;
1515use rustc_ast:: ast:: LitKind ;
1616use rustc_errors:: Applicability ;
1717use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
18+ use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
1819use rustc_hir:: {
1920 self as hir, Arm , BindingAnnotation , Block , BorrowKind , Expr , ExprKind , Guard , HirId , Local , MatchSource ,
2021 Mutability , Node , Pat , PatKind , PathSegment , QPath , RangeEnd , TyKind ,
@@ -1188,10 +1189,10 @@ fn check_match_ref_pats(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], e
11881189
11891190fn check_match_as_ref ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
11901191 if arms. len ( ) == 2 && arms[ 0 ] . guard . is_none ( ) && arms[ 1 ] . guard . is_none ( ) {
1191- let arm_ref: Option < BindingAnnotation > = if is_none_arm ( & arms[ 0 ] ) {
1192- is_ref_some_arm ( & arms[ 1 ] )
1193- } else if is_none_arm ( & arms[ 1 ] ) {
1194- is_ref_some_arm ( & arms[ 0 ] )
1192+ let arm_ref: Option < BindingAnnotation > = if is_none_arm ( cx , & arms[ 0 ] ) {
1193+ is_ref_some_arm ( cx , & arms[ 1 ] )
1194+ } else if is_none_arm ( cx , & arms[ 1 ] ) {
1195+ is_ref_some_arm ( cx , & arms[ 0 ] )
11951196 } else {
11961197 None
11971198 } ;
@@ -1574,20 +1575,20 @@ fn is_unit_expr(expr: &Expr<'_>) -> bool {
15741575}
15751576
15761577// Checks if arm has the form `None => None`
1577- fn is_none_arm ( arm : & Arm < ' _ > ) -> bool {
1578- matches ! ( arm. pat. kind, PatKind :: Path ( ref path ) if match_qpath ( path , & paths :: OPTION_NONE ) )
1578+ fn is_none_arm ( cx : & LateContext < ' _ > , arm : & Arm < ' _ > ) -> bool {
1579+ matches ! ( arm. pat. kind, PatKind :: Path ( ref qpath ) if is_lang_ctor ( cx , qpath , OptionNone ) )
15791580}
15801581
15811582// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
1582- fn is_ref_some_arm ( arm : & Arm < ' _ > ) -> Option < BindingAnnotation > {
1583+ fn is_ref_some_arm ( cx : & LateContext < ' _ > , arm : & Arm < ' _ > ) -> Option < BindingAnnotation > {
15831584 if_chain ! {
1584- if let PatKind :: TupleStruct ( ref path , pats, _) = arm. pat. kind;
1585- if pats . len ( ) == 1 && match_qpath ( path , & paths :: OPTION_SOME ) ;
1585+ if let PatKind :: TupleStruct ( ref qpath , pats, _) = arm. pat. kind;
1586+ if is_lang_ctor ( cx , qpath , OptionSome ) ;
15861587 if let PatKind :: Binding ( rb, .., ident, _) = pats[ 0 ] . kind;
15871588 if rb == BindingAnnotation :: Ref || rb == BindingAnnotation :: RefMut ;
15881589 if let ExprKind :: Call ( e, args) = remove_blocks( arm. body) . kind;
15891590 if let ExprKind :: Path ( ref some_path) = e. kind;
1590- if match_qpath ( some_path, & paths :: OPTION_SOME ) && args. len( ) == 1 ;
1591+ if is_lang_ctor ( cx , some_path, OptionSome ) && args. len( ) == 1 ;
15911592 if let ExprKind :: Path ( QPath :: Resolved ( _, path2) ) = args[ 0 ] . kind;
15921593 if path2. segments. len( ) == 1 && ident. name == path2. segments[ 0 ] . ident. name;
15931594 then {
@@ -1699,10 +1700,11 @@ mod redundant_pattern_match {
16991700 use super :: REDUNDANT_PATTERN_MATCHING ;
17001701 use clippy_utils:: diagnostics:: span_lint_and_then;
17011702 use clippy_utils:: source:: snippet;
1702- use clippy_utils:: { is_trait_method, match_qpath, paths} ;
1703+ use clippy_utils:: { is_lang_ctor , is_trait_method, match_qpath, paths} ;
17031704 use if_chain:: if_chain;
17041705 use rustc_ast:: ast:: LitKind ;
17051706 use rustc_errors:: Applicability ;
1707+ use rustc_hir:: LangItem :: { OptionNone , OptionSome , PollPending , PollReady , ResultErr , ResultOk } ;
17061708 use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , PatKind , QPath } ;
17071709 use rustc_lint:: LateContext ;
17081710 use rustc_span:: sym;
@@ -1734,13 +1736,13 @@ mod redundant_pattern_match {
17341736 let good_method = match kind {
17351737 PatKind :: TupleStruct ( ref path, patterns, _) if patterns. len ( ) == 1 => {
17361738 if let PatKind :: Wild = patterns[ 0 ] . kind {
1737- if match_qpath ( path, & paths :: RESULT_OK ) {
1739+ if is_lang_ctor ( cx , path, ResultOk ) {
17381740 "is_ok()"
1739- } else if match_qpath ( path, & paths :: RESULT_ERR ) {
1741+ } else if is_lang_ctor ( cx , path, ResultErr ) {
17401742 "is_err()"
1741- } else if match_qpath ( path, & paths :: OPTION_SOME ) {
1743+ } else if is_lang_ctor ( cx , path, OptionSome ) {
17421744 "is_some()"
1743- } else if match_qpath ( path, & paths :: POLL_READY ) {
1745+ } else if is_lang_ctor ( cx , path, PollReady ) {
17441746 "is_ready()"
17451747 } else if match_qpath ( path, & paths:: IPADDR_V4 ) {
17461748 "is_ipv4()"
@@ -1754,9 +1756,9 @@ mod redundant_pattern_match {
17541756 }
17551757 } ,
17561758 PatKind :: Path ( ref path) => {
1757- if match_qpath ( path, & paths :: OPTION_NONE ) {
1759+ if is_lang_ctor ( cx , path, OptionNone ) {
17581760 "is_none()"
1759- } else if match_qpath ( path, & paths :: POLL_PENDING ) {
1761+ } else if is_lang_ctor ( cx , path, PollPending ) {
17601762 "is_pending()"
17611763 } else {
17621764 return ;
0 commit comments