Dont suggest remove semi inside macro expansion for redundant semi lint

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
xizheyin
2025-06-17 22:48:58 +08:00
parent 1ab8ff57d6
commit 72fbf3ea61
4 changed files with 22 additions and 8 deletions

View File

@@ -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
}

View File

@@ -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<RedundantSemicolonsSuggestion>,
}
#[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

View File

@@ -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 },
);
}
}

View File

@@ -2,7 +2,7 @@ error: unnecessary trailing semicolon
--> $DIR/suggest-remove-semi-in-macro-expansion-issue-142143.rs:6:43
|
LL | ($stmt:stmt) => { #[allow(bad_style)] $stmt }
| ^^^^^ help: remove this semicolon
| ^^^^^
...
LL | m!(;);
| ----- in this macro invocation