@@ -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 ,
@@ -1189,10 +1190,10 @@ fn check_match_ref_pats(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], e
11891190
11901191fn check_match_as_ref ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
11911192 if arms. len ( ) == 2 && arms[ 0 ] . guard . is_none ( ) && arms[ 1 ] . guard . is_none ( ) {
1192- let arm_ref: Option < BindingAnnotation > = if is_none_arm ( & arms[ 0 ] ) {
1193- is_ref_some_arm ( & arms[ 1 ] )
1194- } else if is_none_arm ( & arms[ 1 ] ) {
1195- is_ref_some_arm ( & arms[ 0 ] )
1193+ let arm_ref: Option < BindingAnnotation > = if is_none_arm ( cx , & arms[ 0 ] ) {
1194+ is_ref_some_arm ( cx , & arms[ 1 ] )
1195+ } else if is_none_arm ( cx , & arms[ 1 ] ) {
1196+ is_ref_some_arm ( cx , & arms[ 0 ] )
11961197 } else {
11971198 None
11981199 } ;
@@ -1575,20 +1576,20 @@ fn is_unit_expr(expr: &Expr<'_>) -> bool {
15751576}
15761577
15771578// Checks if arm has the form `None => None`
1578- fn is_none_arm ( arm : & Arm < ' _ > ) -> bool {
1579- matches ! ( arm. pat. kind, PatKind :: Path ( ref path ) if match_qpath ( path , & paths :: OPTION_NONE ) )
1579+ fn is_none_arm ( cx : & LateContext < ' _ > , arm : & Arm < ' _ > ) -> bool {
1580+ matches ! ( arm. pat. kind, PatKind :: Path ( ref qpath ) if is_lang_ctor ( cx , qpath , OptionNone ) )
15801581}
15811582
15821583// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
1583- fn is_ref_some_arm ( arm : & Arm < ' _ > ) -> Option < BindingAnnotation > {
1584+ fn is_ref_some_arm ( cx : & LateContext < ' _ > , arm : & Arm < ' _ > ) -> Option < BindingAnnotation > {
15841585 if_chain ! {
1585- if let PatKind :: TupleStruct ( ref path , pats, _) = arm. pat. kind;
1586- if pats . len ( ) == 1 && match_qpath ( path , & paths :: OPTION_SOME ) ;
1586+ if let PatKind :: TupleStruct ( ref qpath , pats, _) = arm. pat. kind;
1587+ if is_lang_ctor ( cx , qpath , OptionSome ) ;
15871588 if let PatKind :: Binding ( rb, .., ident, _) = pats[ 0 ] . kind;
15881589 if rb == BindingAnnotation :: Ref || rb == BindingAnnotation :: RefMut ;
15891590 if let ExprKind :: Call ( e, args) = remove_blocks( arm. body) . kind;
15901591 if let ExprKind :: Path ( ref some_path) = e. kind;
1591- if match_qpath ( some_path, & paths :: OPTION_SOME ) && args. len( ) == 1 ;
1592+ if is_lang_ctor ( cx , some_path, OptionSome ) && args. len( ) == 1 ;
15921593 if let ExprKind :: Path ( QPath :: Resolved ( _, path2) ) = args[ 0 ] . kind;
15931594 if path2. segments. len( ) == 1 && ident. name == path2. segments[ 0 ] . ident. name;
15941595 then {
@@ -1700,10 +1701,11 @@ mod redundant_pattern_match {
17001701 use super :: REDUNDANT_PATTERN_MATCHING ;
17011702 use clippy_utils:: diagnostics:: span_lint_and_then;
17021703 use clippy_utils:: source:: snippet;
1703- use clippy_utils:: { is_trait_method, match_qpath, paths} ;
1704+ use clippy_utils:: { is_lang_ctor , is_trait_method, match_qpath, paths} ;
17041705 use if_chain:: if_chain;
17051706 use rustc_ast:: ast:: LitKind ;
17061707 use rustc_errors:: Applicability ;
1708+ use rustc_hir:: LangItem :: { OptionNone , OptionSome , PollPending , PollReady , ResultErr , ResultOk } ;
17071709 use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , PatKind , QPath } ;
17081710 use rustc_lint:: LateContext ;
17091711 use rustc_span:: sym;
@@ -1735,13 +1737,13 @@ mod redundant_pattern_match {
17351737 let good_method = match kind {
17361738 PatKind :: TupleStruct ( ref path, patterns, _) if patterns. len ( ) == 1 => {
17371739 if let PatKind :: Wild = patterns[ 0 ] . kind {
1738- if match_qpath ( path, & paths :: RESULT_OK ) {
1740+ if is_lang_ctor ( cx , path, ResultOk ) {
17391741 "is_ok()"
1740- } else if match_qpath ( path, & paths :: RESULT_ERR ) {
1742+ } else if is_lang_ctor ( cx , path, ResultErr ) {
17411743 "is_err()"
1742- } else if match_qpath ( path, & paths :: OPTION_SOME ) {
1744+ } else if is_lang_ctor ( cx , path, OptionSome ) {
17431745 "is_some()"
1744- } else if match_qpath ( path, & paths :: POLL_READY ) {
1746+ } else if is_lang_ctor ( cx , path, PollReady ) {
17451747 "is_ready()"
17461748 } else if match_qpath ( path, & paths:: IPADDR_V4 ) {
17471749 "is_ipv4()"
@@ -1755,9 +1757,9 @@ mod redundant_pattern_match {
17551757 }
17561758 } ,
17571759 PatKind :: Path ( ref path) => {
1758- if match_qpath ( path, & paths :: OPTION_NONE ) {
1760+ if is_lang_ctor ( cx , path, OptionNone ) {
17591761 "is_none()"
1760- } else if match_qpath ( path, & paths :: POLL_PENDING ) {
1762+ } else if is_lang_ctor ( cx , path, PollPending ) {
17611763 "is_pending()"
17621764 } else {
17631765 return ;
0 commit comments