about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-08-26 18:40:51 +0000
committerbors <bors@rust-lang.org>2020-08-26 18:40:51 +0000
commit48717b6f3ce661d2a0d64f7bdfdfb5fd3484ee5b (patch)
tree6132a4164c89982923e6303d7f39373f2ae46efa
parent1f2dd3b56aec4d8f97eb0b0552c166ef549d3316 (diff)
parentf3024073f92b15d38b42241e65067f0ba796896c (diff)
downloadrust-48717b6f3ce661d2a0d64f7bdfdfb5fd3484ee5b.tar.gz
rust-48717b6f3ce661d2a0d64f7bdfdfb5fd3484ee5b.zip
Auto merge of #75912 - scottmcm:manuallydrop-vs-forget, r=Mark-Simulacrum
Suggest `mem::forget` if `mem::ManuallyDrop::new` isn't used

I think this communicates the intent more idiomatically, and is shorter anyway.

Inspired because [it came up on URLO](https://users.rust-lang.org/t/validity-of-memory-area-after-std-forget/47730/7?u=scottmcm), and it turns out that std had done it too in one spot:
![image](https://user-images.githubusercontent.com/18526288/91203819-e19f2980-e6f2-11ea-9112-835f3b22ce05.png)
-rw-r--r--library/core/src/mem/manually_drop.rs6
-rw-r--r--library/core/src/mem/mod.rs2
-rw-r--r--library/std/src/lazy.rs2
3 files changed, 7 insertions, 3 deletions
diff --git a/library/core/src/mem/manually_drop.rs b/library/core/src/mem/manually_drop.rs
index e45aa86c079..aab0e96d83a 100644
--- a/library/core/src/mem/manually_drop.rs
+++ b/library/core/src/mem/manually_drop.rs
@@ -74,8 +74,12 @@ impl<T> ManuallyDrop<T> {
     ///
     /// ```rust
     /// use std::mem::ManuallyDrop;
-    /// ManuallyDrop::new(Box::new(()));
+    /// let mut x = ManuallyDrop::new(String::from("Hello World!"));
+    /// x.truncate(5); // You can still safely operate on the value
+    /// assert_eq!(*x, "Hello");
+    /// // But `Drop` will not be run here
     /// ```
+    #[must_use = "if you don't need the wrapper, you can use `mem::forget` instead"]
     #[stable(feature = "manually_drop", since = "1.20.0")]
     #[rustc_const_stable(feature = "const_manually_drop", since = "1.36.0")]
     #[inline(always)]
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs
index 9107c570a89..6d8ed2f4ffb 100644
--- a/library/core/src/mem/mod.rs
+++ b/library/core/src/mem/mod.rs
@@ -145,7 +145,7 @@ pub use crate::intrinsics::transmute;
 #[rustc_const_stable(feature = "const_forget", since = "1.46.0")]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub const fn forget<T>(t: T) {
-    ManuallyDrop::new(t);
+    let _ = ManuallyDrop::new(t);
 }
 
 /// Like [`forget`], but also accepts unsized values.
diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs
index f0548582d2f..d0f27df5185 100644
--- a/library/std/src/lazy.rs
+++ b/library/std/src/lazy.rs
@@ -293,7 +293,7 @@ impl<T> SyncOnceCell<T> {
 
         // Don't drop this `SyncOnceCell`. We just moved out one of the fields, but didn't set
         // the state to uninitialized.
-        mem::ManuallyDrop::new(self);
+        mem::forget(self);
         inner
     }