Skip to content

let_underscore_drop hallunicates variable binding #119697

Closed
@matthiaskrgr

Description

@matthiaskrgr

Code

#![warn(let_underscore_drop)]
#![feature(type_alias_impl_trait)]

pub struct Foo {
    /// This type must have nontrivial drop glue
    field: String,
}

pub type Tait = impl Sized;

pub fn ice_cold(beverage: Tait) {
    // Must destructure at least one field of `Foo`
    let Foo { field } = beverage;
    // boom
    _ = field;
}


pub fn main() {}

Current output

warning: non-binding let on a type that implements `Drop`
  --> src/main.rs:15:5
   |
15 |     _ = field;
   |     ^^^^^^^^^
   |
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(let_underscore_drop)]
   |         ^^^^^^^^^^^^^^^^^^^
help: consider binding to an unused variable to avoid immediately dropping the value
   |
15 |     _unused = field;
   |     ~~~~~~~
help: consider immediately dropping the value
   |
15 |     drop(field);
   |     ~~~~~     +

warning: `drop` (bin "drop") generated 1 warning (run `cargo fix --bin "drop"` to apply 1 suggestion)

Desired output

We should suggest `let _unused = filed` since there is no `_unused` in scope.

Rationale and extra context

Otherwise it will fail to cargo-fix:

error[E0425]: cannot find value `_unused` in this scope
  --> src/main.rs:15:5
   |
15 |     _unused = field;
   |     ^^^^^^^
   |
help: you might have meant to introduce a new binding
   |
15 |     let _unused = field;
   |     +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.
Original diagnostics will follow.

warning: `drop` (bin "drop" test) generated 1 warning (1 duplicate)

Other cases

No response

Rust Version

rustc 1.77.0-nightly (87e143089 2024-01-07)
binary: rustc
commit-hash: 87e1430893d55001034bd76c5dbe832d80bc50c3
commit-date: 2024-01-07
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions