1 //! lint when items are used after statements
5 use utils::{in_macro, span_lint};
7 /// **What it does:** This lints checks for items declared after some statement in a block
9 /// **Why is this bad?** Items live for the entire scope they are declared in. But statements are
10 /// processed in order. This might cause confusion as it's hard to figure out which item is meant
13 /// **Known problems:** None
22 /// foo(); // prints "foo"
26 /// foo(); // prints "foo"
30 pub ITEMS_AFTER_STATEMENTS,
32 "finds 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(cx, item.span) {
50 let stmts = item.stmts.iter()
51 .map(|stmt| &stmt.node)
52 .skip_while(|s| matches!(**s, StmtKind::Item(..)));
54 // lint on all further items
56 if let StmtKind::Item(ref it) = *stmt {
57 if in_macro(cx, it.span) {
61 ITEMS_AFTER_STATEMENTS,
63 "adding items after statements is confusing, since items exist from the \