Conversation
Currently, `tracing-attributes` generates a `let _ = ();` in between the
`if tracing::level_enabled!(...) {}` and the function body. This is
intended to suppress the `clippy::suspicious_else_formatting` lint,
which is generated when an `if` is followed immediately by a block with
no whitespace in between. Since we can't add whitespace in the generated
code (as `quote` produces a stream of _rust tokens_, not text), we
can't suppress the lint without adding a no-op statement.
However, unfortunately, the no-op let triggers a _different_ lint
(`clippy::let_unit_value`), when `clippy::pedantic` is enabled. This is
kind of annoying for some users.
This branch changes the code to suppress the
`suspicious_else_formatting` lint using `#[allow(...)]` attributes,
instead. The warning is turned back on inside of user code, since users
probably want the warning.
## Motivation The changes in #1607 introduced a potential compilation error when using the `#[instrument]` attribute on `async fn`s that return a type that includes a closure or is otherwise unnameable. This is because the future's body code was quoted in two separate places in order to have a separate branch when the span is statically disabled. This means that when a closure is returned, it will technically have two distinct types based on whether or not the span is enabled, since it originates from two separate source code locations (although `quote_spanned!` obscures this, so the compiler diagnostic will appear to have two closures originating from the same location). ## Solution This branch fixes this issue by changing the code generated for `#[instrument]`ed async functions. Unfortunately, for async functions, we can't have the optimization of not creating the span at all when the level is disabled, because we need to create the span _before_ creating the future, as it may borrow arguments. I've also added tests reproducing issue #1615 Fixes #1615
## 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 <eliza@buoyant.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
this backports bugfixes for issues introduced in
tracing-attributesv0.1.17 frommastertov0.1.x. in particular, this includes:clippy::suspicious_elsewithout nop let (attributes: suppressclippy::suspicious_elsewithout nop let #1614)i'll rebase and merge this pending CI, since these changes were already approved on
master.