Skip to content

Commit 83095ef

Browse files
committed
Register lint, update lint logic.
1 parent fbd2e6f commit 83095ef

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10211021
store.register_late_pass(|| box wildcard_imports::WildcardImports);
10221022
store.register_early_pass(|| box macro_use::MacroUseImports);
10231023
store.register_late_pass(|| box verbose_file_reads::VerboseFileReads);
1024+
store.register_early_pass(|| box utils::internal_lints::CollapsibleCalls);
10241025

10251026
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10261027
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1129,6 +1130,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11291130
LintId::of(&utils::internal_lints::LINT_WITHOUT_LINT_PASS),
11301131
LintId::of(&utils::internal_lints::OUTER_EXPN_EXPN_DATA),
11311132
LintId::of(&utils::internal_lints::PRODUCE_ICE),
1133+
LintId::of(&utils::internal_lints::DEFAULT_LINT),
1134+
LintId::of(&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS),
11321135
]);
11331136

11341137
store.register_group(true, "clippy::all", Some("clippy"), vec![

clippy_lints/src/utils/internal_lints.rs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use syntax::ast;
2020
use syntax::ast::{Crate as AstCrate, ItemKind, LitKind, Name, Expr as AstExpr};
2121
use syntax::ptr::P;
2222
use syntax::visit::FnKind;
23+
use std::borrow::Cow;
2324

2425
declare_clippy_lint! {
2526
/// **What it does:** Checks for various things we like to keep tidy in clippy.
@@ -408,44 +409,41 @@ impl EarlyLintPass for CollapsibleCalls {
408409
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &AstExpr) {
409410
use ast::{StmtKind, ExprKind};
410411

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+
}
438436
}
439437
}
440438

441-
struct AndThenArgs {
439+
struct AndThenArgs<'a> {
442440
cx: Cow<'a, str>,
443441
lint: Cow<'a, str>,
444442
span: Cow<'a, str>,
445443
msg: Cow<'a, str>,
446444
}
447445

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>{
449447
let cx_snippet = snippet(cx, and_then_args[0].span);
450448
let lint_snippet= snippet(cx, and_then_args[1].span);
451449
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>>) ->
459457
}
460458
}
461459

462-
struct SuggestionArgs {
460+
struct SuggestionArgs<'a> {
463461
span: Cow<'a, str>,
464462
help: Cow<'a, str>,
465463
sugg: Cow<'a, str>,
466464
applicability: Cow<'a, str>,
467465
}
468466

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> {
470468
let span_snippet_of_span_call = snippet(cx, span_call_args[0].span);
471469
let help_snippet = snippet(cx, span_call_args[1].span);
472470
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
480478
}
481479
}
482480

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<'_>) {
484482
if and_then_args.span == suggestion_args.span {
485483
println!("suggestion true");
486484
span_lint_and_sugg (
@@ -504,12 +502,12 @@ fn suggest_span_suggestion(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args:
504502
}
505503
}
506504

507-
struct HelpArgs {
505+
struct HelpArgs<'a> {
508506
span: Cow<'a, str>,
509507
help: Cow<'a, str>,
510508
}
511509

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>{
513511
let span_snippet_of_span_call = snippet(cx, span_call_args[0].span);
514512
let help_snippet = snippet(cx, span_call_args[1].span);
515513

@@ -519,7 +517,7 @@ fn help_args(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> HelpArg
519517
}
520518
}
521519

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<'_>) {
523521
if and_then_args.span == help_args.span {
524522
span_lint_and_sugg(
525523
cx,
@@ -540,12 +538,12 @@ fn suggest_span_help(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndTh
540538
}
541539
}
542540

543-
struct NoteArgs {
541+
struct NoteArgs<'a> {
544542
span: Cow<'a, str>,
545543
note: Cow<'a, str>,
546544
}
547545

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> {
549547
let span_snippet_of_span_call = snippet(cx, span_call_args[0].span);
550548
let note_snippet = snippet(cx, span_call_args[1].span);
551549

@@ -555,7 +553,7 @@ fn note_args(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> NoteArg
555553
}
556554
}
557555

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<'_> ) {
559557
if and_then_args.span == note_args.span {
560558
span_lint_and_sugg(
561559
cx,
@@ -576,6 +574,6 @@ fn suggest_span_note(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndTh
576574
}
577575
}
578576

579-
fn snippet(cx: &EarlyContext<'_>, span: Span) -> Cow<'a, str> {
577+
fn snippet<'a>(cx: &EarlyContext<'_>, span: Span) -> Cow<'a, str> {
580578
other_snippet(cx, span, "Should not be")
581579
}

0 commit comments

Comments
 (0)