diff --git a/tracing-attributes/src/lib.rs b/tracing-attributes/src/lib.rs index b3029341c3..f02ec21a3c 100644 --- a/tracing-attributes/src/lib.rs +++ b/tracing-attributes/src/lib.rs @@ -563,8 +563,6 @@ fn gen_block( __tracing_attr_span = #span; __tracing_attr_guard = __tracing_attr_span.enter(); } - // pacify clippy::suspicious_else_formatting - let _ = (); ); if err { @@ -583,8 +581,17 @@ fn gen_block( } quote_spanned!(block.span()=> - #span - #block + // 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. + // Therefore, suppress the lint inside the generated code... + #[allow(clippy::suspicious_else_formatting)] + { + #span + // ...but turn the lint back on inside the function body. + #[warn(clippy::suspicious_else_formatting)] + #block + } ) } diff --git a/tracing-attributes/tests/async_fn.rs b/tracing-attributes/tests/async_fn.rs index 816795946e..e7627fbbb2 100644 --- a/tracing-attributes/tests/async_fn.rs +++ b/tracing-attributes/tests/async_fn.rs @@ -15,6 +15,9 @@ async fn test_async_fn(polls: usize) -> Result<(), ()> { future.await } +#[instrument] +async fn test_async_fn_empty() {} + #[test] fn async_fn_only_enters_for_polls() { let (collector, handle) = collector::mock() diff --git a/tracing-attributes/tests/err.rs b/tracing-attributes/tests/err.rs index f78b69518d..808134d101 100644 --- a/tracing-attributes/tests/err.rs +++ b/tracing-attributes/tests/err.rs @@ -16,6 +16,12 @@ fn err() -> Result { u8::try_from(1234) } +#[instrument(err)] +fn err_suspicious_else() -> Result { + {} + u8::try_from(1234) +} + #[test] fn test() { let span = span::mock().named("err");