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