Skip to content

Suggest mem::forget if mem::ManuallyDrop::new isn't used #75912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion library/core/src/mem/manually_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @matklad you may want to upstream this change to once_cell.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to be notified about changes to lazy.rs you can add yourself to highfive list: https://github.com/rust-lang/highfive/blob/master/highfive/configs/rust-lang/rust.json

inner
}

Expand Down