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