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
This commit is contained in:
bors 2023-03-28 13:21:14 +00:00
commit d9821e1d9d
4 changed files with 37 additions and 19 deletions

View File

@ -1,8 +1,8 @@
//! lint when items are used after statements //! lint when items are used after statements
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint_hir;
use rustc_ast::ast::{Block, ItemKind, StmtKind}; use rustc_hir::{Block, ItemKind, StmtKind};
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -52,33 +52,34 @@ declare_clippy_lint! {
declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]); declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
impl EarlyLintPass for ItemsAfterStatements { impl LateLintPass<'_> for ItemsAfterStatements {
fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) { fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) {
if in_external_macro(cx.sess(), item.span) { if in_external_macro(cx.sess(), block.span) {
return; return;
} }
// skip initial items and trailing semicolons // skip initial items
let stmts = item let stmts = block
.stmts .stmts
.iter() .iter()
.map(|stmt| &stmt.kind) .skip_while(|stmt| matches!(stmt.kind, StmtKind::Item(..)));
.skip_while(|s| matches!(**s, StmtKind::Item(..) | StmtKind::Empty));
// lint on all further items // lint on all further items
for stmt in stmts { for stmt in stmts {
if let StmtKind::Item(ref it) = *stmt { if let StmtKind::Item(item_id) = stmt.kind {
if in_external_macro(cx.sess(), it.span) { let item = cx.tcx.hir().item(item_id);
if in_external_macro(cx.sess(), item.span) || !item.span.eq_ctxt(block.span) {
return; return;
} }
if let ItemKind::MacroDef(..) = it.kind { if let ItemKind::Macro(..) = item.kind {
// do not lint `macro_rules`, but continue processing further statements // do not lint `macro_rules`, but continue processing further statements
continue; continue;
} }
span_lint( span_lint_hir(
cx, cx,
ITEMS_AFTER_STATEMENTS, ITEMS_AFTER_STATEMENTS,
it.span, item.hir_id(),
item.span,
"adding items after statements is confusing, since items exist from the \ "adding items after statements is confusing, since items exist from the \
start of the scope", start of the scope",
); );

View File

@ -747,7 +747,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(|| Box::new(unused_unit::UnusedUnit)); store.register_early_pass(|| Box::new(unused_unit::UnusedUnit));
store.register_late_pass(|_| Box::new(returns::Return)); store.register_late_pass(|_| Box::new(returns::Return));
store.register_early_pass(|| Box::new(collapsible_if::CollapsibleIf)); store.register_early_pass(|| Box::new(collapsible_if::CollapsibleIf));
store.register_early_pass(|| Box::new(items_after_statements::ItemsAfterStatements)); store.register_late_pass(|_| Box::new(items_after_statements::ItemsAfterStatements));
store.register_early_pass(|| Box::new(precedence::Precedence)); store.register_early_pass(|| Box::new(precedence::Precedence));
store.register_late_pass(|_| Box::new(needless_parens_on_range_literals::NeedlessParensOnRangeLiterals)); store.register_late_pass(|_| Box::new(needless_parens_on_range_literals::NeedlessParensOnRangeLiterals));
store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue)); store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue));

View File

@ -51,3 +51,20 @@ fn semicolon() {
let _ = S::new(3); let _ = S::new(3);
} }
fn item_from_macro() {
macro_rules! static_assert_size {
($ty:ty, $size:expr) => {
const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
};
}
let _ = 1;
static_assert_size!(u32, 4);
}
fn allow_attribute() {
let _ = 1;
#[allow(clippy::items_after_statements)]
const _: usize = 1;
}

View File

@ -1,5 +1,5 @@
error: adding items after statements is confusing, since items exist from the start of the scope error: adding items after statements is confusing, since items exist from the start of the scope
--> $DIR/item_after_statement.rs:13:5 --> $DIR/items_after_statement.rs:13:5
| |
LL | / fn foo() { LL | / fn foo() {
LL | | println!("foo"); LL | | println!("foo");
@ -9,7 +9,7 @@ LL | | }
= note: `-D clippy::items-after-statements` implied by `-D warnings` = note: `-D clippy::items-after-statements` implied by `-D warnings`
error: adding items after statements is confusing, since items exist from the start of the scope error: adding items after statements is confusing, since items exist from the start of the scope
--> $DIR/item_after_statement.rs:20:5 --> $DIR/items_after_statement.rs:20:5
| |
LL | / fn foo() { LL | / fn foo() {
LL | | println!("foo"); LL | | println!("foo");
@ -17,7 +17,7 @@ LL | | }
| |_____^ | |_____^
error: adding items after statements is confusing, since items exist from the start of the scope error: adding items after statements is confusing, since items exist from the start of the scope
--> $DIR/item_after_statement.rs:33:13 --> $DIR/items_after_statement.rs:33:13
| |
LL | / fn say_something() { LL | / fn say_something() {
LL | | println!("something"); LL | | println!("something");