Skip to content

Commit f6bc738

Browse files
authored
Rollup merge of #87385 - Aaron1011:final-enable-semi, r=petrochenkov
Make `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` warn by default This PR makes the `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint warn by default. To avoid showing a large number of un-actionable warnings to users, we only enable the lint for macros defined in the same crate. This ensures that users will be able to fix the warning by simply removing a semicolon. In the future, I'd like to enable this lint unconditionally, and eventually make it into a hard error in a future edition. This PR is a step towards that goal.
2 parents 9391d55 + e70ce57 commit f6bc738

21 files changed

+135
-37
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ crate struct ParserAnyMacro<'a> {
4545
lint_node_id: NodeId,
4646
is_trailing_mac: bool,
4747
arm_span: Span,
48+
/// Whether or not this macro is defined in the current crate
49+
is_local: bool,
4850
}
4951

5052
crate fn annotate_err_with_kind(
@@ -124,6 +126,7 @@ impl<'a> ParserAnyMacro<'a> {
124126
lint_node_id,
125127
arm_span,
126128
is_trailing_mac,
129+
is_local,
127130
} = *self;
128131
let snapshot = &mut parser.clone();
129132
let fragment = match parse_ast_fragment(parser, kind) {
@@ -138,13 +141,15 @@ impl<'a> ParserAnyMacro<'a> {
138141
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
139142
// but `m!()` is allowed in expression positions (cf. issue #34706).
140143
if kind == AstFragmentKind::Expr && parser.token == token::Semi {
141-
parser.sess.buffer_lint_with_diagnostic(
142-
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
143-
parser.token.span,
144-
lint_node_id,
145-
"trailing semicolon in macro used in expression position",
146-
BuiltinLintDiagnostics::TrailingMacro(is_trailing_mac, macro_ident),
147-
);
144+
if is_local {
145+
parser.sess.buffer_lint_with_diagnostic(
146+
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
147+
parser.token.span,
148+
lint_node_id,
149+
"trailing semicolon in macro used in expression position",
150+
BuiltinLintDiagnostics::TrailingMacro(is_trailing_mac, macro_ident),
151+
);
152+
}
148153
parser.bump();
149154
}
150155

@@ -162,6 +167,7 @@ struct MacroRulesMacroExpander {
162167
lhses: Vec<mbe::TokenTree>,
163168
rhses: Vec<mbe::TokenTree>,
164169
valid: bool,
170+
is_local: bool,
165171
}
166172

167173
impl TTMacroExpander for MacroRulesMacroExpander {
@@ -183,6 +189,7 @@ impl TTMacroExpander for MacroRulesMacroExpander {
183189
input,
184190
&self.lhses,
185191
&self.rhses,
192+
self.is_local,
186193
)
187194
}
188195
}
@@ -210,6 +217,7 @@ fn generic_extension<'cx>(
210217
arg: TokenStream,
211218
lhses: &[mbe::TokenTree],
212219
rhses: &[mbe::TokenTree],
220+
is_local: bool,
213221
) -> Box<dyn MacResult + 'cx> {
214222
let sess = &cx.sess.parse_sess;
215223

@@ -311,6 +319,7 @@ fn generic_extension<'cx>(
311319
lint_node_id: cx.current_expansion.lint_node_id,
312320
is_trailing_mac: cx.current_expansion.is_trailing_mac,
313321
arm_span,
322+
is_local,
314323
});
315324
}
316325
Failure(token, msg) => match best_failure {
@@ -544,6 +553,9 @@ pub fn compile_declarative_macro(
544553
lhses,
545554
rhses,
546555
valid,
556+
// Macros defined in the current crate have a real node id,
557+
// whereas macros from an external crate have a dummy id.
558+
is_local: def.id != DUMMY_NODE_ID,
547559
}))
548560
}
549561

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,7 @@ declare_lint! {
27992799
/// [issue #79813]: https://github.com/rust-lang/rust/issues/79813
28002800
/// [future-incompatible]: ../index.md#future-incompatible-lints
28012801
pub SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
2802-
Allow,
2802+
Warn,
28032803
"trailing semicolon in macro body used as expression",
28042804
@future_incompatible = FutureIncompatibleInfo {
28052805
reference: "issue #79813 <https://github.com/rust-lang/rust/issues/79813>",

library/std/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ macro_rules! dbg {
290290
// `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
291291
// will be malformed.
292292
() => {
293-
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
293+
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!())
294294
};
295295
($val:expr $(,)?) => {
296296
// Use of `match` here is intentional because it affects the lifetimes

src/test/ui/hygiene/auxiliary/intercrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod foo {
55
mod bar {
66
fn f() -> u32 { 1 }
77
pub macro m() {
8-
f();
8+
f()
99
}
1010
}
1111
}

src/test/ui/hygiene/hygienic-label-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ macro_rules! foo {
33
}
44

55
pub fn main() {
6-
'x: loop { foo!() }
6+
'x: loop { foo!(); }
77
}

src/test/ui/hygiene/hygienic-label-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0426]: use of undeclared label `'x`
44
LL | () => { break 'x; }
55
| ^^ undeclared label `'x`
66
...
7-
LL | 'x: loop { foo!() }
8-
| ------ in this macro invocation
7+
LL | 'x: loop { foo!(); }
8+
| ------- in this macro invocation
99
|
1010
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

src/test/ui/hygiene/hygienic-label-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ macro_rules! foo {
44

55
pub fn main() {
66
'x: for _ in 0..1 {
7-
foo!()
7+
foo!();
88
};
99
}

src/test/ui/hygiene/hygienic-label-3.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0426]: use of undeclared label `'x`
44
LL | () => { break 'x; }
55
| ^^ undeclared label `'x`
66
...
7-
LL | foo!()
8-
| ------ in this macro invocation
7+
LL | foo!();
8+
| ------- in this macro invocation
99
|
1010
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

src/test/ui/lint/semicolon-in-expressions-from-macros/allow-semicolon-in-expressions-from-macros.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[macro_export]
2+
macro_rules! my_macro {
3+
() => { true; }
4+
}

0 commit comments

Comments
 (0)