summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2023-05-07 14:12:17 +0900
committerGitHub <noreply@github.com>2023-05-07 14:12:17 +0900
commit816e0295b51fe88ec167b47b8dc11982611fc4c4 (patch)
tree83814c9801728cb0e69e1f295ec3656a1c361133
parent88a0204fcb6608862834f8b2c898168881fd3b9e (diff)
parentec7fcdc959872d7ba9e1d20fa598a3f95678885b (diff)
downloadrust-816e0295b51fe88ec167b47b8dc11982611fc4c4.tar.gz
rust-816e0295b51fe88ec167b47b8dc11982611fc4c4.zip
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`.
-rw-r--r--library/core/src/option.rs6
1 files changed, 2 insertions, 4 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index c38c68e1d58..ec1ef3cf43d 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -1641,10 +1641,8 @@ impl<T> Option<T> {
     where
         F: FnOnce() -> T,
     {
-        if let None = *self {
-            // the compiler isn't smart enough to know that we are not dropping a `T`
-            // here and wants us to ensure `T` can be dropped at compile time.
-            mem::forget(mem::replace(self, Some(f())))
+        if let None = self {
+            *self = Some(f());
         }
 
         // SAFETY: a `None` variant for `self` would have been replaced by a `Some`