Add #[must_use] to core and std constructors
This commit is contained in:
parent
6928fafe06
commit
5b5c12be1c
21 changed files with 36 additions and 0 deletions
|
@ -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>();
|
||||
|
|
|
@ -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) })
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
@ -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> {
|
||||
|
@ -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.
|
||||
|
@ -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> {
|
||||
|
|
|
@ -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<Self> {
|
||||
if n != 0 {
|
||||
|
|
|
@ -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)}
|
||||
}
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
|
@ -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())
|
||||
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
@ -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()) }
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 = [
|
||||
|
|
|
@ -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 })
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -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 }),
|
||||
|
|
|
@ -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() }
|
||||
}
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue