1 //! lint when items are used after statements
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
21 /// foo(); // prints "foo"
25 /// foo(); // prints "foo"
29 pub ITEMS_AFTER_STATEMENTS,
31 "finds blocks where an item comes after a statement"
34 pub struct ItemsAfterStatements;
36 impl LintPass for ItemsAfterStatements {
37 fn get_lints(&self) -> LintArray {
38 lint_array!(ITEMS_AFTER_STATEMENTS)
42 impl EarlyLintPass for ItemsAfterStatements {
43 fn check_block(&mut self, cx: &EarlyContext, item: &Block) {
44 if in_macro(cx, item.span) {
47 let mut stmts = item.stmts.iter().map(|stmt| &stmt.node);
49 while let Some(&StmtKind::Decl(ref decl, _)) = stmts.next() {
50 if let DeclKind::Local(_) = decl.node {
54 // lint on all further items
56 if let StmtKind::Decl(ref decl, _) = *stmt {
57 if let DeclKind::Item(ref it) = decl.node {
58 if in_macro(cx, it.span) {
61 cx.struct_span_lint(ITEMS_AFTER_STATEMENTS,
63 "adding items after statements is confusing, since items exist from the \