tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.33k stars 697 forks source link

Adding #[instrument] to a one-line function produces an unused_braces warning #2830

Closed shepmaster closed 5 months ago

shepmaster commented 9 months ago

Bug Report

Version

Platform

N/A (reproduced on the Rust Playground (x86_64 Linux) and my laptop (aarch64-macos-darwin)

Description

I tried this code:

#[tracing::instrument]
fn demo() -> u8 { 1 }

I expected this to compile with no warnings. Instead, a warning is triggered and the warning would make invalid code:

warning: unnecessary braces around block return value
 --> src/lib.rs:2:17
  |
2 | fn demo() -> u8 { 1 }
  |                 ^^ ^^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
2 - fn demo() -> u8 { 1 }
2 + fn demo() -> u8 1
  |

Even stranger, the warning is sensitive to how the original function was formatted. I don't think I've ever seen that behavior before 😬. This code has no warnings and only differs in a few newlines:

#[tracing::instrument]
fn demo() -> u8 {
    1
}
mladedav commented 6 months ago

That's a pretty interesting find.

Even stranger, the warning is sensitive to how the original function was formatted. I don't think I've ever seen that behavior before 😬. This code has no warnings and only differs in a few newlines:

I think there is an issue with the lint. For example this does not create warnings for me (note that there is no macro)

fn demo() -> u8 { {
    1
} }

while this creates the unused braces warning (here the suggestion at least does not break the code)

fn demo() -> u8 { { 1 } }

I don't think this whitespace-dependent behavior is intended so it might be worthwhile opening an issue in rust.

I can confirm that no matter the formatting the expanded code is identical. It seems this lint runs on code before macro expansion so I don't think this can be fixed by tracing for example by adding an allow.

shepmaster commented 5 months ago

Looks like this already exists at https://github.com/rust-lang/rust/issues/116347