Skip to content

Commit e0a8eb4

Browse files
authored
Merge pull request #697 from xtqqczze/must-use-constructors
Add `#[must_use]` to constructors
2 parents 6758b14 + f8e2b06 commit e0a8eb4

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/map.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl<K, V> HashMap<K, V, DefaultHashBuilder> {
267267
/// assert_eq!(map.len(), 0);
268268
/// assert_eq!(map.capacity(), 0);
269269
/// ```
270+
#[must_use]
270271
#[cfg_attr(feature = "inline-more", inline)]
271272
pub fn new() -> Self {
272273
Self::default()
@@ -296,6 +297,7 @@ impl<K, V> HashMap<K, V, DefaultHashBuilder> {
296297
/// assert_eq!(map.len(), 0);
297298
/// assert!(map.capacity() >= 10);
298299
/// ```
300+
#[must_use]
299301
#[cfg_attr(feature = "inline-more", inline)]
300302
pub fn with_capacity(capacity: usize) -> Self {
301303
Self::with_capacity_and_hasher(capacity, DefaultHashBuilder::default())
@@ -342,6 +344,7 @@ impl<K, V, A: Allocator> HashMap<K, V, DefaultHashBuilder, A> {
342344
/// // And it also allocates some capacity
343345
/// assert!(map.capacity() > 1);
344346
/// ```
347+
#[must_use]
345348
#[cfg_attr(feature = "inline-more", inline)]
346349
pub fn new_in(alloc: A) -> Self {
347350
Self::with_hasher_in(DefaultHashBuilder::default(), alloc)
@@ -390,6 +393,7 @@ impl<K, V, A: Allocator> HashMap<K, V, DefaultHashBuilder, A> {
390393
/// // But its capacity isn't changed
391394
/// assert_eq!(map.capacity(), empty_map_capacity)
392395
/// ```
396+
#[must_use]
393397
#[cfg_attr(feature = "inline-more", inline)]
394398
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
395399
Self::with_capacity_and_hasher_in(capacity, DefaultHashBuilder::default(), alloc)
@@ -429,6 +433,7 @@ impl<K, V, S> HashMap<K, V, S> {
429433
///
430434
/// map.insert(1, 2);
431435
/// ```
436+
#[must_use]
432437
#[cfg_attr(feature = "inline-more", inline)]
433438
#[cfg_attr(feature = "rustc-dep-of-std", rustc_const_stable_indirect)]
434439
pub const fn with_hasher(hash_builder: S) -> Self {
@@ -470,6 +475,7 @@ impl<K, V, S> HashMap<K, V, S> {
470475
///
471476
/// map.insert(1, 2);
472477
/// ```
478+
#[must_use]
473479
#[cfg_attr(feature = "inline-more", inline)]
474480
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
475481
Self {
@@ -512,6 +518,7 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
512518
/// let mut map = HashMap::with_hasher(s);
513519
/// map.insert(1, 2);
514520
/// ```
521+
#[must_use]
515522
#[cfg_attr(feature = "inline-more", inline)]
516523
#[cfg_attr(feature = "rustc-dep-of-std", rustc_const_stable_indirect)]
517524
pub const fn with_hasher_in(hash_builder: S, alloc: A) -> Self {
@@ -547,6 +554,7 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
547554
/// let mut map = HashMap::with_capacity_and_hasher(10, s);
548555
/// map.insert(1, 2);
549556
/// ```
557+
#[must_use]
550558
#[cfg_attr(feature = "inline-more", inline)]
551559
pub fn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self {
552560
Self {

src/set.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ impl<T> HashSet<T, DefaultHashBuilder> {
148148
/// use hashbrown::HashSet;
149149
/// let set: HashSet<i32> = HashSet::new();
150150
/// ```
151+
#[must_use]
151152
#[cfg_attr(feature = "inline-more", inline)]
152153
pub fn new() -> Self {
153154
Self {
@@ -178,6 +179,7 @@ impl<T> HashSet<T, DefaultHashBuilder> {
178179
/// let set: HashSet<i32> = HashSet::with_capacity(10);
179180
/// assert!(set.capacity() >= 10);
180181
/// ```
182+
#[must_use]
181183
#[cfg_attr(feature = "inline-more", inline)]
182184
pub fn with_capacity(capacity: usize) -> Self {
183185
Self {
@@ -210,6 +212,7 @@ impl<T: Hash + Eq, A: Allocator> HashSet<T, DefaultHashBuilder, A> {
210212
/// use hashbrown::HashSet;
211213
/// let set: HashSet<i32> = HashSet::new();
212214
/// ```
215+
#[must_use]
213216
#[cfg_attr(feature = "inline-more", inline)]
214217
pub fn new_in(alloc: A) -> Self {
215218
Self {
@@ -240,6 +243,7 @@ impl<T: Hash + Eq, A: Allocator> HashSet<T, DefaultHashBuilder, A> {
240243
/// let set: HashSet<i32> = HashSet::with_capacity(10);
241244
/// assert!(set.capacity() >= 10);
242245
/// ```
246+
#[must_use]
243247
#[cfg_attr(feature = "inline-more", inline)]
244248
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
245249
Self {
@@ -455,6 +459,7 @@ impl<T, S> HashSet<T, S, Global> {
455459
/// let mut set = HashSet::with_hasher(s);
456460
/// set.insert(2);
457461
/// ```
462+
#[must_use]
458463
#[cfg_attr(feature = "inline-more", inline)]
459464
#[cfg_attr(feature = "rustc-dep-of-std", rustc_const_stable_indirect)]
460465
pub const fn with_hasher(hasher: S) -> Self {
@@ -492,6 +497,7 @@ impl<T, S> HashSet<T, S, Global> {
492497
/// let mut set = HashSet::with_capacity_and_hasher(10, s);
493498
/// set.insert(1);
494499
/// ```
500+
#[must_use]
495501
#[cfg_attr(feature = "inline-more", inline)]
496502
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
497503
Self {
@@ -539,6 +545,7 @@ where
539545
/// let mut set = HashSet::with_hasher(s);
540546
/// set.insert(2);
541547
/// ```
548+
#[must_use]
542549
#[cfg_attr(feature = "inline-more", inline)]
543550
#[cfg_attr(feature = "rustc-dep-of-std", rustc_const_stable_indirect)]
544551
pub const fn with_hasher_in(hasher: S, alloc: A) -> Self {
@@ -576,6 +583,7 @@ where
576583
/// let mut set = HashSet::with_capacity_and_hasher(10, s);
577584
/// set.insert(1);
578585
/// ```
586+
#[must_use]
579587
#[cfg_attr(feature = "inline-more", inline)]
580588
pub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> Self {
581589
Self {

src/table.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl<T> HashTable<T, Global> {
6666
/// assert_eq!(table.len(), 0);
6767
/// assert_eq!(table.capacity(), 0);
6868
/// ```
69+
#[must_use]
6970
pub const fn new() -> Self {
7071
Self {
7172
raw: RawTable::new(),
@@ -85,6 +86,7 @@ impl<T> HashTable<T, Global> {
8586
/// assert_eq!(table.len(), 0);
8687
/// assert!(table.capacity() >= 10);
8788
/// ```
89+
#[must_use]
8890
pub fn with_capacity(capacity: usize) -> Self {
8991
Self {
9092
raw: RawTable::with_capacity(capacity),
@@ -133,6 +135,7 @@ where
133135
/// # test()
134136
/// # }
135137
/// ```
138+
#[must_use]
136139
pub const fn new_in(alloc: A) -> Self {
137140
Self {
138141
raw: RawTable::new_in(alloc),
@@ -181,6 +184,7 @@ where
181184
/// # test()
182185
/// # }
183186
/// ```
187+
#[must_use]
184188
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
185189
Self {
186190
raw: RawTable::with_capacity_in(capacity, alloc),

0 commit comments

Comments
 (0)