Skip to content

Commit

Permalink
attributes: remove unnecessary quote_spanned!
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
hawkw committed Oct 5, 2021
1 parent 59f645b commit 4c6466d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ fn gen_block(
)
};

return quote_spanned!(block.span()=>
return quote!(
if tracing::level_enabled!(#level) {
let __tracing_attr_span = #span;
tracing::Instrument::instrument(
Expand All @@ -546,7 +546,7 @@ fn gen_block(
);
}

let span = quote_spanned!(block.span()=>
let span = quote!(
// These variables are left uninitialized and initialized only
// if the tracing level is statically enabled at this point.
// While the tracing level is also checked at span creation
Expand Down Expand Up @@ -580,7 +580,7 @@ fn gen_block(
);
}

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

0 comments on commit 4c6466d

Please sign in to comment.