Skip to content

Commit d9821e1

Browse files
committed
Auto merge of #10542 - Alexendoo:items-after-statements, r=giraffate
Fix allow attribute, items from macros in `items_after_statements` Fixes #10540 changelog: [`items_after_statements`]: Fixes `#[allow(clippy::items_after_statements)]` when applied to an item, and ignores items after statements from different macro contexts
2 parents 70db226 + b13f99c commit d9821e1

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

clippy_lints/src/items_after_statements.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! lint when items are used after statements
22
3-
use clippy_utils::diagnostics::span_lint;
4-
use rustc_ast::ast::{Block, ItemKind, StmtKind};
5-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
3+
use clippy_utils::diagnostics::span_lint_hir;
4+
use rustc_hir::{Block, ItemKind, StmtKind};
5+
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88

@@ -52,33 +52,34 @@ declare_clippy_lint! {
5252

5353
declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
5454

55-
impl EarlyLintPass for ItemsAfterStatements {
56-
fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
57-
if in_external_macro(cx.sess(), item.span) {
55+
impl LateLintPass<'_> for ItemsAfterStatements {
56+
fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) {
57+
if in_external_macro(cx.sess(), block.span) {
5858
return;
5959
}
6060

61-
// skip initial items and trailing semicolons
62-
let stmts = item
61+
// skip initial items
62+
let stmts = block
6363
.stmts
6464
.iter()
65-
.map(|stmt| &stmt.kind)
66-
.skip_while(|s| matches!(**s, StmtKind::Item(..) | StmtKind::Empty));
65+
.skip_while(|stmt| matches!(stmt.kind, StmtKind::Item(..)));
6766

6867
// lint on all further items
6968
for stmt in stmts {
70-
if let StmtKind::Item(ref it) = *stmt {
71-
if in_external_macro(cx.sess(), it.span) {
69+
if let StmtKind::Item(item_id) = stmt.kind {
70+
let item = cx.tcx.hir().item(item_id);
71+
if in_external_macro(cx.sess(), item.span) || !item.span.eq_ctxt(block.span) {
7272
return;
7373
}
74-
if let ItemKind::MacroDef(..) = it.kind {
74+
if let ItemKind::Macro(..) = item.kind {
7575
// do not lint `macro_rules`, but continue processing further statements
7676
continue;
7777
}
78-
span_lint(
78+
span_lint_hir(
7979
cx,
8080
ITEMS_AFTER_STATEMENTS,
81-
it.span,
81+
item.hir_id(),
82+
item.span,
8283
"adding items after statements is confusing, since items exist from the \
8384
start of the scope",
8485
);

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
747747
store.register_early_pass(|| Box::new(unused_unit::UnusedUnit));
748748
store.register_late_pass(|_| Box::new(returns::Return));
749749
store.register_early_pass(|| Box::new(collapsible_if::CollapsibleIf));
750-
store.register_early_pass(|| Box::new(items_after_statements::ItemsAfterStatements));
750+
store.register_late_pass(|_| Box::new(items_after_statements::ItemsAfterStatements));
751751
store.register_early_pass(|| Box::new(precedence::Precedence));
752752
store.register_late_pass(|_| Box::new(needless_parens_on_range_literals::NeedlessParensOnRangeLiterals));
753753
store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue));

tests/ui/item_after_statement.rs renamed to tests/ui/items_after_statement.rs

+17
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,20 @@ fn semicolon() {
5151

5252
let _ = S::new(3);
5353
}
54+
55+
fn item_from_macro() {
56+
macro_rules! static_assert_size {
57+
($ty:ty, $size:expr) => {
58+
const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
59+
};
60+
}
61+
62+
let _ = 1;
63+
static_assert_size!(u32, 4);
64+
}
65+
66+
fn allow_attribute() {
67+
let _ = 1;
68+
#[allow(clippy::items_after_statements)]
69+
const _: usize = 1;
70+
}

tests/ui/item_after_statement.stderr renamed to tests/ui/items_after_statement.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: adding items after statements is confusing, since items exist from the start of the scope
2-
--> $DIR/item_after_statement.rs:13:5
2+
--> $DIR/items_after_statement.rs:13:5
33
|
44
LL | / fn foo() {
55
LL | | println!("foo");
@@ -9,15 +9,15 @@ LL | | }
99
= note: `-D clippy::items-after-statements` implied by `-D warnings`
1010

1111
error: adding items after statements is confusing, since items exist from the start of the scope
12-
--> $DIR/item_after_statement.rs:20:5
12+
--> $DIR/items_after_statement.rs:20:5
1313
|
1414
LL | / fn foo() {
1515
LL | | println!("foo");
1616
LL | | }
1717
| |_____^
1818

1919
error: adding items after statements is confusing, since items exist from the start of the scope
20-
--> $DIR/item_after_statement.rs:33:13
20+
--> $DIR/items_after_statement.rs:33:13
2121
|
2222
LL | / fn say_something() {
2323
LL | | println!("something");

0 commit comments

Comments
 (0)