Skip to content

Commit 8d5e8b2

Browse files
committed
Remove HashSet::get_or_insert_with
This method is unsound because it allows inserting a key at the "wrong" position in a `HashSet`, which could result in it not appearing it future lookups or being inserted multiple times in the set. Instead, `HashSet::get_or_insert` and `HashSet::get_or_insert_owned` should be preferred.
1 parent e78913b commit 8d5e8b2

File tree

2 files changed

+0
-34
lines changed

2 files changed

+0
-34
lines changed

library/std/src/collections/hash/set.rs

-33
Original file line numberDiff line numberDiff line change
@@ -756,39 +756,6 @@ where
756756
self.base.get_or_insert_owned(value)
757757
}
758758

759-
/// Inserts a value computed from `f` into the set if the given `value` is
760-
/// not present, then returns a reference to the value in the set.
761-
///
762-
/// # Examples
763-
///
764-
/// ```
765-
/// #![feature(hash_set_entry)]
766-
///
767-
/// use std::collections::HashSet;
768-
///
769-
/// let mut set: HashSet<String> = ["cat", "dog", "horse"]
770-
/// .iter().map(|&pet| pet.to_owned()).collect();
771-
///
772-
/// assert_eq!(set.len(), 3);
773-
/// for &pet in &["cat", "dog", "fish"] {
774-
/// let value = set.get_or_insert_with(pet, str::to_owned);
775-
/// assert_eq!(value, pet);
776-
/// }
777-
/// assert_eq!(set.len(), 4); // a new "fish" was inserted
778-
/// ```
779-
#[inline]
780-
#[unstable(feature = "hash_set_entry", issue = "60896")]
781-
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
782-
where
783-
T: Borrow<Q>,
784-
Q: Hash + Eq,
785-
F: FnOnce(&Q) -> T,
786-
{
787-
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
788-
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
789-
self.base.get_or_insert_with(value, f)
790-
}
791-
792759
/// Returns `true` if `self` has no elements in common with `other`.
793760
/// This is equivalent to checking for an empty intersection.
794761
///

tests/rustdoc-js-std/path-ordering.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const EXPECTED = [
55
// ensure hashset::insert comes first
66
{ 'path': 'std::collections::hash_set::HashSet', 'name': 'insert' },
77
{ 'path': 'std::collections::hash_set::HashSet', 'name': 'get_or_insert' },
8-
{ 'path': 'std::collections::hash_set::HashSet', 'name': 'get_or_insert_with' },
98
{ 'path': 'std::collections::hash_set::HashSet', 'name': 'get_or_insert_owned' },
109
],
1110
},

0 commit comments

Comments
 (0)