@@ -2,26 +2,25 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
2
2
use clippy_utils:: source:: snippet_with_applicability;
3
3
use clippy_utils:: { is_lang_ctor, peel_blocks} ;
4
4
use rustc_errors:: Applicability ;
5
- use rustc_hir:: { Arm , BindingAnnotation , Expr , ExprKind , LangItem , PatKind , QPath } ;
5
+ use rustc_hir:: { Arm , BindingAnnotation , ByRef , Expr , ExprKind , LangItem , Mutability , PatKind , QPath } ;
6
6
use rustc_lint:: LateContext ;
7
7
use rustc_middle:: ty;
8
8
9
9
use super :: MATCH_AS_REF ;
10
10
11
11
pub ( crate ) fn check ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
12
12
if arms. len ( ) == 2 && arms[ 0 ] . guard . is_none ( ) && arms[ 1 ] . guard . is_none ( ) {
13
- let arm_ref : Option < BindingAnnotation > = if is_none_arm ( cx, & arms[ 0 ] ) {
13
+ let arm_ref_mut = if is_none_arm ( cx, & arms[ 0 ] ) {
14
14
is_ref_some_arm ( cx, & arms[ 1 ] )
15
15
} else if is_none_arm ( cx, & arms[ 1 ] ) {
16
16
is_ref_some_arm ( cx, & arms[ 0 ] )
17
17
} else {
18
18
None
19
19
} ;
20
- if let Some ( rb) = arm_ref {
21
- let suggestion = if rb == BindingAnnotation :: Ref {
22
- "as_ref"
23
- } else {
24
- "as_mut"
20
+ if let Some ( rb) = arm_ref_mut {
21
+ let suggestion = match rb {
22
+ Mutability :: Not => "as_ref" ,
23
+ Mutability :: Mut => "as_mut" ,
25
24
} ;
26
25
27
26
let output_ty = cx. typeck_results ( ) . expr_ty ( expr) ;
@@ -66,19 +65,18 @@ fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
66
65
}
67
66
68
67
// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
69
- fn is_ref_some_arm ( cx : & LateContext < ' _ > , arm : & Arm < ' _ > ) -> Option < BindingAnnotation > {
68
+ fn is_ref_some_arm ( cx : & LateContext < ' _ > , arm : & Arm < ' _ > ) -> Option < Mutability > {
70
69
if_chain ! {
71
70
if let PatKind :: TupleStruct ( ref qpath, [ first_pat, ..] , _) = arm. pat. kind;
72
71
if is_lang_ctor( cx, qpath, LangItem :: OptionSome ) ;
73
- if let PatKind :: Binding ( rb, .., ident, _) = first_pat. kind;
74
- if rb == BindingAnnotation :: Ref || rb == BindingAnnotation :: RefMut ;
72
+ if let PatKind :: Binding ( BindingAnnotation ( ByRef :: Yes , mutabl) , .., ident, _) = first_pat. kind;
75
73
if let ExprKind :: Call ( e, [ arg] ) = peel_blocks( arm. body) . kind;
76
74
if let ExprKind :: Path ( ref some_path) = e. kind;
77
75
if is_lang_ctor( cx, some_path, LangItem :: OptionSome ) ;
78
76
if let ExprKind :: Path ( QPath :: Resolved ( _, path2) ) = arg. kind;
79
77
if path2. segments. len( ) == 1 && ident. name == path2. segments[ 0 ] . ident. name;
80
78
then {
81
- return Some ( rb )
79
+ return Some ( mutabl )
82
80
}
83
81
}
84
82
None
0 commit comments