1
Fork 0

Rollup merge of #111301 - JohnBobbo96:cleanup_option_insert_methods, r=scottmcm

Remove calls to `mem::forget` and `mem::replace` in `Option::get_or_insert_with`.

This removes the unneeded calls to `mem::forget` and `mem::replace` in `Option::get_or_insert_with`.
This commit is contained in:
Yuki Okushi 2023-05-07 14:12:17 +09:00 committed by GitHub
commit 816e0295b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1641,10 +1641,8 @@ impl<T> Option<T> {
where where
F: FnOnce() -> T, F: FnOnce() -> T,
{ {
if let None = *self { if let None = self {
// the compiler isn't smart enough to know that we are not dropping a `T` *self = Some(f());
// here and wants us to ensure `T` can be dropped at compile time.
mem::forget(mem::replace(self, Some(f())))
} }
// SAFETY: a `None` variant for `self` would have been replaced by a `Some` // SAFETY: a `None` variant for `self` would have been replaced by a `Some`