@@ -19,6 +19,7 @@ use syntax::ast;
19
19
use syntax:: ast:: { Crate as AstCrate , ItemKind , LitKind , Name , Expr as AstExpr } ;
20
20
use syntax:: ptr:: P ;
21
21
use syntax:: visit:: FnKind ;
22
+ use std:: borrow:: Cow ;
22
23
23
24
declare_clippy_lint ! {
24
25
/// **What it does:** Checks for various things we like to keep tidy in clippy.
@@ -409,44 +410,41 @@ impl EarlyLintPass for CollapsibleCalls {
409
410
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & AstExpr ) {
410
411
use ast:: { StmtKind , ExprKind } ;
411
412
412
- span_lint_and_help ( cx, COLLAPSIBLE_SPAN_LINT_CALLS , expr. span , "lint message" , "help message" ) ;
413
-
414
- // if_chain! {
415
- // if let ExprKind::Call(ref func, ref and_then_args) = expr.kind;
416
- // if let ExprKind::Path(None, ref path) = func.kind;
417
- // if match_path_ast(path, &["span_lint_and_then"]);
418
- // if and_then_args.len() == 5;
419
- // if let ExprKind::Closure(_, _, _, _, block, _) = &and_then_args[4].kind;
420
- // if let ExprKind::Block(block, _) = &block.kind;
421
- // let stmts = &block.stmts;
422
- // if stmts.len() == 1;
423
- // if let StmtKind::Expr(only_expr) = &stmts[0].kind;
424
- // if let ExprKind::MethodCall(ref ps, ref span_call_args) = &only_expr.kind;
425
- // let and_then_args = get_and_then_args(cx, and_then_args);
426
- // then {
427
- // match &*ps.ident.as_str() {
428
- // "span_suggestion" =>
429
- // suggest_span_suggestion(cx, expr, and_then_args, suggestion_args(cx, span_call_args)),
430
- // "span_help" =>
431
- // suggest_span_help(cx, expr, and_then_args, help_args(cx, span_call_args)),
432
- // "span_note" =>
433
- // suggest_span_note(cx, expr, and_then_args, note_args(cx, span_call_args)),
434
- // _ => (),
435
- // }
436
- // span_lint_and_help(cx, COLLAPSIBLE_SPAN_LINT_CALLS, expr.span, "lint message", "help message");
437
- // }
438
- // }
413
+ if_chain ! {
414
+ if let ExprKind :: Call ( ref func, ref and_then_args) = expr. kind;
415
+ if let ExprKind :: Path ( None , ref path) = func. kind;
416
+ if match_path_ast( path, & [ "span_lint_and_then" ] ) ;
417
+ if and_then_args. len( ) == 5 ;
418
+ if let ExprKind :: Closure ( _, _, _, _, block, _) = & and_then_args[ 4 ] . kind;
419
+ if let ExprKind :: Block ( block, _) = & block. kind;
420
+ let stmts = & block. stmts;
421
+ if stmts. len( ) == 1 ;
422
+ if let StmtKind :: Expr ( only_expr) = & stmts[ 0 ] . kind;
423
+ if let ExprKind :: MethodCall ( ref ps, ref span_call_args) = & only_expr. kind;
424
+ let and_then_args = get_and_then_args( cx, and_then_args) ;
425
+ then {
426
+ match & * ps. ident. as_str( ) {
427
+ "span_suggestion" =>
428
+ suggest_span_suggestion( cx, expr, and_then_args, suggestion_args( cx, span_call_args) ) ,
429
+ "span_help" =>
430
+ suggest_span_help( cx, expr, and_then_args, help_args( cx, span_call_args) ) ,
431
+ "span_note" =>
432
+ suggest_span_note( cx, expr, and_then_args, note_args( cx, span_call_args) ) ,
433
+ _ => ( ) ,
434
+ }
435
+ }
436
+ }
439
437
}
440
438
}
441
439
442
- struct AndThenArgs {
440
+ struct AndThenArgs < ' a > {
443
441
cx : Cow < ' a , str > ,
444
442
lint : Cow < ' a , str > ,
445
443
span : Cow < ' a , str > ,
446
444
msg : Cow < ' a , str > ,
447
445
}
448
446
449
- fn get_and_then_args ( cx : & EarlyContext < ' _ > , and_then_args : & Vec < P < AstExpr > > ) -> AndThenArgs {
447
+ fn get_and_then_args < ' a > ( cx : & EarlyContext < ' _ > , and_then_args : & Vec < P < AstExpr > > ) -> AndThenArgs < ' a > {
450
448
let cx_snippet = snippet ( cx, and_then_args[ 0 ] . span ) ;
451
449
let lint_snippet= snippet ( cx, and_then_args[ 1 ] . span ) ;
452
450
let span_snippet = snippet ( cx, and_then_args[ 2 ] . span ) ;
@@ -460,14 +458,14 @@ fn get_and_then_args(cx: &EarlyContext<'_>, and_then_args: &Vec<P<AstExpr>>) ->
460
458
}
461
459
}
462
460
463
- struct SuggestionArgs {
461
+ struct SuggestionArgs < ' a > {
464
462
span : Cow < ' a , str > ,
465
463
help : Cow < ' a , str > ,
466
464
sugg : Cow < ' a , str > ,
467
465
applicability : Cow < ' a , str > ,
468
466
}
469
467
470
- fn suggestion_args ( cx : & EarlyContext < ' _ > , span_call_args : & Vec < P < AstExpr > > ) -> SuggestionArgs {
468
+ fn suggestion_args < ' a > ( cx : & EarlyContext < ' _ > , span_call_args : & Vec < P < AstExpr > > ) -> SuggestionArgs < ' a > {
471
469
let span_snippet_of_span_call = snippet ( cx, span_call_args[ 0 ] . span ) ;
472
470
let help_snippet = snippet ( cx, span_call_args[ 1 ] . span ) ;
473
471
let sugg_snippet = snippet ( cx, span_call_args[ 2 ] . span ) ;
@@ -481,7 +479,7 @@ fn suggestion_args(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> S
481
479
}
482
480
}
483
481
484
- fn suggest_span_suggestion ( cx : & EarlyContext < ' _ > , expr : & AstExpr , and_then_args : AndThenArgs , suggestion_args : SuggestionArgs ) {
482
+ fn suggest_span_suggestion ( cx : & EarlyContext < ' _ > , expr : & AstExpr , and_then_args : AndThenArgs < ' _ > , suggestion_args : SuggestionArgs < ' _ > ) {
485
483
if and_then_args. span == suggestion_args. span {
486
484
println ! ( "suggestion true" ) ;
487
485
span_lint_and_sugg (
@@ -505,12 +503,12 @@ fn suggest_span_suggestion(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args:
505
503
}
506
504
}
507
505
508
- struct HelpArgs {
506
+ struct HelpArgs < ' a > {
509
507
span : Cow < ' a , str > ,
510
508
help : Cow < ' a , str > ,
511
509
}
512
510
513
- fn help_args ( cx : & EarlyContext < ' _ > , span_call_args : & Vec < P < AstExpr > > ) -> HelpArgs {
511
+ fn help_args < ' a > ( cx : & EarlyContext < ' _ > , span_call_args : & Vec < P < AstExpr > > ) -> HelpArgs < ' a > {
514
512
let span_snippet_of_span_call = snippet ( cx, span_call_args[ 0 ] . span ) ;
515
513
let help_snippet = snippet ( cx, span_call_args[ 1 ] . span ) ;
516
514
@@ -520,7 +518,7 @@ fn help_args(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> HelpArg
520
518
}
521
519
}
522
520
523
- fn suggest_span_help ( cx : & EarlyContext < ' _ > , expr : & AstExpr , and_then_args : AndThenArgs , help_args : HelpArgs ) {
521
+ fn suggest_span_help ( cx : & EarlyContext < ' _ > , expr : & AstExpr , and_then_args : AndThenArgs < ' _ > , help_args : HelpArgs < ' _ > ) {
524
522
if and_then_args. span == help_args. span {
525
523
span_lint_and_sugg (
526
524
cx,
@@ -541,12 +539,12 @@ fn suggest_span_help(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndTh
541
539
}
542
540
}
543
541
544
- struct NoteArgs {
542
+ struct NoteArgs < ' a > {
545
543
span : Cow < ' a , str > ,
546
544
note : Cow < ' a , str > ,
547
545
}
548
546
549
- fn note_args ( cx : & EarlyContext < ' _ > , span_call_args : & Vec < P < AstExpr > > ) -> NoteArgs {
547
+ fn note_args < ' a > ( cx : & EarlyContext < ' _ > , span_call_args : & Vec < P < AstExpr > > ) -> NoteArgs < ' a > {
550
548
let span_snippet_of_span_call = snippet ( cx, span_call_args[ 0 ] . span ) ;
551
549
let note_snippet = snippet ( cx, span_call_args[ 1 ] . span ) ;
552
550
@@ -556,7 +554,7 @@ fn note_args(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> NoteArg
556
554
}
557
555
}
558
556
559
- fn suggest_span_note ( cx : & EarlyContext < ' _ > , expr : & AstExpr , and_then_args : AndThenArgs , note_args : NoteArgs ) {
557
+ fn suggest_span_note ( cx : & EarlyContext < ' _ > , expr : & AstExpr , and_then_args : AndThenArgs < ' _ > , note_args : NoteArgs < ' _ > ) {
560
558
if and_then_args. span == note_args. span {
561
559
span_lint_and_sugg (
562
560
cx,
@@ -577,6 +575,6 @@ fn suggest_span_note(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndTh
577
575
}
578
576
}
579
577
580
- fn snippet ( cx : & EarlyContext < ' _ > , span : Span ) -> Cow < ' a , str > {
578
+ fn snippet < ' a > ( cx : & EarlyContext < ' _ > , span : Span ) -> Cow < ' a , str > {
581
579
other_snippet ( cx, span, "Should not be" )
582
580
}
0 commit comments