@@ -7,14 +7,15 @@ use clippy_utils::sugg::Sugg;
7
7
use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item, match_type, peel_mid_ty_refs} ;
8
8
use clippy_utils:: visitors:: LocalUsedVisitor ;
9
9
use 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,
11
11
path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns, remove_blocks, strip_pat_refs,
12
12
} ;
13
13
use clippy_utils:: { paths, search_same, SpanlessEq , SpanlessHash } ;
14
14
use if_chain:: if_chain;
15
15
use rustc_ast:: ast:: LitKind ;
16
16
use rustc_errors:: Applicability ;
17
17
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
18
+ use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
18
19
use rustc_hir:: {
19
20
self as hir, Arm , BindingAnnotation , Block , BorrowKind , Expr , ExprKind , Guard , HirId , Local , MatchSource ,
20
21
Mutability , Node , Pat , PatKind , PathSegment , QPath , RangeEnd , TyKind ,
@@ -1189,10 +1190,10 @@ fn check_match_ref_pats(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], e
1189
1190
1190
1191
fn check_match_as_ref ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
1191
1192
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 ] )
1196
1197
} else {
1197
1198
None
1198
1199
} ;
@@ -1575,20 +1576,20 @@ fn is_unit_expr(expr: &Expr<'_>) -> bool {
1575
1576
}
1576
1577
1577
1578
// 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 ) )
1580
1581
}
1581
1582
1582
1583
// 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 > {
1584
1585
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 ) ;
1587
1588
if let PatKind :: Binding ( rb, .., ident, _) = pats[ 0 ] . kind;
1588
1589
if rb == BindingAnnotation :: Ref || rb == BindingAnnotation :: RefMut ;
1589
1590
if let ExprKind :: Call ( e, args) = remove_blocks( arm. body) . kind;
1590
1591
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 ;
1592
1593
if let ExprKind :: Path ( QPath :: Resolved ( _, path2) ) = args[ 0 ] . kind;
1593
1594
if path2. segments. len( ) == 1 && ident. name == path2. segments[ 0 ] . ident. name;
1594
1595
then {
@@ -1700,10 +1701,11 @@ mod redundant_pattern_match {
1700
1701
use super :: REDUNDANT_PATTERN_MATCHING ;
1701
1702
use clippy_utils:: diagnostics:: span_lint_and_then;
1702
1703
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} ;
1704
1705
use if_chain:: if_chain;
1705
1706
use rustc_ast:: ast:: LitKind ;
1706
1707
use rustc_errors:: Applicability ;
1708
+ use rustc_hir:: LangItem :: { OptionNone , OptionSome , PollPending , PollReady , ResultErr , ResultOk } ;
1707
1709
use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , PatKind , QPath } ;
1708
1710
use rustc_lint:: LateContext ;
1709
1711
use rustc_span:: sym;
@@ -1735,13 +1737,13 @@ mod redundant_pattern_match {
1735
1737
let good_method = match kind {
1736
1738
PatKind :: TupleStruct ( ref path, patterns, _) if patterns. len ( ) == 1 => {
1737
1739
if let PatKind :: Wild = patterns[ 0 ] . kind {
1738
- if match_qpath ( path, & paths :: RESULT_OK ) {
1740
+ if is_lang_ctor ( cx , path, ResultOk ) {
1739
1741
"is_ok()"
1740
- } else if match_qpath ( path, & paths :: RESULT_ERR ) {
1742
+ } else if is_lang_ctor ( cx , path, ResultErr ) {
1741
1743
"is_err()"
1742
- } else if match_qpath ( path, & paths :: OPTION_SOME ) {
1744
+ } else if is_lang_ctor ( cx , path, OptionSome ) {
1743
1745
"is_some()"
1744
- } else if match_qpath ( path, & paths :: POLL_READY ) {
1746
+ } else if is_lang_ctor ( cx , path, PollReady ) {
1745
1747
"is_ready()"
1746
1748
} else if match_qpath ( path, & paths:: IPADDR_V4 ) {
1747
1749
"is_ipv4()"
@@ -1755,9 +1757,9 @@ mod redundant_pattern_match {
1755
1757
}
1756
1758
} ,
1757
1759
PatKind :: Path ( ref path) => {
1758
- if match_qpath ( path, & paths :: OPTION_NONE ) {
1760
+ if is_lang_ctor ( cx , path, OptionNone ) {
1759
1761
"is_none()"
1760
- } else if match_qpath ( path, & paths :: POLL_PENDING ) {
1762
+ } else if is_lang_ctor ( cx , path, PollPending ) {
1761
1763
"is_pending()"
1762
1764
} else {
1763
1765
return ;
0 commit comments