Skip to content

Commit 72fbf3e

Browse files
committed
Dont suggest remove semi inside macro expansion for redundant semi lint
Signed-off-by: xizheyin <[email protected]>
1 parent 1ab8ff5 commit 72fbf3e

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,8 @@ lint_redundant_semicolons =
738738
[true] semicolons
739739
*[false] semicolon
740740
}
741-
.suggestion = remove {$multiple ->
741+
742+
lint_redundant_semicolons_suggestion = remove {$multiple_semicolons ->
742743
[true] these semicolons
743744
*[false] this semicolon
744745
}

compiler/rustc_lint/src/lints.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,16 @@ pub(crate) struct PassByValueDiag {
15381538
#[diag(lint_redundant_semicolons)]
15391539
pub(crate) struct RedundantSemicolonsDiag {
15401540
pub multiple: bool,
1541-
#[suggestion(code = "", applicability = "maybe-incorrect")]
1542-
pub suggestion: Span,
1541+
#[subdiagnostic]
1542+
pub suggestion: Option<RedundantSemicolonsSuggestion>,
1543+
}
1544+
1545+
#[derive(Subdiagnostic)]
1546+
#[suggestion(lint_redundant_semicolons_suggestion, code = "", applicability = "maybe-incorrect")]
1547+
pub(crate) struct RedundantSemicolonsSuggestion {
1548+
pub multiple_semicolons: bool,
1549+
#[primary_span]
1550+
pub span: Span,
15431551
}
15441552

15451553
// traits.rs

compiler/rustc_lint/src/redundant_semicolon.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::{Block, StmtKind};
22
use rustc_session::{declare_lint, declare_lint_pass};
33
use rustc_span::Span;
44

5-
use crate::lints::RedundantSemicolonsDiag;
5+
use crate::lints::{RedundantSemicolonsDiag, RedundantSemicolonsSuggestion};
66
use crate::{EarlyContext, EarlyLintPass, LintContext};
77

88
declare_lint! {
@@ -44,16 +44,21 @@ impl EarlyLintPass for RedundantSemicolons {
4444

4545
fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
4646
if let Some((span, multiple)) = seq.take() {
47-
// FIXME: Find a better way of ignoring the trailing
48-
// semicolon from macro expansion
4947
if span == rustc_span::DUMMY_SP {
5048
return;
5149
}
5250

51+
// Ignore redundant semicolons inside macro expansion.(issue #142143)
52+
let suggestion = if span.from_expansion() {
53+
None
54+
} else {
55+
Some(RedundantSemicolonsSuggestion { multiple_semicolons: multiple, span })
56+
};
57+
5358
cx.emit_span_lint(
5459
REDUNDANT_SEMICOLONS,
5560
span,
56-
RedundantSemicolonsDiag { multiple, suggestion: span },
61+
RedundantSemicolonsDiag { multiple, suggestion },
5762
);
5863
}
5964
}

tests/ui/lint/redundant-semicolon/suggest-remove-semi-in-macro-expansion-issue-142143.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unnecessary trailing semicolon
22
--> $DIR/suggest-remove-semi-in-macro-expansion-issue-142143.rs:6:43
33
|
44
LL | ($stmt:stmt) => { #[allow(bad_style)] $stmt }
5-
| ^^^^^ help: remove this semicolon
5+
| ^^^^^
66
...
77
LL | m!(;);
88
| ----- in this macro invocation

0 commit comments

Comments
 (0)