diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 46ae414652870..f92f830780801 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -738,7 +738,8 @@ lint_redundant_semicolons = [true] semicolons *[false] semicolon } - .suggestion = remove {$multiple -> + +lint_redundant_semicolons_suggestion = remove {$multiple_semicolons -> [true] these semicolons *[false] this semicolon } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 3d17dfbc45198..0b8c68404f16c 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1538,8 +1538,16 @@ pub(crate) struct PassByValueDiag { #[diag(lint_redundant_semicolons)] pub(crate) struct RedundantSemicolonsDiag { pub multiple: bool, - #[suggestion(code = "", applicability = "maybe-incorrect")] - pub suggestion: Span, + #[subdiagnostic] + pub suggestion: Option, +} + +#[derive(Subdiagnostic)] +#[suggestion(lint_redundant_semicolons_suggestion, code = "", applicability = "maybe-incorrect")] +pub(crate) struct RedundantSemicolonsSuggestion { + pub multiple_semicolons: bool, + #[primary_span] + pub span: Span, } // traits.rs diff --git a/compiler/rustc_lint/src/redundant_semicolon.rs b/compiler/rustc_lint/src/redundant_semicolon.rs index b43e4938b736c..f6d2fbe42618e 100644 --- a/compiler/rustc_lint/src/redundant_semicolon.rs +++ b/compiler/rustc_lint/src/redundant_semicolon.rs @@ -2,7 +2,7 @@ use rustc_ast::{Block, StmtKind}; use rustc_session::{declare_lint, declare_lint_pass}; use rustc_span::Span; -use crate::lints::RedundantSemicolonsDiag; +use crate::lints::{RedundantSemicolonsDiag, RedundantSemicolonsSuggestion}; use crate::{EarlyContext, EarlyLintPass, LintContext}; declare_lint! { @@ -44,16 +44,21 @@ impl EarlyLintPass for RedundantSemicolons { fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) { if let Some((span, multiple)) = seq.take() { - // FIXME: Find a better way of ignoring the trailing - // semicolon from macro expansion if span == rustc_span::DUMMY_SP { return; } + // Ignore redundant semicolons inside macro expansion.(issue #142143) + let suggestion = if span.from_expansion() { + None + } else { + Some(RedundantSemicolonsSuggestion { multiple_semicolons: multiple, span }) + }; + cx.emit_span_lint( REDUNDANT_SEMICOLONS, span, - RedundantSemicolonsDiag { multiple, suggestion: span }, + RedundantSemicolonsDiag { multiple, suggestion }, ); } } diff --git a/tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.rs b/tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.rs new file mode 100644 index 0000000000000..4360eb964a4af --- /dev/null +++ b/tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.rs @@ -0,0 +1,11 @@ +// Make sure we don't suggest remove redundant semicolon inside macro expansion.(issue #142143) + +#![deny(redundant_semicolons)] + +macro_rules! m { + ($stmt:stmt) => { #[allow(bad_style)] $stmt } //~ ERROR unnecessary trailing semicolon [redundant_semicolons] +} + +fn main() { + m!(;); +} diff --git a/tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.stderr b/tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.stderr new file mode 100644 index 0000000000000..7a38ec318ab6a --- /dev/null +++ b/tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.stderr @@ -0,0 +1,18 @@ +error: unnecessary trailing semicolon + --> $DIR/suggest-remove-semi-in-macro-expansion-issue-142143.rs:6:43 + | +LL | ($stmt:stmt) => { #[allow(bad_style)] $stmt } + | ^^^^^ +... +LL | m!(;); + | ----- in this macro invocation + | +note: the lint level is defined here + --> $DIR/suggest-remove-semi-in-macro-expansion-issue-142143.rs:3:9 + | +LL | #![deny(redundant_semicolons)] + | ^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error +