]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/items_after_statements.rs
Merge pull request #3269 from rust-lang-nursery/relicense
[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
11 //! lint when items are used after statements
12
13 use matches::matches;
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};
18
19 /// **What it does:** Checks for items declared after some statement in a block.
20 ///
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.
24 ///
25 /// **Known problems:** None.
26 ///
27 /// **Example:**
28 /// ```rust
29 /// fn foo() {
30 ///     println!("cake");
31 /// }
32 ///
33 /// fn main() {
34 ///     foo(); // prints "foo"
35 ///     fn foo() {
36 ///         println!("foo");
37 ///     }
38 ///     foo(); // prints "foo"
39 /// }
40 /// ```
41 declare_clippy_lint! {
42     pub ITEMS_AFTER_STATEMENTS,
43     pedantic,
44     "blocks where an item comes after a statement"
45 }
46
47 pub struct ItemsAfterStatements;
48
49 impl LintPass for ItemsAfterStatements {
50     fn get_lints(&self) -> LintArray {
51         lint_array!(ITEMS_AFTER_STATEMENTS)
52     }
53 }
54
55 impl EarlyLintPass for ItemsAfterStatements {
56     fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
57         if in_macro(item.span) {
58             return;
59         }
60
61         // skip initial items
62         let stmts = item.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 }