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.
10 //! lint when items are used after statements
12 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::syntax::ast::*;
15 use crate::utils::{in_macro, span_lint};
18 /// **What it does:** Checks for items declared after some statement in a block.
20 /// **Why is this bad?** Items live for the entire scope they are declared
21 /// in. But statements are processed in order. This might cause confusion as
22 /// it's hard to figure out which item is meant in a statement.
24 /// **Known problems:** None.
33 /// foo(); // prints "foo"
37 /// foo(); // prints "foo"
40 declare_clippy_lint! {
41 pub ITEMS_AFTER_STATEMENTS,
43 "blocks where an item comes after a statement"
46 pub struct ItemsAfterStatements;
48 impl LintPass for ItemsAfterStatements {
49 fn get_lints(&self) -> LintArray {
50 lint_array!(ITEMS_AFTER_STATEMENTS)
54 impl EarlyLintPass for ItemsAfterStatements {
55 fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
56 if in_macro(item.span) {
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 \