@@ -2,26 +2,25 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22use clippy_utils:: source:: snippet_with_applicability;
33use clippy_utils:: { is_lang_ctor, peel_blocks} ;
44use 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 } ;
66use rustc_lint:: LateContext ;
77use rustc_middle:: ty;
88
99use super :: MATCH_AS_REF ;
1010
1111pub ( crate ) fn check ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
1212 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 ] ) {
1414 is_ref_some_arm ( cx, & arms[ 1 ] )
1515 } else if is_none_arm ( cx, & arms[ 1 ] ) {
1616 is_ref_some_arm ( cx, & arms[ 0 ] )
1717 } else {
1818 None
1919 } ;
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" ,
2524 } ;
2625
2726 let output_ty = cx. typeck_results ( ) . expr_ty ( expr) ;
@@ -66,19 +65,18 @@ fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
6665}
6766
6867// 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 > {
7069 if_chain ! {
7170 if let PatKind :: TupleStruct ( ref qpath, [ first_pat, ..] , _) = arm. pat. kind;
7271 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;
7573 if let ExprKind :: Call ( e, [ arg] ) = peel_blocks( arm. body) . kind;
7674 if let ExprKind :: Path ( ref some_path) = e. kind;
7775 if is_lang_ctor( cx, some_path, LangItem :: OptionSome ) ;
7876 if let ExprKind :: Path ( QPath :: Resolved ( _, path2) ) = arg. kind;
7977 if path2. segments. len( ) == 1 && ident. name == path2. segments[ 0 ] . ident. name;
8078 then {
81- return Some ( rb )
79+ return Some ( mutabl )
8280 }
8381 }
8482 None
0 commit comments