@@ -8,7 +8,7 @@ use std::cmp::Ordering;
8
8
use syntax:: ast:: LitKind ;
9
9
use syntax:: codemap:: Span ;
10
10
use utils:: { COW_PATH , OPTION_PATH , RESULT_PATH } ;
11
- use utils:: { match_type, snippet, span_lint , span_note_and_lint, span_lint_and_then, in_external_macro, expr_block} ;
11
+ use utils:: { match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block} ;
12
12
13
13
/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice.
14
14
///
@@ -309,18 +309,26 @@ fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: Match
309
309
if has_only_ref_pats ( arms) {
310
310
if let ExprAddrOf ( Mutability :: MutImmutable , ref inner) = ex. node {
311
311
let template = match_template ( cx, expr. span , source, "" , inner) ;
312
- span_lint ( cx,
313
- MATCH_REF_PATS ,
314
- expr. span ,
315
- & format ! ( "you don't need to add `&` to both the expression and the patterns: use `{}`" ,
316
- template) ) ;
312
+ span_lint_and_then ( cx,
313
+ MATCH_REF_PATS ,
314
+ expr. span ,
315
+ "you don't need to add `&` to both the expression and the patterns" ,
316
+ |db| {
317
+ db. span_suggestion ( expr. span ,
318
+ "try" ,
319
+ template) ;
320
+ } ) ;
317
321
} else {
318
322
let template = match_template ( cx, expr. span , source, "*" , ex) ;
319
- span_lint ( cx,
320
- MATCH_REF_PATS ,
321
- expr. span ,
322
- & format ! ( "instead of prefixing all patterns with `&`, you can dereference the expression: `{}`" ,
323
- template) ) ;
323
+ span_lint_and_then ( cx,
324
+ MATCH_REF_PATS ,
325
+ expr. span ,
326
+ "you don't need to add `&` to all patterns" ,
327
+ |db| {
328
+ db. span_suggestion ( expr. span ,
329
+ "instead of prefixing all patterns with `&`, you can dereference the expression" ,
330
+ template) ;
331
+ } ) ;
324
332
}
325
333
}
326
334
}
@@ -435,10 +443,11 @@ fn has_only_ref_pats(arms: &[Arm]) -> bool {
435
443
fn match_template ( cx : & LateContext , span : Span , source : MatchSource , op : & str , expr : & Expr ) -> String {
436
444
let expr_snippet = snippet ( cx, expr. span , ".." ) ;
437
445
match source {
438
- MatchSource :: Normal => format ! ( "match {}{} {{ ... " , op, expr_snippet) ,
439
- MatchSource :: IfLetDesugar { .. } => format ! ( "if let ... = {}{} {{" , op, expr_snippet) ,
440
- MatchSource :: WhileLetDesugar => format ! ( "while let ... = {}{} {{" , op, expr_snippet) ,
446
+ MatchSource :: Normal => format ! ( "match {}{} {{ .. }} " , op, expr_snippet) ,
447
+ MatchSource :: IfLetDesugar { .. } => format ! ( "if let .. = {}{} {{ .. }} " , op, expr_snippet) ,
448
+ MatchSource :: WhileLetDesugar => format ! ( "while let .. = {}{} {{ .. }} " , op, expr_snippet) ,
441
449
MatchSource :: ForLoopDesugar => cx. sess ( ) . span_bug ( span, "for loop desugared to match with &-patterns!" ) ,
450
+ MatchSource :: TryDesugar => cx. sess ( ) . span_bug ( span, "`?` operator desugared to match with &-patterns!" )
442
451
}
443
452
}
444
453
0 commit comments