1 //! lint when items are used after statements
5 use utils::{in_macro, span_lint};
7 /// **What it does:** Checks for items declared after some statement in a block.
9 /// **Why is this bad?** Items live for the entire scope they are declared
10 /// in. But statements are processed in order. This might cause confusion as
11 /// it's hard to figure out which item is meant in a statement.
13 /// **Known problems:** None.
22 /// foo(); // prints "foo"
26 /// foo(); // prints "foo"
30 pub ITEMS_AFTER_STATEMENTS,
32 "blocks where an item comes after a statement"
35 pub struct ItemsAfterStatements;
37 impl LintPass for ItemsAfterStatements {
38 fn get_lints(&self) -> LintArray {
39 lint_array!(ITEMS_AFTER_STATEMENTS)
43 impl EarlyLintPass for ItemsAfterStatements {
44 fn check_block(&mut self, cx: &EarlyContext, item: &Block) {
45 if in_macro(item.span) {
50 let stmts = item.stmts
52 .map(|stmt| &stmt.node)
53 .skip_while(|s| matches!(**s, StmtKind::Item(..)));
55 // lint on all further items
57 if let StmtKind::Item(ref it) = *stmt {
58 if in_macro(it.span) {
61 if let ItemKind::MacroDef(..) = it.node {
62 // do not lint `macro_rules`, but continue processing further statements
67 ITEMS_AFTER_STATEMENTS,
69 "adding items after statements is confusing, since items exist from the \