|
1 | 1 | //! lint when items are used after statements
|
2 | 2 |
|
3 |
| -use clippy_utils::diagnostics::span_lint; |
4 |
| -use rustc_ast::ast::{Block, ItemKind, StmtKind}; |
5 |
| -use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; |
| 3 | +use clippy_utils::diagnostics::span_lint_hir; |
| 4 | +use rustc_hir::{Block, ItemKind, StmtKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
6 | 6 | use rustc_middle::lint::in_external_macro;
|
7 | 7 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
8 | 8 |
|
@@ -52,33 +52,34 @@ declare_clippy_lint! {
|
52 | 52 |
|
53 | 53 | declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
|
54 | 54 |
|
55 |
| -impl EarlyLintPass for ItemsAfterStatements { |
56 |
| - fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) { |
57 |
| - if in_external_macro(cx.sess(), item.span) { |
| 55 | +impl LateLintPass<'_> for ItemsAfterStatements { |
| 56 | + fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) { |
| 57 | + if in_external_macro(cx.sess(), block.span) { |
58 | 58 | return;
|
59 | 59 | }
|
60 | 60 |
|
61 |
| - // skip initial items and trailing semicolons |
62 |
| - let stmts = item |
| 61 | + // skip initial items |
| 62 | + let stmts = block |
63 | 63 | .stmts
|
64 | 64 | .iter()
|
65 |
| - .map(|stmt| &stmt.kind) |
66 |
| - .skip_while(|s| matches!(**s, StmtKind::Item(..) | StmtKind::Empty)); |
| 65 | + .skip_while(|stmt| matches!(stmt.kind, StmtKind::Item(..))); |
67 | 66 |
|
68 | 67 | // lint on all further items
|
69 | 68 | for stmt in stmts {
|
70 |
| - if let StmtKind::Item(ref it) = *stmt { |
71 |
| - if in_external_macro(cx.sess(), it.span) { |
| 69 | + if let StmtKind::Item(item_id) = stmt.kind { |
| 70 | + let item = cx.tcx.hir().item(item_id); |
| 71 | + if in_external_macro(cx.sess(), item.span) || !item.span.eq_ctxt(block.span) { |
72 | 72 | return;
|
73 | 73 | }
|
74 |
| - if let ItemKind::MacroDef(..) = it.kind { |
| 74 | + if let ItemKind::Macro(..) = item.kind { |
75 | 75 | // do not lint `macro_rules`, but continue processing further statements
|
76 | 76 | continue;
|
77 | 77 | }
|
78 |
| - span_lint( |
| 78 | + span_lint_hir( |
79 | 79 | cx,
|
80 | 80 | ITEMS_AFTER_STATEMENTS,
|
81 |
| - it.span, |
| 81 | + item.hir_id(), |
| 82 | + item.span, |
82 | 83 | "adding items after statements is confusing, since items exist from the \
|
83 | 84 | start of the scope",
|
84 | 85 | );
|
|
0 commit comments