Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #[must_use] to core and std constructors #89729

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>() -> Self {
let (size, align) = size_align::<T>();
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/hash/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) })
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl<T> From<T> for OnceCell<T> {
impl<T> OnceCell<T> {
/// Creates a new empty cell.
#[unstable(feature = "once_cell", issue = "74465")]
#[must_use]
pub const fn new() -> OnceCell<T> {
OnceCell { inner: UnsafeCell::new(None) }
}
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ impl<T> MaybeUninit<T> {
/// ```
#[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<T> {
Expand Down Expand Up @@ -349,6 +350,7 @@ impl<T> MaybeUninit<T> {
/// ```
#[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<const LEN: usize>() -> [Self; LEN] {
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
Expand Down Expand Up @@ -391,6 +393,7 @@ impl<T> MaybeUninit<T> {
/// // 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<T> {
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Self> {
if n != 0 {
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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)}
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl<K, V> HashMap<K, V, RandomState> {
/// let mut map: HashMap<&str, i32> = HashMap::new();
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> HashMap<K, V, RandomState> {
Default::default()
Expand All @@ -240,6 +241,7 @@ impl<K, V> HashMap<K, V, RandomState> {
/// 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<K, V, RandomState> {
HashMap::with_capacity_and_hasher(capacity, Default::default())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl<T> HashSet<T, RandomState> {
/// let set: HashSet<i32> = HashSet::new();
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> HashSet<T, RandomState> {
Default::default()
Expand All @@ -144,6 +145,7 @@ impl<T> HashSet<T, RandomState> {
/// assert!(set.capacity() >= 10);
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> {
HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, Default::default()) }
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
Expand Down Expand Up @@ -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) }
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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 }
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl<T: Eq> Eq for SyncOnceCell<T> {}
impl<T> SyncOnceCell<T> {
/// Creates a new empty cell.
#[unstable(feature = "once_cell", issue = "74465")]
#[must_use]
pub const fn new() -> SyncOnceCell<T> {
SyncOnceCell {
once: Once::new(),
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/unix/net/ancillary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand All @@ -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) }
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down