Skip to content

Commit a74b2fb

Browse files
committed
Fix early lints inside an async desugaring
Fixes #81531 When we buffer an early lint for a macro invocation, we need to determine which NodeId to take the lint level from. Currently, we use the `NodeId` of the closest def parent. However, if the macro invocation is inside the desugared closure from an `async fn` or async closure, that `NodeId` does not actually exist in the AST. This commit explicitly calls `check_lint` for the `NodeId`s of closures desugared from async expressions, ensuring that we do not miss any buffered lints.
1 parent b81f581 commit a74b2fb

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

compiler/rustc_lint/src/early.rs

+16
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
143143
run_early_pass!(self, check_fn, fk, span, id);
144144
self.check_id(id);
145145
ast_visit::walk_fn(self, fk, span);
146+
147+
// Explicitly check for lints associated with 'closure_id', since
148+
// it does not have a corresponding AST node
149+
if let ast_visit::FnKind::Fn(_, _, sig, _, _) = fk {
150+
if let ast::Async::Yes { closure_id, .. } = sig.header.asyncness {
151+
self.check_id(closure_id);
152+
}
153+
}
146154
run_early_pass!(self, check_fn_post, fk, span, id);
147155
}
148156

@@ -208,6 +216,14 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
208216

209217
fn visit_expr_post(&mut self, e: &'a ast::Expr) {
210218
run_early_pass!(self, check_expr_post, e);
219+
220+
// Explicitly check for lints associated with 'closure_id', since
221+
// it does not have a corresponding AST node
222+
if let ast::ExprKind::Closure(_, asyncness, ..) = e.kind {
223+
if let ast::Async::Yes { closure_id, .. } = asyncness {
224+
self.check_id(closure_id);
225+
}
226+
}
211227
}
212228

213229
fn visit_generic_arg(&mut self, arg: &'a ast::GenericArg) {

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

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// check-pass
2+
// edition:2018
23
#![warn(semicolon_in_expressions_from_macros)]
34

45
#[allow(dead_code)]
@@ -11,6 +12,11 @@ macro_rules! foo {
1112
}
1213
}
1314

15+
#[allow(semicolon_in_expressions_from_macros)]
16+
async fn bar() {
17+
foo!(first);
18+
}
19+
1420
fn main() {
1521
// This `allow` doesn't work
1622
#[allow(semicolon_in_expressions_from_macros)]

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: trailing semicolon in macro used in expression position
2-
--> $DIR/semicolon-in-expressions-from-macros.rs:7:13
2+
--> $DIR/semicolon-in-expressions-from-macros.rs:8:13
33
|
44
LL | true;
55
| ^
@@ -8,7 +8,7 @@ LL | foo!(first)
88
| ----------- in this macro invocation
99
|
1010
note: the lint level is defined here
11-
--> $DIR/semicolon-in-expressions-from-macros.rs:2:9
11+
--> $DIR/semicolon-in-expressions-from-macros.rs:3:9
1212
|
1313
LL | #![warn(semicolon_in_expressions_from_macros)]
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -17,7 +17,7 @@ LL | #![warn(semicolon_in_expressions_from_macros)]
1717
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1818

1919
warning: trailing semicolon in macro used in expression position
20-
--> $DIR/semicolon-in-expressions-from-macros.rs:7:13
20+
--> $DIR/semicolon-in-expressions-from-macros.rs:8:13
2121
|
2222
LL | true;
2323
| ^

0 commit comments

Comments
 (0)