rust-lang / rust-lang/rust-clippy
Lint leaky Drop impls that do nothing but drop items in a loop
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Oftentimes Drop impls contain code like this:
while let Some(_) = self.pop_front_node() {}
or this:
self.0.for_each(drop);
Both of these implementations will leak all items following an item whose destructor panics, and should be avoided.
If possible, ptr::drop_in_place should be used, which, when called on a *mut [T], will handle this case correctly and continue invoking the remaining destructors in the unwind path (this is currently not documented though – https://github.com/rust-lang/rust/issues/64407).
Often ptr::drop_in_place is not usable though (when the container doesn't use one fully linear backing store like Vec does). In that case, a guard struct can be defined and constructed just before dropping an item. The Drop impl of the guard struct then has to continue draining the container. An example of this can be found here: https://github.com/rust-lang/rust/pull/67243
(this is only an issue if the dropped type is user-controlled ie. a generic type)
It would be nice to lint this, but I'm not yet sure how to make it generic enough (eg. an empty for loop draining an iterator should also be linted against).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the issue's loop and for_each(drop) examples, then review the discussion of ptr::drop_in_place and guard structs, including the linked Rust issue and pull request. Done means defining a sufficiently generic Clippy lint for user-controlled dropped types while avoiding false positives for cases that are safe.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100