]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/items_after_statements.rs
Rollup merge of #105123 - BlackHoleFox:fixing-the-macos-deployment, r=oli-obk
[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, LintContext};
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     /// fn foo() {
21     ///     println!("cake");
22     /// }
23     ///
24     /// fn main() {
25     ///     foo(); // prints "foo"
26     ///     fn foo() {
27     ///         println!("foo");
28     ///     }
29     ///     foo(); // prints "foo"
30     /// }
31     /// ```
32     ///
33     /// Use instead:
34     /// ```rust
35     /// fn foo() {
36     ///     println!("cake");
37     /// }
38     ///
39     /// fn main() {
40     ///     fn foo() {
41     ///         println!("foo");
42     ///     }
43     ///     foo(); // prints "foo"
44     ///     foo(); // prints "foo"
45     /// }
46     /// ```
47     #[clippy::version = "pre 1.29.0"]
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 }