@@ -11,8 +11,8 @@ use syntax::ast::LitKind;
11
11
use syntax:: ast:: NodeId ;
12
12
use syntax:: codemap:: Span ;
13
13
use utils:: paths;
14
- use utils:: { expr_block, in_external_macro, is_allowed, is_expn_of, match_qpath, match_type, remove_blocks ,
15
- snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty} ;
14
+ use utils:: { expr_block, in_external_macro, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg ,
15
+ remove_blocks , snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty} ;
16
16
use utils:: sugg:: Sugg ;
17
17
18
18
/// **What it does:** Checks for matches with a single arm where an `if let`
@@ -195,8 +195,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass {
195
195
check_wild_err_arm ( cx, ex, arms) ;
196
196
check_match_as_ref ( cx, ex, arms, expr) ;
197
197
}
198
- if let ExprMatch ( ref ex, ref arms, source ) = expr. node {
199
- check_match_ref_pats ( cx, ex, arms, source , expr) ;
198
+ if let ExprMatch ( ref ex, ref arms, _ ) = expr. node {
199
+ check_match_ref_pats ( cx, ex, arms, expr) ;
200
200
}
201
201
}
202
202
}
@@ -400,37 +400,34 @@ fn is_panic_block(block: &Block) -> bool {
400
400
}
401
401
}
402
402
403
- fn check_match_ref_pats ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , source : MatchSource , expr : & Expr ) {
403
+ fn check_match_ref_pats ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr ) {
404
404
if has_only_ref_pats ( arms) {
405
- if let ExprAddrOf ( Mutability :: MutImmutable , ref inner) = ex. node {
406
- span_lint_and_then (
407
- cx,
408
- MATCH_REF_PATS ,
409
- expr. span ,
405
+ let mut suggs = Vec :: new ( ) ;
406
+ let ( title, msg) = if let ExprAddrOf ( Mutability :: MutImmutable , ref inner) = ex. node {
407
+ suggs. push ( ( ex. span , Sugg :: hir ( cx, inner, ".." ) . to_string ( ) ) ) ;
408
+ (
410
409
"you don't need to add `&` to both the expression and the patterns" ,
411
- |db| {
412
- let inner = Sugg :: hir ( cx, inner, ".." ) ;
413
- let template = match_template ( expr. span , source, & inner) ;
414
- db. span_suggestion ( expr. span , "try" , template) ;
415
- } ,
416
- ) ;
410
+ "try" ,
411
+ )
417
412
} else {
418
- span_lint_and_then (
419
- cx,
420
- MATCH_REF_PATS ,
421
- expr. span ,
413
+ suggs. push ( ( ex. span , Sugg :: hir ( cx, ex, ".." ) . deref ( ) . to_string ( ) ) ) ;
414
+ (
422
415
"you don't need to add `&` to all patterns" ,
423
- |db| {
424
- let ex = Sugg :: hir ( cx, ex, ".." ) ;
425
- let template = match_template ( expr. span , source, & ex. deref ( ) ) ;
426
- db. span_suggestion (
427
- expr. span ,
428
- "instead of prefixing all patterns with `&`, you can dereference the expression" ,
429
- template,
430
- ) ;
431
- } ,
432
- ) ;
433
- }
416
+ "instead of prefixing all patterns with `&`, you can dereference the expression" ,
417
+ )
418
+ } ;
419
+
420
+ suggs. extend ( arms. iter ( ) . flat_map ( |a| & a. pats ) . filter_map ( |p| {
421
+ if let PatKind :: Ref ( ref refp, _) = p. node {
422
+ Some ( ( p. span , snippet ( cx, refp. span , ".." ) . to_string ( ) ) )
423
+ } else {
424
+ None
425
+ }
426
+ } ) ) ;
427
+
428
+ span_lint_and_then ( cx, MATCH_REF_PATS , expr. span , title, |db| {
429
+ multispan_sugg ( db, msg. to_owned ( ) , suggs) ;
430
+ } ) ;
434
431
}
435
432
}
436
433
@@ -615,16 +612,6 @@ fn has_only_ref_pats(arms: &[Arm]) -> bool {
615
612
mapped. map_or ( false , |v| v. iter ( ) . any ( |el| * el) )
616
613
}
617
614
618
- fn match_template ( span : Span , source : MatchSource , expr : & Sugg ) -> String {
619
- match source {
620
- MatchSource :: Normal => format ! ( "match {} {{ .. }}" , expr) ,
621
- MatchSource :: IfLetDesugar { .. } => format ! ( "if let .. = {} {{ .. }}" , expr) ,
622
- MatchSource :: WhileLetDesugar => format ! ( "while let .. = {} {{ .. }}" , expr) ,
623
- MatchSource :: ForLoopDesugar => span_bug ! ( span, "for loop desugared to match with &-patterns!" ) ,
624
- MatchSource :: TryDesugar => span_bug ! ( span, "`?` operator desugared to match with &-patterns!" ) ,
625
- }
626
- }
627
-
628
615
pub fn overlapping < T > ( ranges : & [ SpannedRange < T > ] ) -> Option < ( & SpannedRange < T > , & SpannedRange < T > ) >
629
616
where
630
617
T : Copy + Ord ,
0 commit comments