Skip to content

Commit 48717b6

Browse files
committed
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)
2 parents 1f2dd3b + f302407 commit 48717b6

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

library/core/src/mem/manually_drop.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ impl<T> ManuallyDrop<T> {
7474
///
7575
/// ```rust
7676
/// use std::mem::ManuallyDrop;
77-
/// ManuallyDrop::new(Box::new(()));
77+
/// let mut x = ManuallyDrop::new(String::from("Hello World!"));
78+
/// x.truncate(5); // You can still safely operate on the value
79+
/// assert_eq!(*x, "Hello");
80+
/// // But `Drop` will not be run here
7881
/// ```
82+
#[must_use = "if you don't need the wrapper, you can use `mem::forget` instead"]
7983
#[stable(feature = "manually_drop", since = "1.20.0")]
8084
#[rustc_const_stable(feature = "const_manually_drop", since = "1.36.0")]
8185
#[inline(always)]

library/core/src/mem/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub use crate::intrinsics::transmute;
145145
#[rustc_const_stable(feature = "const_forget", since = "1.46.0")]
146146
#[stable(feature = "rust1", since = "1.0.0")]
147147
pub const fn forget<T>(t: T) {
148-
ManuallyDrop::new(t);
148+
let _ = ManuallyDrop::new(t);
149149
}
150150

151151
/// Like [`forget`], but also accepts unsized values.

library/std/src/lazy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<T> SyncOnceCell<T> {
293293

294294
// Don't drop this `SyncOnceCell`. We just moved out one of the fields, but didn't set
295295
// the state to uninitialized.
296-
mem::ManuallyDrop::new(self);
296+
mem::forget(self);
297297
inner
298298
}
299299

0 commit comments

Comments
 (0)