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

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

Open
LeoniePhiline opened this issue Dec 5, 2023 · 0 comments

Comments

@LeoniePhiline
Copy link
Contributor

LeoniePhiline commented Dec 5, 2023

Bug Report

Version

  • tracing v0.1.40
  • tracing-attributes v0.1.27 (proc-macro)
  • tracing-core v0.1.32
  • tracing-error v0.2.0
  • tracing-log v0.2.0
  • tracing-subscriber v0.3.18
  • syslog-tracing v0.2.0

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.

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

1 participant