Oftentimes Drop impls contain code like this:
while let Some(_) = self.pop_front_node() {}
or this:
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).
Oftentimes
Dropimpls contain code like this:or this:
Both of these implementations will leak all items following an item whose destructor panics, and should be avoided.
If possible,
ptr::drop_in_placeshould 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_placeis not usable though (when the container doesn't use one fully linear backing store likeVecdoes). In that case, a guard struct can be defined and constructed just before dropping an item. TheDropimpl 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
forloop draining an iterator should also be linted against).