]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/items_after_statements.rs
Add 'library/portable-simd/' from commit '1ce1c645cf27c4acdefe6ec8a11d1f0491954a99'
[rust.git] / src / tools / clippy / clippy_lints / src / items_after_statements.rs
1 //! lint when items are used after statements
2
3 use clippy_utils::diagnostics::span_lint;
4 use rustc_ast::ast::{Block, ItemKind, StmtKind};
5 use rustc_lint::{EarlyContext, EarlyLintPass};
6 use rustc_middle::lint::in_external_macro;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for items declared after some statement in a block.
12     ///
13     /// ### Why is this bad?
14     /// Items live for the entire scope they are declared
15     /// in. But statements are processed in order. This might cause confusion as
16     /// it's hard to figure out which item is meant in a statement.
17     ///
18     /// ### Example
19     /// ```rust
20     /// // Bad
21     /// fn foo() {
22     ///     println!("cake");
23     /// }
24     ///
25     /// fn main() {
26     ///     foo(); // prints "foo"
27     ///     fn foo() {
28     ///         println!("foo");
29     ///     }
30     ///     foo(); // prints "foo"
31     /// }
32     /// ```
33     ///
34     /// ```rust
35     /// // Good
36     /// fn foo() {
37     ///     println!("cake");
38     /// }
39     ///
40     /// fn main() {
41     ///     fn foo() {
42     ///         println!("foo");
43     ///     }
44     ///     foo(); // prints "foo"
45     ///     foo(); // prints "foo"
46     /// }
47     /// ```
48     pub ITEMS_AFTER_STATEMENTS,
49     pedantic,
50     "blocks where an item comes after a statement"
51 }
52
53 declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
54
55 impl EarlyLintPass for ItemsAfterStatements {
56     fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
57         if in_external_macro(cx.sess, item.span) {
58             return;
59         }
60
61         // skip initial items and trailing semicolons
62         let stmts = item
63             .stmts
64             .iter()
65             .map(|stmt| &stmt.kind)
66             .skip_while(|s| matches!(**s, StmtKind::Item(..) | StmtKind::Empty));
67
68         // lint on all further items
69         for stmt in stmts {
70             if let StmtKind::Item(ref it) = *stmt {
71                 if in_external_macro(cx.sess, it.span) {
72                     return;
73                 }
74                 if let ItemKind::MacroDef(..) = it.kind {
75                     // do not lint `macro_rules`, but continue processing further statements
76                     continue;
77                 }
78                 span_lint(
79                     cx,
80                     ITEMS_AFTER_STATEMENTS,
81                     it.span,
82                     "adding items after statements is confusing, since items exist from the \
83                      start of the scope",
84                 );
85             }
86         }
87     }
88 }