Skip to content

Commit f663823

Browse files
authored
Rollup merge of #142631 - xizheyin:142143, r=Urgau
Dont suggest remove semi inside macro expansion for redundant semi lint Fixes #142143 r? compiler
2 parents 6148ec9 + 72fbf3e commit f663823

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
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
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Make sure we don't suggest remove redundant semicolon inside macro expansion.(issue #142143)
2+
3+
#![deny(redundant_semicolons)]
4+
5+
macro_rules! m {
6+
($stmt:stmt) => { #[allow(bad_style)] $stmt } //~ ERROR unnecessary trailing semicolon [redundant_semicolons]
7+
}
8+
9+
fn main() {
10+
m!(;);
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unnecessary trailing semicolon
2+
--> $DIR/suggest-remove-semi-in-macro-expansion-issue-142143.rs:6:43
3+
|
4+
LL | ($stmt:stmt) => { #[allow(bad_style)] $stmt }
5+
| ^^^^^
6+
...
7+
LL | m!(;);
8+
| ----- in this macro invocation
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/suggest-remove-semi-in-macro-expansion-issue-142143.rs:3:9
12+
|
13+
LL | #![deny(redundant_semicolons)]
14+
| ^^^^^^^^^^^^^^^^^^^^
15+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: aborting due to 1 previous error
18+

0 commit comments

Comments
 (0)