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

Incorrect let_underscore_drop warning on type without Drop implementation #130430

Closed
Elrendio opened this issue Sep 16, 2024 · 2 comments · Fixed by #130833
Closed

Incorrect let_underscore_drop warning on type without Drop implementation #130430

Elrendio opened this issue Sep 16, 2024 · 2 comments · Fixed by #130833
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-let_underscore_drop Lint: let_underscore_drop T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Elrendio
Copy link
Contributor

Elrendio commented Sep 16, 2024

I tried this code:

#![deny(let_underscore_drop)]

fn main() {
    let _ = MyStruct {
        field: vec![1, 2, 3],
    };
}

struct MyStruct {
    field: Vec<usize>,
}

I got this warning:

error: non-binding let on a type that implements `Drop`
 --> src/main.rs:4:5
  |
4 | /     let _ = MyStruct {
5 | |         field: vec![1, 2, 3],
6 | |     };
  | |______^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(let_underscore_drop)]
  |         ^^^^^^^^^^^^^^^^^^^
help: consider binding to an unused variable to avoid immediately dropping the value
  |
4 |     let _unused = MyStruct {
  |         ~~~~~~~
help: consider immediately dropping the value
  |
4 ~     drop(MyStruct {
5 |         field: vec![1, 2, 3],
6 ~     });
  |

However, there is no Drop impl on the type, so the error message is misleading.
It is true however that there is a destructor, as defined here: https://doc.rust-lang.org/stable/std/ops/trait.Drop.html

That being said, it's insiginificant for all underlying types (only #[may_dangle] destructor of Vec and Copy type in the Vec), so ideally the warning still wouldn't even show.

Meta

rustc --version --verbose:

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7
Backtrace

   Compiling MinimalReproNonBindingLetDrop v0.1.0 (/home/elrendio/Stockly/MinimalReproNonBindingLetDrop)
warning: field `field` is never read
  --> src/main.rs:10:5
   |
9  | struct MyStruct {
   |        -------- field in this struct
10 |     field: Vec<usize>,
   |     ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

error: non-binding let on a type that implements `Drop`
 --> src/main.rs:4:5
  |
4 | /     let _ = MyStruct {
5 | |         field: vec![1, 2, 3],
6 | |     };
  | |______^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(let_underscore_drop)]
  |         ^^^^^^^^^^^^^^^^^^^
help: consider binding to an unused variable to avoid immediately dropping the value
  |
4 |     let _unused = MyStruct {
  |         ~~~~~~~
help: consider immediately dropping the value
  |
4 ~     drop(MyStruct {
5 |         field: vec![1, 2, 3],
6 ~     });
  |

warning: `MinimalReproNonBindingLetDrop` (bin "MinimalReproNonBindingLetDrop") generated 1 warning
error: could not compile `MinimalReproNonBindingLetDrop` (bin "MinimalReproNonBindingLetDrop") due to 1 previous error; 1 warning emitted

@Elrendio Elrendio added the C-bug Category: This is a bug. label Sep 16, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 16, 2024
@jieyouxu jieyouxu added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. L-let_underscore_drop Lint: let_underscore_drop T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 16, 2024
@compiler-errors
Copy link
Member

Well, even if the T parameter of Vec<T> is #[may_dangle], it still has a side-effect (deallocates memory). I don't think we need this special-casing. I do think the diagnostic is a bit misleading and should just say "has a destructor" or "has side-effects when dropped".

Is there a more practical code example for why you'd want to suppress this error? The example you gave is a bit artificial, so it's a hard to motivate why we'd want to suppress this code since it really could be rewritten to be clearer.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 18, 2024
@makai410
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 26, 2024
…,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 26, 2024
…,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 26, 2024
…,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 26, 2024
…,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Sep 26, 2024
…,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
tgross35 added a commit to tgross35/rust that referenced this issue Sep 26, 2024
…,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
@bors bors closed this as completed in d3cb1ce Sep 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 27, 2024
Rollup merge of rust-lang#130833 - makai410:master, r=compiler-errors,fee1-dead

Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation

Closes: rust-lang#130430
r? rust-lang/diagnostics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-let_underscore_drop Lint: let_underscore_drop T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants