Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 77be7e4

Browse files
Rollup merge of rust-lang#89729 - jkugelman:must-use-core-std-constructors, r=joshtriplett
Add #[must_use] to core and std constructors Parent issue: rust-lang#89692 r? ``@joshtriplett``
2 parents d7c9693 + 5b5c12b commit 77be7e4

File tree

21 files changed

+36
-0
lines changed

21 files changed

+36
-0
lines changed

library/core/src/alloc/layout.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl Layout {
119119
/// Constructs a `Layout` suitable for holding a value of type `T`.
120120
#[stable(feature = "alloc_layout", since = "1.28.0")]
121121
#[rustc_const_stable(feature = "alloc_layout_const_new", since = "1.42.0")]
122+
#[must_use]
122123
#[inline]
123124
pub const fn new<T>() -> Self {
124125
let (size, align) = size_align::<T>();

library/core/src/hash/sip.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl SipHasher {
157157
since = "1.13.0",
158158
reason = "use `std::collections::hash_map::DefaultHasher` instead"
159159
)]
160+
#[must_use]
160161
pub fn new() -> SipHasher {
161162
SipHasher::new_with_keys(0, 0)
162163
}
@@ -168,6 +169,7 @@ impl SipHasher {
168169
since = "1.13.0",
169170
reason = "use `std::collections::hash_map::DefaultHasher` instead"
170171
)]
172+
#[must_use]
171173
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
172174
SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) })
173175
}

library/core/src/lazy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl<T> From<T> for OnceCell<T> {
8383
impl<T> OnceCell<T> {
8484
/// Creates a new empty cell.
8585
#[unstable(feature = "once_cell", issue = "74465")]
86+
#[must_use]
8687
pub const fn new() -> OnceCell<T> {
8788
OnceCell { inner: UnsafeCell::new(None) }
8889
}

library/core/src/mem/maybe_uninit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ impl<T> MaybeUninit<T> {
312312
/// ```
313313
#[stable(feature = "maybe_uninit", since = "1.36.0")]
314314
#[rustc_const_stable(feature = "const_maybe_uninit", since = "1.36.0")]
315+
#[must_use]
315316
#[inline(always)]
316317
#[rustc_diagnostic_item = "maybe_uninit_uninit"]
317318
pub const fn uninit() -> MaybeUninit<T> {
@@ -349,6 +350,7 @@ impl<T> MaybeUninit<T> {
349350
/// ```
350351
#[unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
351352
#[rustc_const_unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
353+
#[must_use]
352354
#[inline(always)]
353355
pub const fn uninit_array<const LEN: usize>() -> [Self; LEN] {
354356
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
@@ -391,6 +393,7 @@ impl<T> MaybeUninit<T> {
391393
/// // This is undefined behavior. ⚠️
392394
/// ```
393395
#[stable(feature = "maybe_uninit", since = "1.36.0")]
396+
#[must_use]
394397
#[inline]
395398
#[rustc_diagnostic_item = "maybe_uninit_zeroed"]
396399
pub fn zeroed() -> MaybeUninit<T> {

library/core/src/num/nonzero.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ macro_rules! nonzero_integers {
5050
/// The value must not be zero.
5151
#[$stability]
5252
#[$const_new_unchecked_stability]
53+
#[must_use]
5354
#[inline]
5455
pub const unsafe fn new_unchecked(n: $Int) -> Self {
5556
// SAFETY: this is guaranteed to be safe by the caller.
@@ -59,6 +60,7 @@ macro_rules! nonzero_integers {
5960
/// Creates a non-zero if the given value is not zero.
6061
#[$stability]
6162
#[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
63+
#[must_use]
6264
#[inline]
6365
pub const fn new(n: $Int) -> Option<Self> {
6466
if n != 0 {

library/core/src/sync/atomic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ impl AtomicBool {
290290
#[inline]
291291
#[stable(feature = "rust1", since = "1.0.0")]
292292
#[rustc_const_stable(feature = "const_atomic_new", since = "1.24.0")]
293+
#[must_use]
293294
pub const fn new(v: bool) -> AtomicBool {
294295
AtomicBool { v: UnsafeCell::new(v as u8) }
295296
}
@@ -1392,6 +1393,7 @@ macro_rules! atomic_int {
13921393
#[inline]
13931394
#[$stable]
13941395
#[$const_stable]
1396+
#[must_use]
13951397
pub const fn new(v: $int_type) -> Self {
13961398
Self {v: UnsafeCell::new(v)}
13971399
}

library/core/src/task/wake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl RawWaker {
3939
#[rustc_promotable]
4040
#[stable(feature = "futures_api", since = "1.36.0")]
4141
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
42+
#[must_use]
4243
pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
4344
RawWaker { data, vtable }
4445
}

library/core/src/time.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ impl Duration {
181181
#[stable(feature = "duration", since = "1.3.0")]
182182
#[inline]
183183
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
184+
#[must_use]
184185
pub const fn new(secs: u64, nanos: u32) -> Duration {
185186
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
186187
Some(secs) => secs,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ impl<K, V> HashMap<K, V, RandomState> {
223223
/// let mut map: HashMap<&str, i32> = HashMap::new();
224224
/// ```
225225
#[inline]
226+
#[must_use]
226227
#[stable(feature = "rust1", since = "1.0.0")]
227228
pub fn new() -> HashMap<K, V, RandomState> {
228229
Default::default()
@@ -240,6 +241,7 @@ impl<K, V> HashMap<K, V, RandomState> {
240241
/// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
241242
/// ```
242243
#[inline]
244+
#[must_use]
243245
#[stable(feature = "rust1", since = "1.0.0")]
244246
pub fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState> {
245247
HashMap::with_capacity_and_hasher(capacity, Default::default())
@@ -2894,6 +2896,7 @@ impl RandomState {
28942896
#[inline]
28952897
#[allow(deprecated)]
28962898
// rand
2899+
#[must_use]
28972900
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
28982901
pub fn new() -> RandomState {
28992902
// Historically this function did not cache keys from the OS and instead
@@ -2946,6 +2949,7 @@ impl DefaultHasher {
29462949
/// instances created through `new` or `default`.
29472950
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
29482951
#[allow(deprecated)]
2952+
#[must_use]
29492953
pub fn new() -> DefaultHasher {
29502954
DefaultHasher(SipHasher13::new_with_keys(0, 0))
29512955
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl<T> HashSet<T, RandomState> {
126126
/// let set: HashSet<i32> = HashSet::new();
127127
/// ```
128128
#[inline]
129+
#[must_use]
129130
#[stable(feature = "rust1", since = "1.0.0")]
130131
pub fn new() -> HashSet<T, RandomState> {
131132
Default::default()
@@ -144,6 +145,7 @@ impl<T> HashSet<T, RandomState> {
144145
/// assert!(set.capacity() >= 10);
145146
/// ```
146147
#[inline]
148+
#[must_use]
147149
#[stable(feature = "rust1", since = "1.0.0")]
148150
pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> {
149151
HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, Default::default()) }

0 commit comments

Comments
 (0)