|
| 1 | +use crate::utils::SpanlessEq; |
1 | 2 | use crate::utils::{
|
2 |
| - is_expn_of, match_def_path, match_type, method_calls, paths, span_lint, span_lint_and_help, span_lint_and_sugg, |
3 |
| - walk_ptrs_ty, snippet as other_snippet, match_path_ast |
| 3 | + is_expn_of, match_def_path, match_qpath, match_type, method_calls, paths, snippet as other_snippet, span_lint, |
| 4 | + span_lint_and_help, span_lint_and_sugg, walk_ptrs_ty, |
4 | 5 | };
|
5 | 6 | use if_chain::if_chain;
|
6 | 7 | use rustc::hir::map::Map;
|
7 |
| -use rustc_ast::ast; |
8 |
| -use rustc_ast::ast::{Crate as AstCrate, Expr as AstExpr, ItemKind, LitKind, Name, NodeId}; |
| 8 | +use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, Name, NodeId}; |
9 | 9 | use rustc_ast::visit::FnKind;
|
10 |
| -use rustc_ast::ptr::P; |
11 | 10 | use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
12 | 11 | use rustc_errors::Applicability;
|
13 | 12 | use rustc_hir as hir;
|
14 | 13 | use rustc_hir::def::{DefKind, Res};
|
15 | 14 | use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
16 |
| -use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Path, Ty, TyKind}; |
| 15 | +use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Path, StmtKind, Ty, TyKind}; |
17 | 16 | use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
|
18 | 17 | use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
|
19 | 18 | use rustc_span::source_map::{Span, Spanned};
|
20 | 19 | use rustc_span::symbol::SymbolStr;
|
21 | 20 |
|
22 |
| - |
23 | 21 | use std::borrow::Cow;
|
24 | 22 |
|
25 | 23 | declare_clippy_lint! {
|
@@ -176,7 +174,7 @@ impl EarlyLintPass for ClippyLintsInternal {
|
176 | 174 | CLIPPY_LINTS_INTERNAL,
|
177 | 175 | item.span,
|
178 | 176 | "this constant should be before the previous constant due to lexical \
|
179 |
| - ordering", |
| 177 | + ordering", |
180 | 178 | );
|
181 | 179 | }
|
182 | 180 | }
|
@@ -206,8 +204,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
|
206 | 204 | if let ExprKind::AddrOf(_, _, ref inner_exp) = expr.kind;
|
207 | 205 | if let ExprKind::Struct(_, ref fields, _) = inner_exp.kind;
|
208 | 206 | let field = fields.iter()
|
209 |
| - .find(|f| f.ident.as_str() == "desc") |
210 |
| - .expect("lints must have a description field"); |
| 207 | + .find(|f| f.ident.as_str() == "desc") |
| 208 | + .expect("lints must have a description field"); |
211 | 209 | if let ExprKind::Lit(Spanned {
|
212 | 210 | node: LitKind::Str(ref sym, _),
|
213 | 211 | ..
|
@@ -343,7 +341,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions {
|
343 | 341 | if let Some(sugg) = self.map.get(&*fn_name.as_str());
|
344 | 342 | let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));
|
345 | 343 | if match_type(cx, ty, &paths::EARLY_CONTEXT)
|
346 |
| - || match_type(cx, ty, &paths::LATE_CONTEXT); |
| 344 | + || match_type(cx, ty, &paths::LATE_CONTEXT); |
347 | 345 | then {
|
348 | 346 | span_lint_and_help(
|
349 | 347 | cx,
|
@@ -405,175 +403,162 @@ fn is_trigger_fn(fn_kind: FnKind<'_>) -> bool {
|
405 | 403 |
|
406 | 404 | declare_lint_pass!(CollapsibleCalls => [COLLAPSIBLE_SPAN_LINT_CALLS]);
|
407 | 405 |
|
408 |
| -impl EarlyLintPass for CollapsibleCalls { |
409 |
| - fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &AstExpr) { |
410 |
| - use ast::{StmtKind, ExprKind}; |
411 |
| - |
| 406 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls { |
| 407 | + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) { |
412 | 408 | if_chain! {
|
413 | 409 | 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"]); |
| 410 | + if let ExprKind::Path(ref path) = func.kind; |
| 411 | + if match_qpath(path, &["span_lint_and_then"]); |
416 | 412 | 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; |
| 413 | + if let ExprKind::Closure(_, _, body_id, _, _) = &and_then_args[4].kind; |
| 414 | + if let body = cx.tcx.hir().body(*body_id); |
| 415 | + if let ExprKind::Block(block, _) = &body.value.kind; |
419 | 416 | let stmts = &block.stmts;
|
420 | 417 | 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); |
| 418 | + // |
| 419 | + if let StmtKind::Semi(only_expr) = &stmts[0].kind; |
| 420 | + if let ExprKind::MethodCall(ref ps, _, ref span_call_args) = &only_expr.kind; |
| 421 | + let and_then_snippets = get_and_then_snippets(cx, and_then_args); |
| 422 | + let mut sle = SpanlessEq::new(cx).ignore_fn(); |
424 | 423 | then {
|
425 | 424 | 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)), |
| 425 | + "span_suggestion" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => { |
| 426 | + suggest_suggestion(cx, expr, and_then_snippets, span_suggestion_snippets(cx, span_call_args)); |
| 427 | + }, |
| 428 | + "span_suggestion" => { |
| 429 | + dbg!(&span_call_args[0]); |
| 430 | + dbg!(&span_call_args[1]); |
| 431 | + dbg!(&span_call_args[2]); |
| 432 | + } |
| 433 | + "span_help" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => { |
| 434 | + let help_snippet = snippet(cx, span_call_args[2].span); |
| 435 | + suggest_help(cx, expr, and_then_snippets, help_snippet); |
| 436 | + }, |
| 437 | + "span_note" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => { |
| 438 | + let note_snippet = snippet(cx, span_call_args[2].span); |
| 439 | + suggest_note(cx, expr, and_then_snippets, note_snippet); |
| 440 | + }, |
| 441 | + "help" => { |
| 442 | + let help_snippet = snippet(cx, span_call_args[1].span); |
| 443 | + suggest_help(cx, expr, and_then_snippets, help_snippet); |
| 444 | + } |
| 445 | + "note" => { |
| 446 | + let note_snippet = snippet(cx, span_call_args[1].span); |
| 447 | + suggest_note(cx, expr, and_then_snippets, note_snippet); |
| 448 | + } |
432 | 449 | _ => (),
|
433 | 450 | }
|
434 | 451 | }
|
435 | 452 | }
|
436 | 453 | }
|
437 | 454 | }
|
438 | 455 |
|
439 |
| -struct AndThenArgs<'a> { |
| 456 | +struct AndThenSnippets<'a> { |
440 | 457 | cx: Cow<'a, str>,
|
441 | 458 | lint: Cow<'a, str>,
|
442 | 459 | span: Cow<'a, str>,
|
443 | 460 | msg: Cow<'a, str>,
|
444 | 461 | }
|
445 | 462 |
|
446 |
| -fn get_and_then_args<'a>(cx: &EarlyContext<'_>, and_then_args: &Vec<P<AstExpr>>) -> AndThenArgs<'a>{ |
447 |
| - let cx_snippet = snippet(cx, and_then_args[0].span); |
448 |
| - let lint_snippet= snippet(cx, and_then_args[1].span); |
449 |
| - let span_snippet = snippet(cx, and_then_args[2].span); |
450 |
| - let msg_snippet= snippet(cx, and_then_args[3].span); |
| 463 | +fn get_and_then_snippets<'a, 'hir>( |
| 464 | + cx: &LateContext<'_, '_>, |
| 465 | + and_then_snippets: &'hir [Expr<'hir>], |
| 466 | +) -> AndThenSnippets<'a> { |
| 467 | + let cx_snippet = snippet(cx, and_then_snippets[0].span); |
| 468 | + let lint_snippet = snippet(cx, and_then_snippets[1].span); |
| 469 | + let span_snippet = snippet(cx, and_then_snippets[2].span); |
| 470 | + let msg_snippet = snippet(cx, and_then_snippets[3].span); |
451 | 471 |
|
452 |
| - AndThenArgs { |
| 472 | + AndThenSnippets { |
453 | 473 | cx: cx_snippet,
|
454 | 474 | lint: lint_snippet,
|
455 | 475 | span: span_snippet,
|
456 | 476 | msg: msg_snippet,
|
457 | 477 | }
|
458 | 478 | }
|
459 | 479 |
|
460 |
| -struct SuggestionArgs<'a> { |
461 |
| - span: Cow<'a, str>, |
| 480 | +struct SpanSuggestionSnippets<'a> { |
462 | 481 | help: Cow<'a, str>,
|
463 | 482 | sugg: Cow<'a, str>,
|
464 | 483 | applicability: Cow<'a, str>,
|
465 | 484 | }
|
466 | 485 |
|
467 |
| -fn suggestion_args<'a>(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> SuggestionArgs<'a> { |
468 |
| - let span_snippet_of_span_call = snippet(cx, span_call_args[0].span); |
469 |
| - let help_snippet = snippet(cx, span_call_args[1].span); |
470 |
| - let sugg_snippet = snippet(cx, span_call_args[2].span); |
471 |
| - let applicability_snippet = snippet(cx, span_call_args[3].span); |
| 486 | +fn span_suggestion_snippets<'a, 'hir>( |
| 487 | + cx: &LateContext<'_, '_>, |
| 488 | + span_call_args: &'hir [Expr<'hir>], |
| 489 | +) -> SpanSuggestionSnippets<'a> { |
| 490 | + let help_snippet = snippet(cx, span_call_args[2].span); |
| 491 | + let sugg_snippet = snippet(cx, span_call_args[3].span); |
| 492 | + let applicability_snippet = snippet(cx, span_call_args[4].span); |
472 | 493 |
|
473 |
| - SuggestionArgs { |
474 |
| - span: span_snippet_of_span_call, |
| 494 | + SpanSuggestionSnippets { |
475 | 495 | help: help_snippet,
|
476 | 496 | sugg: sugg_snippet,
|
477 | 497 | applicability: applicability_snippet,
|
478 | 498 | }
|
479 | 499 | }
|
480 | 500 |
|
481 |
| -fn suggest_span_suggestion(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndThenArgs<'_>, suggestion_args: SuggestionArgs<'_>) { |
482 |
| - if and_then_args.span == suggestion_args.span { |
483 |
| - println!("suggestion true"); |
484 |
| - span_lint_and_sugg ( |
485 |
| - cx, |
486 |
| - COLLAPSIBLE_SPAN_LINT_CALLS, |
487 |
| - expr.span, |
488 |
| - "this call is collapsible", |
489 |
| - "collapse into", |
490 |
| - format!( |
491 |
| - "span_lint_and_sugg({}, {}, {}, {}, {}, {},{})", |
492 |
| - and_then_args.cx, |
493 |
| - and_then_args.lint, |
494 |
| - and_then_args.span, |
495 |
| - and_then_args.msg, |
496 |
| - suggestion_args.help, |
497 |
| - suggestion_args.sugg, |
498 |
| - suggestion_args.applicability |
499 |
| - ), |
500 |
| - Applicability::MachineApplicable |
501 |
| - ); |
502 |
| - } |
503 |
| -} |
504 |
| - |
505 |
| -struct HelpArgs<'a> { |
506 |
| - span: Cow<'a, str>, |
507 |
| - help: Cow<'a, str>, |
508 |
| -} |
509 |
| - |
510 |
| -fn help_args<'a>(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> HelpArgs<'a>{ |
511 |
| - let span_snippet_of_span_call = snippet(cx, span_call_args[0].span); |
512 |
| - let help_snippet = snippet(cx, span_call_args[1].span); |
513 |
| - |
514 |
| - HelpArgs { |
515 |
| - span: span_snippet_of_span_call, |
516 |
| - help: help_snippet, |
517 |
| - } |
518 |
| -} |
519 |
| - |
520 |
| -fn suggest_span_help(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndThenArgs<'_>, help_args: HelpArgs<'_>) { |
521 |
| - if and_then_args.span == help_args.span { |
522 |
| - span_lint_and_sugg( |
523 |
| - cx, |
524 |
| - COLLAPSIBLE_SPAN_LINT_CALLS, |
525 |
| - expr.span, |
526 |
| - "this call is collapsible", |
527 |
| - "collapse into", |
528 |
| - format!( |
529 |
| - "span_lint_and_help({}, {}, {}, {},{})", |
530 |
| - and_then_args.cx, |
531 |
| - and_then_args.lint, |
532 |
| - and_then_args.span, |
533 |
| - and_then_args.msg, |
534 |
| - help_args.help |
535 |
| - ), |
536 |
| - Applicability::MachineApplicable |
537 |
| - ); |
538 |
| - } |
539 |
| -} |
540 |
| - |
541 |
| -struct NoteArgs<'a> { |
542 |
| - span: Cow<'a, str>, |
543 |
| - note: Cow<'a, str>, |
544 |
| -} |
545 |
| - |
546 |
| -fn note_args<'a>(cx: &EarlyContext<'_>, span_call_args: &Vec<P<AstExpr>>) -> NoteArgs<'a> { |
547 |
| - let span_snippet_of_span_call = snippet(cx, span_call_args[0].span); |
548 |
| - let note_snippet = snippet(cx, span_call_args[1].span); |
549 |
| - |
550 |
| - NoteArgs { |
551 |
| - span: span_snippet_of_span_call, |
552 |
| - note: note_snippet, |
553 |
| - } |
554 |
| -} |
555 |
| - |
556 |
| -fn suggest_span_note(cx: &EarlyContext<'_>, expr: &AstExpr, and_then_args: AndThenArgs<'_> , note_args: NoteArgs<'_> ) { |
557 |
| - if and_then_args.span == note_args.span { |
558 |
| - span_lint_and_sugg( |
559 |
| - cx, |
560 |
| - COLLAPSIBLE_SPAN_LINT_CALLS, |
561 |
| - expr.span, |
562 |
| - "this call is collspible", |
563 |
| - "collapse into", |
564 |
| - format!( |
565 |
| - "span_lint_and_note({},{}, {}, {}, {})", |
566 |
| - and_then_args.cx, |
567 |
| - and_then_args.lint, |
568 |
| - and_then_args.span, |
569 |
| - and_then_args.msg, |
570 |
| - note_args.note |
571 |
| - ), |
572 |
| - Applicability::MachineApplicable |
573 |
| - ); |
574 |
| - } |
575 |
| -} |
576 |
| - |
577 |
| -fn snippet<'a>(cx: &EarlyContext<'_>, span: Span) -> Cow<'a, str> { |
578 |
| - other_snippet(cx, span, "Should not be") |
| 501 | +fn suggest_suggestion( |
| 502 | + cx: &LateContext<'_, '_>, |
| 503 | + expr: &Expr<'_>, |
| 504 | + and_then_snippets: AndThenSnippets<'_>, |
| 505 | + span_suggestion_snippets: SpanSuggestionSnippets<'_>, |
| 506 | +) { |
| 507 | + span_lint_and_sugg( |
| 508 | + cx, |
| 509 | + COLLAPSIBLE_SPAN_LINT_CALLS, |
| 510 | + expr.span, |
| 511 | + "this call is collapsible", |
| 512 | + "collapse into", |
| 513 | + format!( |
| 514 | + "span_lint_and_sugg({}, {}, {}, {}, {}, {}, {})", |
| 515 | + and_then_snippets.cx, |
| 516 | + and_then_snippets.lint, |
| 517 | + and_then_snippets.span, |
| 518 | + and_then_snippets.msg, |
| 519 | + span_suggestion_snippets.help, |
| 520 | + span_suggestion_snippets.sugg, |
| 521 | + span_suggestion_snippets.applicability |
| 522 | + ), |
| 523 | + Applicability::MachineApplicable, |
| 524 | + ); |
| 525 | +} |
| 526 | + |
| 527 | +fn suggest_help(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: AndThenSnippets<'_>, help: Cow<'_, str>) { |
| 528 | + span_lint_and_sugg( |
| 529 | + cx, |
| 530 | + COLLAPSIBLE_SPAN_LINT_CALLS, |
| 531 | + expr.span, |
| 532 | + "this call is collapsible", |
| 533 | + "collapse into", |
| 534 | + format!( |
| 535 | + "span_lint_and_help({}, {}, {}, {}, {})", |
| 536 | + and_then_snippets.cx, and_then_snippets.lint, and_then_snippets.span, and_then_snippets.msg, help |
| 537 | + ), |
| 538 | + Applicability::MachineApplicable, |
| 539 | + ); |
| 540 | +} |
| 541 | + |
| 542 | +fn suggest_note(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: AndThenSnippets<'_>, note: Cow<'_, str>) { |
| 543 | + span_lint_and_sugg( |
| 544 | + cx, |
| 545 | + COLLAPSIBLE_SPAN_LINT_CALLS, |
| 546 | + expr.span, |
| 547 | + "this call is collspible", |
| 548 | + "collapse into", |
| 549 | + format!( |
| 550 | + "span_lint_and_note({}, {}, {}, {}, {}, {})", |
| 551 | + and_then_snippets.cx, |
| 552 | + and_then_snippets.lint, |
| 553 | + and_then_snippets.span, |
| 554 | + and_then_snippets.msg, |
| 555 | + and_then_snippets.span, |
| 556 | + note |
| 557 | + ), |
| 558 | + Applicability::MachineApplicable, |
| 559 | + ); |
| 560 | +} |
| 561 | + |
| 562 | +fn snippet<'a>(cx: &LateContext<'_, '_>, span: Span) -> Cow<'a, str> { |
| 563 | + other_snippet(cx, span, "Should not be this snippet") |
579 | 564 | }
|
0 commit comments