From 5b5c12be1cf2b2531a1cbf7097d2b5102cfaccd8 Mon Sep 17 00:00:00 2001 From: John Kugelman Date: Sun, 10 Oct 2021 02:44:26 -0400 Subject: [PATCH] Add #[must_use] to core and std constructors --- library/core/src/alloc/layout.rs | 1 + library/core/src/hash/sip.rs | 2 ++ library/core/src/lazy.rs | 1 + library/core/src/mem/maybe_uninit.rs | 3 +++ library/core/src/num/nonzero.rs | 2 ++ library/core/src/sync/atomic.rs | 2 ++ library/core/src/task/wake.rs | 1 + library/core/src/time.rs | 1 + library/std/src/collections/hash/map.rs | 4 ++++ library/std/src/collections/hash/set.rs | 2 ++ library/std/src/ffi/os_str.rs | 2 ++ library/std/src/fs.rs | 2 ++ library/std/src/io/mod.rs | 1 + library/std/src/lazy.rs | 1 + library/std/src/net/addr.rs | 3 +++ library/std/src/net/ip.rs | 2 ++ library/std/src/os/unix/net/ancillary.rs | 1 + library/std/src/path.rs | 2 ++ library/std/src/sync/barrier.rs | 1 + library/std/src/sync/condvar.rs | 1 + library/std/src/sync/once.rs | 1 + 21 files changed, 36 insertions(+) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index ccf6e420de7a9..c98aa38e3df29 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -119,6 +119,7 @@ impl Layout { /// Constructs a `Layout` suitable for holding a value of type `T`. #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_stable(feature = "alloc_layout_const_new", since = "1.42.0")] + #[must_use] #[inline] pub const fn new() -> Self { let (size, align) = size_align::(); diff --git a/library/core/src/hash/sip.rs b/library/core/src/hash/sip.rs index 6178b0af137e8..b9443e30074b0 100644 --- a/library/core/src/hash/sip.rs +++ b/library/core/src/hash/sip.rs @@ -157,6 +157,7 @@ impl SipHasher { since = "1.13.0", reason = "use `std::collections::hash_map::DefaultHasher` instead" )] + #[must_use] pub fn new() -> SipHasher { SipHasher::new_with_keys(0, 0) } @@ -168,6 +169,7 @@ impl SipHasher { since = "1.13.0", reason = "use `std::collections::hash_map::DefaultHasher` instead" )] + #[must_use] pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher { SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) }) } diff --git a/library/core/src/lazy.rs b/library/core/src/lazy.rs index e6bea462fa99a..d109141216aef 100644 --- a/library/core/src/lazy.rs +++ b/library/core/src/lazy.rs @@ -83,6 +83,7 @@ impl From for OnceCell { impl OnceCell { /// Creates a new empty cell. #[unstable(feature = "once_cell", issue = "74465")] + #[must_use] pub const fn new() -> OnceCell { OnceCell { inner: UnsafeCell::new(None) } } diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 9c88a62336161..b9772eaa67813 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -312,6 +312,7 @@ impl MaybeUninit { /// ``` #[stable(feature = "maybe_uninit", since = "1.36.0")] #[rustc_const_stable(feature = "const_maybe_uninit", since = "1.36.0")] + #[must_use] #[inline(always)] #[rustc_diagnostic_item = "maybe_uninit_uninit"] pub const fn uninit() -> MaybeUninit { @@ -349,6 +350,7 @@ impl MaybeUninit { /// ``` #[unstable(feature = "maybe_uninit_uninit_array", issue = "none")] #[rustc_const_unstable(feature = "maybe_uninit_uninit_array", issue = "none")] + #[must_use] #[inline(always)] pub const fn uninit_array() -> [Self; LEN] { // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid. @@ -391,6 +393,7 @@ impl MaybeUninit { /// // This is undefined behavior. ⚠️ /// ``` #[stable(feature = "maybe_uninit", since = "1.36.0")] + #[must_use] #[inline] #[rustc_diagnostic_item = "maybe_uninit_zeroed"] pub fn zeroed() -> MaybeUninit { diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index e44597279baf2..5b50b13b23e37 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -50,6 +50,7 @@ macro_rules! nonzero_integers { /// The value must not be zero. #[$stability] #[$const_new_unchecked_stability] + #[must_use] #[inline] pub const unsafe fn new_unchecked(n: $Int) -> Self { // SAFETY: this is guaranteed to be safe by the caller. @@ -59,6 +60,7 @@ macro_rules! nonzero_integers { /// Creates a non-zero if the given value is not zero. #[$stability] #[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")] + #[must_use] #[inline] pub const fn new(n: $Int) -> Option { if n != 0 { diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs index d9de37e9c5197..b07752116e514 100644 --- a/library/core/src/sync/atomic.rs +++ b/library/core/src/sync/atomic.rs @@ -290,6 +290,7 @@ impl AtomicBool { #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_atomic_new", since = "1.24.0")] + #[must_use] pub const fn new(v: bool) -> AtomicBool { AtomicBool { v: UnsafeCell::new(v as u8) } } @@ -1392,6 +1393,7 @@ macro_rules! atomic_int { #[inline] #[$stable] #[$const_stable] + #[must_use] pub const fn new(v: $int_type) -> Self { Self {v: UnsafeCell::new(v)} } diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index b775e022a54b4..2b540716154a1 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -39,6 +39,7 @@ impl RawWaker { #[rustc_promotable] #[stable(feature = "futures_api", since = "1.36.0")] #[rustc_const_stable(feature = "futures_api", since = "1.36.0")] + #[must_use] pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker { RawWaker { data, vtable } } diff --git a/library/core/src/time.rs b/library/core/src/time.rs index d1533b8d67a6b..d2757ef71213f 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -181,6 +181,7 @@ impl Duration { #[stable(feature = "duration", since = "1.3.0")] #[inline] #[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")] + #[must_use] pub const fn new(secs: u64, nanos: u32) -> Duration { let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { Some(secs) => secs, diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 528bb1bf6e9f9..7fa2e3ed3f6ac 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -223,6 +223,7 @@ impl HashMap { /// let mut map: HashMap<&str, i32> = HashMap::new(); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn new() -> HashMap { Default::default() @@ -240,6 +241,7 @@ impl HashMap { /// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn with_capacity(capacity: usize) -> HashMap { HashMap::with_capacity_and_hasher(capacity, Default::default()) @@ -2891,6 +2893,7 @@ impl RandomState { #[inline] #[allow(deprecated)] // rand + #[must_use] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] pub fn new() -> RandomState { // Historically this function did not cache keys from the OS and instead @@ -2943,6 +2946,7 @@ impl DefaultHasher { /// instances created through `new` or `default`. #[stable(feature = "hashmap_default_hasher", since = "1.13.0")] #[allow(deprecated)] + #[must_use] pub fn new() -> DefaultHasher { DefaultHasher(SipHasher13::new_with_keys(0, 0)) } diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index dcfe322095082..5804701892e6e 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -126,6 +126,7 @@ impl HashSet { /// let set: HashSet = HashSet::new(); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn new() -> HashSet { Default::default() @@ -144,6 +145,7 @@ impl HashSet { /// assert!(set.capacity() >= 10); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn with_capacity(capacity: usize) -> HashSet { HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, Default::default()) } diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index c305519dd4461..e4ad6cba88046 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -119,6 +119,7 @@ impl OsString { /// let os_string = OsString::new(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub fn new() -> OsString { OsString { inner: Buf::from_string(String::new()) } @@ -199,6 +200,7 @@ impl OsString { /// assert_eq!(capacity, os_string.capacity()); /// ``` #[stable(feature = "osstring_simple_functions", since = "1.9.0")] + #[must_use] #[inline] pub fn with_capacity(capacity: usize) -> OsString { OsString { inner: Buf::with_capacity(capacity) } diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 726c855c4fd36..85d350729239c 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -744,6 +744,7 @@ impl OpenOptions { /// let file = options.read(true).open("foo.txt"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn new() -> Self { OpenOptions(fs_imp::OpenOptions::new()) } @@ -2184,6 +2185,7 @@ impl DirBuilder { /// let builder = DirBuilder::new(); /// ``` #[stable(feature = "dir_builder", since = "1.6.0")] + #[must_use] pub fn new() -> DirBuilder { DirBuilder { inner: fs_imp::DirBuilder::new(), recursive: false } } diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 8c71138aa231d..abe29ba0f7caa 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1206,6 +1206,7 @@ impl<'a> IoSlice<'a> { /// /// Panics on Windows if the slice is larger than 4GB. #[stable(feature = "iovec", since = "1.36.0")] + #[must_use] #[inline] pub fn new(buf: &'a [u8]) -> IoSlice<'a> { IoSlice(sys::io::IoSlice::new(buf)) diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs index 5afdb799f0c74..d7450962359df 100644 --- a/library/std/src/lazy.rs +++ b/library/std/src/lazy.rs @@ -171,6 +171,7 @@ impl Eq for SyncOnceCell {} impl SyncOnceCell { /// Creates a new empty cell. #[unstable(feature = "once_cell", issue = "74465")] + #[must_use] pub const fn new() -> SyncOnceCell { SyncOnceCell { once: Once::new(), diff --git a/library/std/src/net/addr.rs b/library/std/src/net/addr.rs index f4ebcd53a25f7..cd2007cc2cb42 100644 --- a/library/std/src/net/addr.rs +++ b/library/std/src/net/addr.rs @@ -131,6 +131,7 @@ impl SocketAddr { /// assert_eq!(socket.port(), 8080); /// ``` #[stable(feature = "ip_addr", since = "1.7.0")] + #[must_use] pub fn new(ip: IpAddr, port: u16) -> SocketAddr { match ip { IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)), @@ -272,6 +273,7 @@ impl SocketAddrV4 { /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 { SocketAddrV4 { inner: c::sockaddr_in { @@ -368,6 +370,7 @@ impl SocketAddrV6 { /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 { SocketAddrV6 { inner: c::sockaddr_in6 { diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index c05a32742ba1e..99922eef61f10 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -442,6 +442,7 @@ impl Ipv4Addr { /// ``` #[rustc_const_stable(feature = "const_ipv4", since = "1.32.0")] #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr { // `s_addr` is stored as BE on all machine and the array is in BE order. @@ -1192,6 +1193,7 @@ impl Ipv6Addr { /// ``` #[rustc_const_stable(feature = "const_ipv6", since = "1.32.0")] #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr { let addr16 = [ diff --git a/library/std/src/os/unix/net/ancillary.rs b/library/std/src/os/unix/net/ancillary.rs index 1f9d42812ecc7..57bb61903c147 100644 --- a/library/std/src/os/unix/net/ancillary.rs +++ b/library/std/src/os/unix/net/ancillary.rs @@ -189,6 +189,7 @@ impl SocketCred { /// /// PID, UID and GID is set to 0. #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] + #[must_use] pub fn new() -> SocketCred { SocketCred(libc::ucred { pid: 0, uid: 0, gid: 0 }) } diff --git a/library/std/src/path.rs b/library/std/src/path.rs index a45ecf6ea8c63..c5476c3d5267b 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1145,6 +1145,7 @@ impl PathBuf { /// let path = PathBuf::new(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub fn new() -> PathBuf { PathBuf { inner: OsString::new() } @@ -1169,6 +1170,7 @@ impl PathBuf { /// /// [`with_capacity`]: OsString::with_capacity #[stable(feature = "path_buf_capacity", since = "1.44.0")] + #[must_use] #[inline] pub fn with_capacity(capacity: usize) -> PathBuf { PathBuf { inner: OsString::with_capacity(capacity) } diff --git a/library/std/src/sync/barrier.rs b/library/std/src/sync/barrier.rs index a17b82f82e8c2..0e28a2d166f13 100644 --- a/library/std/src/sync/barrier.rs +++ b/library/std/src/sync/barrier.rs @@ -80,6 +80,7 @@ impl Barrier { /// let barrier = Barrier::new(10); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn new(n: usize) -> Barrier { Barrier { lock: Mutex::new(BarrierState { count: 0, generation_id: 0 }), diff --git a/library/std/src/sync/condvar.rs b/library/std/src/sync/condvar.rs index 00a4afc570519..d8aca9651b867 100644 --- a/library/std/src/sync/condvar.rs +++ b/library/std/src/sync/condvar.rs @@ -121,6 +121,7 @@ impl Condvar { /// let condvar = Condvar::new(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn new() -> Condvar { Condvar { inner: sys::Condvar::new() } } diff --git a/library/std/src/sync/once.rs b/library/std/src/sync/once.rs index a2e935a0cebc4..1710c0053934a 100644 --- a/library/std/src/sync/once.rs +++ b/library/std/src/sync/once.rs @@ -186,6 +186,7 @@ impl Once { #[inline] #[stable(feature = "once_new", since = "1.2.0")] #[rustc_const_stable(feature = "const_once_new", since = "1.32.0")] + #[must_use] pub const fn new() -> Once { Once { state_and_queue: AtomicUsize::new(INCOMPLETE), _marker: marker::PhantomData } }