1 //! lint when items are used after statements
4 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
7 use crate::utils::{in_macro, span_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 declare_clippy_lint! {
32 pub ITEMS_AFTER_STATEMENTS,
34 "blocks where an item comes after a statement"
37 pub struct ItemsAfterStatements;
39 impl LintPass for ItemsAfterStatements {
40 fn get_lints(&self) -> LintArray {
41 lint_array!(ITEMS_AFTER_STATEMENTS)
45 impl EarlyLintPass for ItemsAfterStatements {
46 fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
47 if in_macro(item.span) {
52 let stmts = item.stmts
54 .map(|stmt| &stmt.node)
55 .skip_while(|s| matches!(**s, StmtKind::Item(..)));
57 // lint on all further items
59 if let StmtKind::Item(ref it) = *stmt {
60 if in_macro(it.span) {
63 if let ItemKind::MacroDef(..) = it.node {
64 // do not lint `macro_rules`, but continue processing further statements
69 ITEMS_AFTER_STATEMENTS,
71 "adding items after statements is confusing, since items exist from the \