Skip to content

Commit ed9d772

Browse files
Rollup merge of #153204 - xtqqczze:must-use-map, r=Amanieu,joboet
Add `#[must_use]` attribute to `HashMap` and `HashSet` constructors - `new_in` - `with_capacity_and_hasher` - `with_capacity_and_hasher_in` - `with_hasher` - `with_hasher_in` See also: #89692
2 parents 5ff104c + dda4f84 commit ed9d772

4 files changed

Lines changed: 10 additions & 0 deletions

File tree

library/alloc/src/collections/btree/map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
693693
/// map.insert(1, "a");
694694
/// ```
695695
#[unstable(feature = "btreemap_alloc", issue = "32838")]
696+
#[must_use]
696697
pub const fn new_in(alloc: A) -> BTreeMap<K, V, A> {
697698
BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(alloc), _marker: PhantomData }
698699
}

library/alloc/src/collections/btree/set.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
361361
/// let mut set: BTreeSet<i32> = BTreeSet::new_in(Global);
362362
/// ```
363363
#[unstable(feature = "btreemap_alloc", issue = "32838")]
364+
#[must_use]
364365
pub const fn new_in(alloc: A) -> BTreeSet<T, A> {
365366
BTreeSet { map: BTreeMap::new_in(alloc) }
366367
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ impl<K, V, S> HashMap<K, V, S> {
357357
/// map.insert(1, 2);
358358
/// ```
359359
#[inline]
360+
#[must_use]
360361
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
361362
#[rustc_const_stable(feature = "const_collections_with_hasher", since = "1.85.0")]
362363
pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
@@ -389,6 +390,7 @@ impl<K, V, S> HashMap<K, V, S> {
389390
/// map.insert(1, 2);
390391
/// ```
391392
#[inline]
393+
#[must_use]
392394
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
393395
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashMap<K, V, S> {
394396
HashMap { base: base::HashMap::with_capacity_and_hasher(capacity, hasher) }
@@ -409,6 +411,7 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
409411
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
410412
/// the `HashMap` to be useful, see its documentation for details.
411413
#[inline]
414+
#[must_use]
412415
#[unstable(feature = "allocator_api", issue = "32838")]
413416
pub fn with_hasher_in(hash_builder: S, alloc: A) -> Self {
414417
HashMap { base: base::HashMap::with_hasher_in(hash_builder, alloc) }
@@ -430,6 +433,7 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
430433
/// the `HashMap` to be useful, see its documentation for details.
431434
///
432435
#[inline]
436+
#[must_use]
433437
#[unstable(feature = "allocator_api", issue = "32838")]
434438
pub fn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self {
435439
HashMap { base: base::HashMap::with_capacity_and_hasher_in(capacity, hash_builder, alloc) }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ impl<T, S> HashSet<T, S> {
229229
/// set.insert(2);
230230
/// ```
231231
#[inline]
232+
#[must_use]
232233
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
233234
#[rustc_const_stable(feature = "const_collections_with_hasher", since = "1.85.0")]
234235
pub const fn with_hasher(hasher: S) -> HashSet<T, S> {
@@ -261,6 +262,7 @@ impl<T, S> HashSet<T, S> {
261262
/// set.insert(1);
262263
/// ```
263264
#[inline]
265+
#[must_use]
264266
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
265267
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> {
266268
HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, hasher) }
@@ -281,6 +283,7 @@ impl<T, S, A: Allocator> HashSet<T, S, A> {
281283
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
282284
/// the `HashSet` to be useful, see its documentation for details.
283285
#[inline]
286+
#[must_use]
284287
#[unstable(feature = "allocator_api", issue = "32838")]
285288
pub fn with_hasher_in(hasher: S, alloc: A) -> HashSet<T, S, A> {
286289
HashSet { base: base::HashSet::with_hasher_in(hasher, alloc) }
@@ -301,6 +304,7 @@ impl<T, S, A: Allocator> HashSet<T, S, A> {
301304
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
302305
/// the `HashSet` to be useful, see its documentation for details.
303306
#[inline]
307+
#[must_use]
304308
#[unstable(feature = "allocator_api", issue = "32838")]
305309
pub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> HashSet<T, S, A> {
306310
HashSet { base: base::HashSet::with_capacity_and_hasher_in(capacity, hasher, alloc) }

0 commit comments

Comments
 (0)