Skip to content

Commit 41fda21

Browse files
committed
Register lint, update lint logic.
1 parent 05cd16a commit 41fda21

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
997997
store.register_late_pass(|| box let_underscore::LetUnderscore);
998998
store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
999999
store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports);
1000+
store.register_early_pass(|| box utils::internal_lints::CollapsibleCalls);
10001001

10011002
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10021003
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1098,6 +1099,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10981099
LintId::of(&utils::internal_lints::OUTER_EXPN_EXPN_DATA),
10991100
LintId::of(&utils::internal_lints::PRODUCE_ICE),
11001101
LintId::of(&utils::internal_lints::DEFAULT_LINT),
1102+
LintId::of(&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS),
11011103
]);
11021104

11031105
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
@@ -19,6 +19,7 @@ use syntax::ast;
1919
use syntax::ast::{Crate as AstCrate, ItemKind, LitKind, Name, Expr as AstExpr};
2020
use syntax::ptr::P;
2121
use syntax::visit::FnKind;
22+
use std::borrow::Cow;
2223

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

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

442-
struct AndThenArgs {
440+
struct AndThenArgs<'a> {
443441
cx: Cow<'a, str>,
444442
lint: Cow<'a, str>,
445443
span: Cow<'a, str>,
446444
msg: Cow<'a, str>,
447445
}
448446

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

463-
struct SuggestionArgs {
461+
struct SuggestionArgs<'a> {
464462
span: Cow<'a, str>,
465463
help: Cow<'a, str>,
466464
sugg: Cow<'a, str>,
467465
applicability: Cow<'a, str>,
468466
}
469467

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

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

508-
struct HelpArgs {
506+
struct HelpArgs<'a> {
509507
span: Cow<'a, str>,
510508
help: Cow<'a, str>,
511509
}
512510

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

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

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

544-
struct NoteArgs {
542+
struct NoteArgs<'a> {
545543
span: Cow<'a, str>,
546544
note: Cow<'a, str>,
547545
}
548546

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

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

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

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

0 commit comments

Comments
 (0)