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

Lint leaky Drop impls that do nothing but drop items in a loop #4899

Open
jonas-schievink opened this issue Dec 13, 2019 · 0 comments
Open
Labels
A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group

Comments

@jonas-schievink
Copy link
Contributor

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 – rust-lang/rust#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: rust-lang/rust#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).

@flip1995 flip1995 added L-correctness Lint: Belongs in the correctness lint group A-lint Area: New lints labels Dec 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
Development

No branches or pull requests

2 participants