Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shepmaster opened this issue Dec 7, 2023 · 2 comments

Comments

@shepmaster
Copy link
Contributor

Bug Report

Version

  • tracing 0.1.40

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
Copy link
Contributor

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
Copy link
Contributor Author

Looks like this already exists at rust-lang/rust#116347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants