1 //! lint when items are used after statements
3 use crate::utils::span_lint;
5 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Checks for items declared after some statement in a block.
12 /// **Why is this bad?** Items live for the entire scope they are declared
13 /// in. But statements are processed in order. This might cause confusion as
14 /// it's hard to figure out which item is meant in a statement.
16 /// **Known problems:** None.
25 /// foo(); // prints "foo"
29 /// foo(); // prints "foo"
32 pub ITEMS_AFTER_STATEMENTS,
34 "blocks where an item comes after a statement"
37 declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
39 impl EarlyLintPass for ItemsAfterStatements {
40 fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
41 if item.span.from_expansion() {
49 .map(|stmt| &stmt.kind)
50 .skip_while(|s| matches!(**s, StmtKind::Item(..)));
52 // lint on all further items
54 if let StmtKind::Item(ref it) = *stmt {
55 if it.span.from_expansion() {
58 if let ItemKind::MacroDef(..) = it.kind {
59 // do not lint `macro_rules`, but continue processing further statements
64 ITEMS_AFTER_STATEMENTS,
66 "adding items after statements is confusing, since items exist from the \