]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/items_after_statements.rs
Remove crate:: prefixes from crate paths
[rust.git] / clippy_lints / src / items_after_statements.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 //! lint when items are used after statements
11
12 use crate::utils::{in_macro, span_lint};
13 use matches::matches;
14 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
15 use rustc::{declare_tool_lint, lint_array};
16 use syntax::ast::*;
17
18 /// **What it does:** Checks for items declared after some statement in a block.
19 ///
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.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 /// ```rust
28 /// fn foo() {
29 ///     println!("cake");
30 /// }
31 ///
32 /// fn main() {
33 ///     foo(); // prints "foo"
34 ///     fn foo() {
35 ///         println!("foo");
36 ///     }
37 ///     foo(); // prints "foo"
38 /// }
39 /// ```
40 declare_clippy_lint! {
41     pub ITEMS_AFTER_STATEMENTS,
42     pedantic,
43     "blocks where an item comes after a statement"
44 }
45
46 pub struct ItemsAfterStatements;
47
48 impl LintPass for ItemsAfterStatements {
49     fn get_lints(&self) -> LintArray {
50         lint_array!(ITEMS_AFTER_STATEMENTS)
51     }
52 }
53
54 impl EarlyLintPass for ItemsAfterStatements {
55     fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
56         if in_macro(item.span) {
57             return;
58         }
59
60         // skip initial items
61         let stmts = item
62             .stmts
63             .iter()
64             .map(|stmt| &stmt.node)
65             .skip_while(|s| matches!(**s, StmtKind::Item(..)));
66
67         // lint on all further items
68         for stmt in stmts {
69             if let StmtKind::Item(ref it) = *stmt {
70                 if in_macro(it.span) {
71                     return;
72                 }
73                 if let ItemKind::MacroDef(..) = it.node {
74                     // do not lint `macro_rules`, but continue processing further statements
75                     continue;
76                 }
77                 span_lint(
78                     cx,
79                     ITEMS_AFTER_STATEMENTS,
80                     it.span,
81                     "adding items after statements is confusing, since items exist from the \
82                      start of the scope",
83                 );
84             }
85         }
86     }
87 }