rust-lang / rust-lang/rust-clippy

let_underscore_drop should not suggest `drop()` ing types that do not impl drop() bounds

Open
#10,412 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

.

Lint Name

let-underscore-drop

Reproducer

I tried this code:

fn bar<T> (it: Box<[T]>)
 where
     [T] : std::ops::Index<usize>,
 {
     let _ = it[0];
 }

 fn main(){}

I saw this happen:
cargo clippy --fix --allow-dirty -- -Aclippy::all -Wclippy::let-underscore-drop
clippy suggests

fn bar<T> (it: Box<[T]>)
 where
     [T] : std::ops::Index<usize>,
 {
     drop(it[0]); // drop here
 }

 fn main(){}

But this does not compile

error[E0277]: the size for values of type `<[T] as Index<usize>>::Output` cannot be known at compilation time
   --> src/main.rs:5:11
    |
5   |      drop(it[0]);
    |      ---- ^^^^^ doesn't have a size known at compile-time
    |      |
    |      required by a bound introduced by this call
    |
    = help: the trait `Sized` is not implemented for `<[T] as Index<usize>>::Output`
note: required by a bound in `std::mem::drop`
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/mem/mod.rs:979:13
    |
979 | pub fn drop<T>(_x: T) {}
    |             ^ required by this bound in `drop`

For more information about this error, try `rustc --explain E0277`.
Version
rustc 1.69.0-nightly (49b9cc513 2023-02-27)
binary: rustc
commit-hash: 49b9cc5139dd4d11ef78dc08c1f9170de5b1ca39
commit-date: 2023-02-27
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the let-underscore-drop lint entry point and reproduce the issue with the Rust snippet and cargo clippy command shown. Check the lint's handling of unsized Index output types; done means it no longer suggests drop() when the suggested code fails to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.