Skip to content

false positive for no_effect_underscore_binding when drop is implemented for a field of the assigned struct #14592

Open
@koalatux

Description

@koalatux

Summary

clippy shows a false positive warning for no_effect_underscore_binding when the assigned struct does not implement Drop by itself but a field of it does.

Lint Name

no_effect_underscore_binding

Reproducer

I tried this code:

struct MyStruct {
    _inner: MyInner,
}
struct MyInner {}

impl Drop for MyInner {
    fn drop(&mut self) {
        println!("dropping");
    }
}

fn main() {
    let x = MyStruct { _inner: MyInner {} };

    let closure = || {
        let _x = x;
    };

    println!("1");
    closure();
    println!("2");
}

I saw this happen:

$ cargo clippy --all-features --all-targets -- -Wclippy::all -Wclippy::pedantic
    Checking clippy_test v0.1.0 (/home/adi/clippy_test)
warning: binding to `_` prefixed variable with no side-effect
  --> src/main.rs:16:13
   |
16 |         let _x = x;
   |             ^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#no_effect_underscore_binding
   = note: `-W clippy::no-effect-underscore-binding` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::no_effect_underscore_binding)]`

warning: `clippy_test` (bin "clippy_test") generated 1 warning
warning: `clippy_test` (bin "clippy_test" test) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s

I expected to see this happen:

I did not expect a clippy warning for this line.

Replacing the _x with the non-binding _ or removing the line altogether changes the behaviour of the program, which can easily be seen from the different output when the example is run.

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions