@@ -148,7 +148,7 @@ declare_lint! {
148
148
/// **What it does:** Checks for match which is used to add a reference to an
149
149
/// `Option` value.
150
150
///
151
- /// **Why is this bad?** Using `as_ref()` instead is shorter.
151
+ /// **Why is this bad?** Using `as_ref()` or `as_mut()` instead is shorter.
152
152
///
153
153
/// **Known problems:** None.
154
154
///
@@ -163,7 +163,7 @@ declare_lint! {
163
163
declare_lint ! {
164
164
pub MATCH_AS_REF ,
165
165
Warn ,
166
- "a match on an Option value instead of using `as_ref()`"
166
+ "a match on an Option value instead of using `as_ref()` or `as_mut` "
167
167
}
168
168
169
169
#[ allow( missing_copy_implementations) ]
@@ -438,15 +438,22 @@ fn check_match_as_ref(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
438
438
if arms. len ( ) == 2 &&
439
439
arms[ 0 ] . pats . len ( ) == 1 && arms[ 0 ] . guard . is_none ( ) &&
440
440
arms[ 1 ] . pats . len ( ) == 1 && arms[ 1 ] . guard . is_none ( ) {
441
- if ( is_ref_some_arm ( & arms[ 0 ] ) && is_none_arm ( & arms[ 1 ] ) ) ||
442
- ( is_ref_some_arm ( & arms[ 1 ] ) && is_none_arm ( & arms[ 0 ] ) ) {
441
+ let arm_ref: Option < BindingAnnotation > = if is_none_arm ( & arms[ 0 ] ) {
442
+ is_ref_some_arm ( & arms[ 1 ] )
443
+ } else if is_none_arm ( & arms[ 1 ] ) {
444
+ is_ref_some_arm ( & arms[ 0 ] )
445
+ } else {
446
+ None
447
+ } ;
448
+ if let Some ( rb) = arm_ref {
449
+ let suggestion = if rb == BindingAnnotation :: Ref { "as_ref" } else { "as_mut" } ;
443
450
span_lint_and_sugg (
444
451
cx,
445
452
MATCH_AS_REF ,
446
453
expr. span ,
447
- "use as_ref () instead" ,
454
+ & format ! ( "use {} () instead" , suggestion ) ,
448
455
"try this" ,
449
- format ! ( "{}.as_ref ()" , snippet( cx, ex. span, "_" ) )
456
+ format ! ( "{}.{} ()" , snippet( cx, ex. span, "_" ) , suggestion )
450
457
)
451
458
}
452
459
}
@@ -574,7 +581,7 @@ fn is_none_arm(arm: &Arm) -> bool {
574
581
}
575
582
576
583
// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
577
- fn is_ref_some_arm ( arm : & Arm ) -> bool {
584
+ fn is_ref_some_arm ( arm : & Arm ) -> Option < BindingAnnotation > {
578
585
if_chain ! {
579
586
if let PatKind :: TupleStruct ( ref path, ref pats, _) = arm. pats[ 0 ] . node;
580
587
if pats. len( ) == 1 && match_qpath( path, & paths:: OPTION_SOME ) ;
@@ -585,12 +592,12 @@ fn is_ref_some_arm(arm: &Arm) -> bool {
585
592
if match_qpath( some_path, & paths:: OPTION_SOME ) && args. len( ) == 1 ;
586
593
if let ExprPath ( ref qpath) = args[ 0 ] . node;
587
594
if let & QPath :: Resolved ( _, ref path2) = qpath;
588
- if path2. segments. len( ) == 1 ;
595
+ if path2. segments. len( ) == 1 && ident . node == path2 . segments [ 0 ] . name ;
589
596
then {
590
- return ident . node == path2 . segments [ 0 ] . name
597
+ return Some ( rb )
591
598
}
592
599
}
593
- false
600
+ None
594
601
}
595
602
596
603
fn has_only_ref_pats ( arms : & [ Arm ] ) -> bool {
0 commit comments