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 – 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).