Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint items after statements in local macro expansions #6176

Merged
merged 1 commit into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clippy_lints/src/items_after_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use crate::utils::span_lint;
use rustc_ast::ast::{Block, ItemKind, StmtKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
Expand Down Expand Up @@ -53,7 +54,7 @@ declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);

impl EarlyLintPass for ItemsAfterStatements {
fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
if item.span.from_expansion() {
if in_external_macro(cx.sess(), item.span) {
return;
}

Expand All @@ -67,7 +68,7 @@ impl EarlyLintPass for ItemsAfterStatements {
// lint on all further items
for stmt in stmts {
if let StmtKind::Item(ref it) = *stmt {
if it.span.from_expansion() {
if in_external_macro(cx.sess(), it.span) {
return;
}
if let ItemKind::MacroDef(..) = it.kind {
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/item_after_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ fn mac() {
// do not lint this, because it needs to be after `a`
macro_rules! b {
() => {{
a = 6
a = 6;
fn say_something() {
println!("something");
}
}};
}
b!();
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/item_after_statement.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,18 @@ LL | | println!("foo");
LL | | }
| |_____^

error: aborting due to 2 previous errors
error: adding items after statements is confusing, since items exist from the start of the scope
--> $DIR/item_after_statement.rs:32:13
|
LL | / fn say_something() {
LL | | println!("something");
LL | | }
| |_____________^
...
LL | b!();
| ----- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors