tokio-rs / tracing

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

`tracing::instrument` triggers `clippy::used_underscore_bindings` on unused arguments starting with underscore #2822

Open LeoniePhiline opened 9 months ago

LeoniePhiline commented 9 months ago

Bug Report

Version

Platform

Linux <hostname> 6.6.3-1-default #1 SMP PREEMPT_DYNAMIC Wed Nov 29 05:06:07 UTC 2023 (d766c57) x86_64 x86_64 x86_64 GNU/Linux

Crates

tracing-attributes (tracing::instrument)

Description

The following code contains an unused function argument, prefixed with _.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2308e79f9b714df38c3d424a0bb90a08

use tracing::instrument; // 0.1.40

#[warn(clippy::used_underscore_binding)]
#[instrument]
fn foo(_bar: u8) {
    println!("Not using bar.");
}

This code unexpectedly triggers clippy::used_underscore_binding:

    Checking playground v0.0.1 (/playground)
warning: used binding `_bar` which is prefixed with an underscore. A leading underscore signals that a binding will not be used
 --> src/lib.rs:5:8
  |
5 | fn foo(_bar: u8) {
  |        ^^^^
  |
note: `_bar` is defined here
 --> src/lib.rs:5:8
  |
5 | fn foo(_bar: u8) {
  |        ^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#used_underscore_binding
note: the lint level is defined here
 --> src/lib.rs:3:8
  |
3 | #[warn(clippy::used_underscore_binding)]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `playground` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 2.34s

To my mind, _bar should be considered unused, even though tracing::instrument does indeed access it - as it is unused in non-autogenerated user code.

This expectation is in line with struct fields which are unaccessed in user code and prefixed with _:

These do not trigger clippy in derived code.

Proposed fix

tracing::instrument should apply #[allow(clippy::used_underscore_bindings)] to the argument in auto-generated code.

Workaround

As a temporary workaround #[instrument(skip(_bar))] can be used, which makes tracing in fact not access the unused field.

However, the logging of parameters passed as supposedly unused argument may be intentional and desired.