Skip to content
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

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

Merged
merged 1 commit into from
May 7, 2023
Merged
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: 2 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

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

This change makes sense to me, though -- the forget+replace is a residue from when this was unstably const, before that was undone.

}

// SAFETY: a `None` variant for `self` would have been replaced by a `Some`
Expand Down