1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 //! lint when items are used after statements
14 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
15 use crate::rustc::{declare_tool_lint, lint_array};
16 use crate::syntax::ast::*;
17 use crate::utils::{in_macro, span_lint};
19 /// **What it does:** Checks for items declared after some statement in a block.
21 /// **Why is this bad?** Items live for the entire scope they are declared
22 /// in. But statements are processed in order. This might cause confusion as
23 /// it's hard to figure out which item is meant in a statement.
25 /// **Known problems:** None.
34 /// foo(); // prints "foo"
38 /// foo(); // prints "foo"
41 declare_clippy_lint! {
42 pub ITEMS_AFTER_STATEMENTS,
44 "blocks where an item comes after a statement"
47 pub struct ItemsAfterStatements;
49 impl LintPass for ItemsAfterStatements {
50 fn get_lints(&self) -> LintArray {
51 lint_array!(ITEMS_AFTER_STATEMENTS)
55 impl EarlyLintPass for ItemsAfterStatements {
56 fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
57 if in_macro(item.span) {
62 let stmts = item.stmts
64 .map(|stmt| &stmt.node)
65 .skip_while(|s| matches!(**s, StmtKind::Item(..)));
67 // lint on all further items
69 if let StmtKind::Item(ref it) = *stmt {
70 if in_macro(it.span) {
73 if let ItemKind::MacroDef(..) = it.node {
74 // do not lint `macro_rules`, but continue processing further statements
79 ITEMS_AFTER_STATEMENTS,
81 "adding items after statements is confusing, since items exist from the \