Skip to content

Commit f3c613e

Browse files
authored
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 0bf9605 + 5b5c12b commit f3c613e

File tree

21 files changed

+36
-0
lines changed

21 files changed

+36
-0
lines changed

library/core/src/alloc/layout.rs

+1
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

+2
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

+1
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

+3
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

+2
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

+2
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

+1
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

+1
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

+4
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())
@@ -2891,6 +2893,7 @@ impl RandomState {
28912893
#[inline]
28922894
#[allow(deprecated)]
28932895
// rand
2896+
#[must_use]
28942897
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
28952898
pub fn new() -> RandomState {
28962899
// Historically this function did not cache keys from the OS and instead
@@ -2943,6 +2946,7 @@ impl DefaultHasher {
29432946
/// instances created through `new` or `default`.
29442947
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
29452948
#[allow(deprecated)]
2949+
#[must_use]
29462950
pub fn new() -> DefaultHasher {
29472951
DefaultHasher(SipHasher13::new_with_keys(0, 0))
29482952
}

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

+2
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()) }

library/std/src/ffi/os_str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl OsString {
119119
/// let os_string = OsString::new();
120120
/// ```
121121
#[stable(feature = "rust1", since = "1.0.0")]
122+
#[must_use]
122123
#[inline]
123124
pub fn new() -> OsString {
124125
OsString { inner: Buf::from_string(String::new()) }
@@ -199,6 +200,7 @@ impl OsString {
199200
/// assert_eq!(capacity, os_string.capacity());
200201
/// ```
201202
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
203+
#[must_use]
202204
#[inline]
203205
pub fn with_capacity(capacity: usize) -> OsString {
204206
OsString { inner: Buf::with_capacity(capacity) }

library/std/src/fs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ impl OpenOptions {
744744
/// let file = options.read(true).open("foo.txt");
745745
/// ```
746746
#[stable(feature = "rust1", since = "1.0.0")]
747+
#[must_use]
747748
pub fn new() -> Self {
748749
OpenOptions(fs_imp::OpenOptions::new())
749750
}
@@ -2184,6 +2185,7 @@ impl DirBuilder {
21842185
/// let builder = DirBuilder::new();
21852186
/// ```
21862187
#[stable(feature = "dir_builder", since = "1.6.0")]
2188+
#[must_use]
21872189
pub fn new() -> DirBuilder {
21882190
DirBuilder { inner: fs_imp::DirBuilder::new(), recursive: false }
21892191
}

library/std/src/io/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,7 @@ impl<'a> IoSlice<'a> {
12061206
///
12071207
/// Panics on Windows if the slice is larger than 4GB.
12081208
#[stable(feature = "iovec", since = "1.36.0")]
1209+
#[must_use]
12091210
#[inline]
12101211
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
12111212
IoSlice(sys::io::IoSlice::new(buf))

library/std/src/lazy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl<T: Eq> Eq for SyncOnceCell<T> {}
171171
impl<T> SyncOnceCell<T> {
172172
/// Creates a new empty cell.
173173
#[unstable(feature = "once_cell", issue = "74465")]
174+
#[must_use]
174175
pub const fn new() -> SyncOnceCell<T> {
175176
SyncOnceCell {
176177
once: Once::new(),

library/std/src/net/addr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl SocketAddr {
131131
/// assert_eq!(socket.port(), 8080);
132132
/// ```
133133
#[stable(feature = "ip_addr", since = "1.7.0")]
134+
#[must_use]
134135
pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
135136
match ip {
136137
IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
@@ -272,6 +273,7 @@ impl SocketAddrV4 {
272273
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
273274
/// ```
274275
#[stable(feature = "rust1", since = "1.0.0")]
276+
#[must_use]
275277
pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
276278
SocketAddrV4 {
277279
inner: c::sockaddr_in {
@@ -368,6 +370,7 @@ impl SocketAddrV6 {
368370
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
369371
/// ```
370372
#[stable(feature = "rust1", since = "1.0.0")]
373+
#[must_use]
371374
pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 {
372375
SocketAddrV6 {
373376
inner: c::sockaddr_in6 {

library/std/src/net/ip.rs

+2
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ impl Ipv4Addr {
442442
/// ```
443443
#[rustc_const_stable(feature = "const_ipv4", since = "1.32.0")]
444444
#[stable(feature = "rust1", since = "1.0.0")]
445+
#[must_use]
445446
#[inline]
446447
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
447448
// `s_addr` is stored as BE on all machine and the array is in BE order.
@@ -1192,6 +1193,7 @@ impl Ipv6Addr {
11921193
/// ```
11931194
#[rustc_const_stable(feature = "const_ipv6", since = "1.32.0")]
11941195
#[stable(feature = "rust1", since = "1.0.0")]
1196+
#[must_use]
11951197
#[inline]
11961198
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
11971199
let addr16 = [

library/std/src/os/unix/net/ancillary.rs

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ impl SocketCred {
189189
///
190190
/// PID, UID and GID is set to 0.
191191
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
192+
#[must_use]
192193
pub fn new() -> SocketCred {
193194
SocketCred(libc::ucred { pid: 0, uid: 0, gid: 0 })
194195
}

library/std/src/path.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ impl PathBuf {
11451145
/// let path = PathBuf::new();
11461146
/// ```
11471147
#[stable(feature = "rust1", since = "1.0.0")]
1148+
#[must_use]
11481149
#[inline]
11491150
pub fn new() -> PathBuf {
11501151
PathBuf { inner: OsString::new() }
@@ -1169,6 +1170,7 @@ impl PathBuf {
11691170
///
11701171
/// [`with_capacity`]: OsString::with_capacity
11711172
#[stable(feature = "path_buf_capacity", since = "1.44.0")]
1173+
#[must_use]
11721174
#[inline]
11731175
pub fn with_capacity(capacity: usize) -> PathBuf {
11741176
PathBuf { inner: OsString::with_capacity(capacity) }

library/std/src/sync/barrier.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl Barrier {
8080
/// let barrier = Barrier::new(10);
8181
/// ```
8282
#[stable(feature = "rust1", since = "1.0.0")]
83+
#[must_use]
8384
pub fn new(n: usize) -> Barrier {
8485
Barrier {
8586
lock: Mutex::new(BarrierState { count: 0, generation_id: 0 }),

library/std/src/sync/condvar.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl Condvar {
121121
/// let condvar = Condvar::new();
122122
/// ```
123123
#[stable(feature = "rust1", since = "1.0.0")]
124+
#[must_use]
124125
pub fn new() -> Condvar {
125126
Condvar { inner: sys::Condvar::new() }
126127
}

library/std/src/sync/once.rs

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl Once {
186186
#[inline]
187187
#[stable(feature = "once_new", since = "1.2.0")]
188188
#[rustc_const_stable(feature = "const_once_new", since = "1.32.0")]
189+
#[must_use]
189190
pub const fn new() -> Once {
190191
Once { state_and_queue: AtomicUsize::new(INCOMPLETE), _marker: marker::PhantomData }
191192
}

0 commit comments

Comments
 (0)