1 //! lint when items are used after statements
3 use crate::utils::span_lint;
4 use rustc_ast::ast::{Block, ItemKind, StmtKind};
5 use rustc_lint::{EarlyContext, EarlyLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Checks for items declared after some statement in a block.
11 /// **Why is this bad?** Items live for the entire scope they are declared
12 /// in. But statements are processed in order. This might cause confusion as
13 /// it's hard to figure out which item is meant in a statement.
15 /// **Known problems:** None.
24 /// foo(); // prints "foo"
28 /// foo(); // prints "foo"
31 pub ITEMS_AFTER_STATEMENTS,
33 "blocks where an item comes after a statement"
36 declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
38 impl EarlyLintPass for ItemsAfterStatements {
39 fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
40 if item.span.from_expansion() {
48 .map(|stmt| &stmt.kind)
49 .skip_while(|s| matches!(**s, StmtKind::Item(..)));
51 // lint on all further items
53 if let StmtKind::Item(ref it) = *stmt {
54 if it.span.from_expansion() {
57 if let ItemKind::MacroDef(..) = it.kind {
58 // do not lint `macro_rules`, but continue processing further statements
63 ITEMS_AFTER_STATEMENTS,
65 "adding items after statements is confusing, since items exist from the \