]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/items_after_statements.rs
Rollup merge of #91470 - wesleywiser:code_coverage_link_error, r=tmandry
[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     #[clippy::version = "pre 1.29.0"]
49     pub ITEMS_AFTER_STATEMENTS,
50     pedantic,
51     "blocks where an item comes after a statement"
52 }
53
54 declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
55
56 impl EarlyLintPass for ItemsAfterStatements {
57     fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
58         if in_external_macro(cx.sess, item.span) {
59             return;
60         }
61
62         // skip initial items and trailing semicolons
63         let stmts = item
64             .stmts
65             .iter()
66             .map(|stmt| &stmt.kind)
67             .skip_while(|s| matches!(**s, StmtKind::Item(..) | StmtKind::Empty));
68
69         // lint on all further items
70         for stmt in stmts {
71             if let StmtKind::Item(ref it) = *stmt {
72                 if in_external_macro(cx.sess, it.span) {
73                     return;
74                 }
75                 if let ItemKind::MacroDef(..) = it.kind {
76                     // do not lint `macro_rules`, but continue processing further statements
77                     continue;
78                 }
79                 span_lint(
80                     cx,
81                     ITEMS_AFTER_STATEMENTS,
82                     it.span,
83                     "adding items after statements is confusing, since items exist from the \
84                      start of the scope",
85                 );
86             }
87         }
88     }
89 }