Skip to content

Commit 2f6fcb0

Browse files
authored
attributes: remove unnecessary quote_spanned! (#1617)
## Motivation Apparently, using `quote_spanned!` can trigger a Clippy bug where the text `else`, even inside a comment, _may_ cause the `suspicious_else_formatting` lint to be triggered incorrectly (see rust-lang/rust-clippy#7760 and rust-lang/rust-clippy#6249). This causes the lint to fire in some cases when the `#[instrument]` attribute is used on `async fn`s. See issue #1613 for details. ## Solution It turns out that some of the uses of `quote_spanned!` in the `tracing-attributes` code generation are not needed. We really only need `quote_spanned!` when actually interpolating the user provided code into a block, not in the `tracing-attributes` code that inserts the generated code for producing the span etc. Replacing some of these `quote_spanned!` uses with the normal `quote!` macro still generates correct location diagnostics for errors in the user code, but fixes the incorrect clippy lint. I've added a few test cases that should reproduce the bug. Fixes #1613 Signed-off-by: Eliza Weisman <[email protected]>
1 parent 72400d7 commit 2f6fcb0

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

tracing-attributes/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ fn gen_block(
532532
)
533533
};
534534

535-
return quote_spanned!(block.span()=>
535+
return quote!(
536536
let __tracing_attr_span = #span;
537537
let __tracing_instrument_future = #mk_fut;
538538
if !__tracing_attr_span.is_disabled() {
@@ -547,7 +547,7 @@ fn gen_block(
547547
);
548548
}
549549

550-
let span = quote_spanned!(block.span()=>
550+
let span = quote!(
551551
// These variables are left uninitialized and initialized only
552552
// if the tracing level is statically enabled at this point.
553553
// While the tracing level is also checked at span creation
@@ -581,7 +581,7 @@ fn gen_block(
581581
);
582582
}
583583

584-
quote_spanned!(block.span()=>
584+
quote_spanned!(block.span() =>
585585
// Because `quote` produces a stream of tokens _without_ whitespace, the
586586
// `if` and the block will appear directly next to each other. This
587587
// generates a clippy lint about suspicious `if/else` formatting.

tracing-attributes/tests/async_fn.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ async fn test_ret_impl_trait_err(n: i32) -> Result<impl Iterator<Item = i32>, &'
3333
#[instrument]
3434
async fn test_async_fn_empty() {}
3535

36+
// Reproduces https://github.com/tokio-rs/tracing/issues/1613
37+
#[instrument]
38+
// LOAD-BEARING `#[rustfmt::skip]`! This is necessary to reproduce the bug;
39+
// with the rustfmt-generated formatting, the lint will not be triggered!
40+
#[rustfmt::skip]
41+
#[deny(clippy::suspicious_else_formatting)]
42+
async fn repro_1613(var: bool) {
43+
println!(
44+
"{}",
45+
if var { "true" } else { "false" }
46+
);
47+
}
48+
49+
// Reproduces https://github.com/tokio-rs/tracing/issues/1613
50+
// and https://github.com/rust-lang/rust-clippy/issues/7760
51+
#[instrument]
52+
#[deny(clippy::suspicious_else_formatting)]
53+
async fn repro_1613_2() {
54+
// hello world
55+
// else
56+
}
57+
3658
#[test]
3759
fn async_fn_only_enters_for_polls() {
3860
let (collector, handle) = collector::mock()

0 commit comments

Comments
 (0)