Skip to content

Commit a313f5d

Browse files
jarrodldavishawkw
authored andcommitted
attributes: remove extra braces around async blocks (#2090)
## Motivation When using an `async` block (as an alternative to `async fn`, e.g. when implementing a trait), `#[instrument]` adds extra braces around the wrapped `async` block. This causes `rustc` to emit an `unused_braces` lint in some cases (usually for single-line `async` blocks, as far as I can tell). See #1831 for an example. ## Solution Since the `async` block extracted by `AsyncInfo::from_fn` already has braces around its statements, there's no need to wrap it with additional braces. This updates `gen_block` to remove those extra braces when generating the code providing the value of `__tracing_instrument_future`. - [x] add repros for `unused_braces` issue - [x] remove extra braces from async blocks Fixes #1831
1 parent 0e135bc commit a313f5d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

tracing-attributes/src/expand.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ fn gen_block<B: ToTokens>(
217217
let mk_fut = match (err_event, ret_event) {
218218
(Some(err_event), Some(ret_event)) => quote_spanned!(block.span()=>
219219
async move {
220-
match async move { #block }.await {
220+
match async move #block.await {
221221
#[allow(clippy::unit_arg)]
222222
Ok(x) => {
223223
#ret_event;
@@ -232,7 +232,7 @@ fn gen_block<B: ToTokens>(
232232
),
233233
(Some(err_event), None) => quote_spanned!(block.span()=>
234234
async move {
235-
match async move { #block }.await {
235+
match async move #block.await {
236236
#[allow(clippy::unit_arg)]
237237
Ok(x) => Ok(x),
238238
Err(e) => {
@@ -244,13 +244,13 @@ fn gen_block<B: ToTokens>(
244244
),
245245
(None, Some(ret_event)) => quote_spanned!(block.span()=>
246246
async move {
247-
let x = async move { #block }.await;
247+
let x = async move #block.await;
248248
#ret_event;
249249
x
250250
}
251251
),
252252
(None, None) => quote_spanned!(block.span()=>
253-
async move { #block }
253+
async move #block
254254
),
255255
};
256256

tracing-attributes/tests/async_fn.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use tracing_mock::*;
22

3+
use std::convert::Infallible;
34
use std::{future::Future, pin::Pin, sync::Arc};
45
use tracing::subscriber::with_default;
56
use tracing_attributes::instrument;
@@ -51,6 +52,22 @@ async fn repro_1613_2() {
5152
// else
5253
}
5354

55+
// Reproduces https://github.com/tokio-rs/tracing/issues/1831
56+
#[instrument]
57+
#[deny(unused_braces)]
58+
fn repro_1831() -> Pin<Box<dyn Future<Output = ()>>> {
59+
Box::pin(async move {})
60+
}
61+
62+
// This replicates the pattern used to implement async trait methods on nightly using the
63+
// `type_alias_impl_trait` feature
64+
#[instrument(ret, err)]
65+
#[deny(unused_braces)]
66+
#[allow(clippy::manual_async_fn)]
67+
fn repro_1831_2() -> impl Future<Output = Result<(), Infallible>> {
68+
async { Ok(()) }
69+
}
70+
5471
#[test]
5572
fn async_fn_only_enters_for_polls() {
5673
let (subscriber, handle) = subscriber::mock()

0 commit comments

Comments
 (0)