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

let_underscore_drop hallunicates variable binding #119697

Closed
matthiaskrgr opened this issue Jan 7, 2024 · 0 comments · Fixed by #119704
Closed

let_underscore_drop hallunicates variable binding #119697

matthiaskrgr opened this issue Jan 7, 2024 · 0 comments · Fixed by #119704
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 7, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 7, 2024
@chenyukang chenyukang self-assigned this Jan 7, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 7, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 8, 2024
…re, r=Nilstrieb

Fix two variable binding issues in lint let_underscore

Fixes rust-lang#119696
Fixes rust-lang#119697
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 8, 2024
…re, r=Nilstrieb

Fix two variable binding issues in lint let_underscore

Fixes rust-lang#119696
Fixes rust-lang#119697
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 8, 2024
…re, r=Nilstrieb

Fix two variable binding issues in lint let_underscore

Fixes rust-lang#119696
Fixes rust-lang#119697
@bors bors closed this as completed in 5efa69d Jan 9, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 9, 2024
Rollup merge of rust-lang#119704 - chenyukang:yukang-fix-let_underscore, r=Nilstrieb

Fix two variable binding issues in lint let_underscore

Fixes rust-lang#119696
Fixes rust-lang#119697
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants