You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
The text was updated successfully, but these errors were encountered:
Oftentimes
Drop
impls 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_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 likeVec
does). In that case, a guard struct can be defined and constructed just before dropping an item. TheDrop
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).The text was updated successfully, but these errors were encountered: