Skip to content

Commit 887503a

Browse files
committed
Add #[must_use] to mem/ptr functions
1 parent 0446743 commit 887503a

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

library/core/src/mem/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ pub fn forget_unsized<T: ?Sized>(t: T) {
296296
///
297297
/// [alignment]: align_of
298298
#[inline(always)]
299+
#[must_use]
299300
#[stable(feature = "rust1", since = "1.0.0")]
300301
#[rustc_promotable]
301302
#[rustc_const_stable(feature = "const_size_of", since = "1.24.0")]
@@ -324,6 +325,7 @@ pub const fn size_of<T>() -> usize {
324325
/// assert_eq!(13, mem::size_of_val(y));
325326
/// ```
326327
#[inline]
328+
#[must_use]
327329
#[stable(feature = "rust1", since = "1.0.0")]
328330
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
329331
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_size_of_val")]
@@ -373,6 +375,7 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
373375
/// assert_eq!(13, unsafe { mem::size_of_val_raw(y) });
374376
/// ```
375377
#[inline]
378+
#[must_use]
376379
#[unstable(feature = "layout_for_ptr", issue = "69835")]
377380
#[rustc_const_unstable(feature = "const_size_of_val_raw", issue = "46571")]
378381
pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
@@ -397,6 +400,7 @@ pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
397400
/// assert_eq!(4, mem::min_align_of::<i32>());
398401
/// ```
399402
#[inline]
403+
#[must_use]
400404
#[stable(feature = "rust1", since = "1.0.0")]
401405
#[rustc_deprecated(reason = "use `align_of` instead", since = "1.2.0")]
402406
pub fn min_align_of<T>() -> usize {
@@ -418,6 +422,7 @@ pub fn min_align_of<T>() -> usize {
418422
/// assert_eq!(4, mem::min_align_of_val(&5i32));
419423
/// ```
420424
#[inline]
425+
#[must_use]
421426
#[stable(feature = "rust1", since = "1.0.0")]
422427
#[rustc_deprecated(reason = "use `align_of_val` instead", since = "1.2.0")]
423428
pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
@@ -441,6 +446,7 @@ pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
441446
/// assert_eq!(4, mem::align_of::<i32>());
442447
/// ```
443448
#[inline(always)]
449+
#[must_use]
444450
#[stable(feature = "rust1", since = "1.0.0")]
445451
#[rustc_promotable]
446452
#[rustc_const_stable(feature = "const_align_of", since = "1.24.0")]
@@ -462,6 +468,7 @@ pub const fn align_of<T>() -> usize {
462468
/// assert_eq!(4, mem::align_of_val(&5i32));
463469
/// ```
464470
#[inline]
471+
#[must_use]
465472
#[stable(feature = "rust1", since = "1.0.0")]
466473
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
467474
#[allow(deprecated)]
@@ -507,6 +514,7 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
507514
/// assert_eq!(4, unsafe { mem::align_of_val_raw(&5i32) });
508515
/// ```
509516
#[inline]
517+
#[must_use]
510518
#[unstable(feature = "layout_for_ptr", issue = "69835")]
511519
#[rustc_const_unstable(feature = "const_align_of_val_raw", issue = "46571")]
512520
pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
@@ -571,6 +579,7 @@ pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
571579
/// }
572580
/// ```
573581
#[inline]
582+
#[must_use]
574583
#[stable(feature = "needs_drop", since = "1.21.0")]
575584
#[rustc_const_stable(feature = "const_needs_drop", since = "1.36.0")]
576585
#[rustc_diagnostic_item = "needs_drop"]
@@ -618,6 +627,7 @@ pub const fn needs_drop<T>() -> bool {
618627
/// let _y: fn() = unsafe { mem::zeroed() }; // And again!
619628
/// ```
620629
#[inline(always)]
630+
#[must_use]
621631
#[stable(feature = "rust1", since = "1.0.0")]
622632
#[allow(deprecated_in_future)]
623633
#[allow(deprecated)]
@@ -653,6 +663,7 @@ pub unsafe fn zeroed<T>() -> T {
653663
/// [assume_init]: MaybeUninit::assume_init
654664
/// [inv]: MaybeUninit#initialization-invariant
655665
#[inline(always)]
666+
#[must_use]
656667
#[rustc_deprecated(since = "1.39.0", reason = "use `mem::MaybeUninit` instead")]
657668
#[stable(feature = "rust1", since = "1.0.0")]
658669
#[allow(deprecated_in_future)]
@@ -938,6 +949,7 @@ pub fn drop<T>(_x: T) {}
938949
/// assert_eq!(foo_array, [10]);
939950
/// ```
940951
#[inline]
952+
#[must_use]
941953
#[stable(feature = "rust1", since = "1.0.0")]
942954
#[rustc_const_unstable(feature = "const_transmute_copy", issue = "83165")]
943955
pub const unsafe fn transmute_copy<T, U>(src: &T) -> U {
@@ -1051,6 +1063,7 @@ pub const fn discriminant<T>(v: &T) -> Discriminant<T> {
10511063
/// assert_eq!(mem::variant_count::<Result<!, !>>(), 2);
10521064
/// ```
10531065
#[inline(always)]
1066+
#[must_use]
10541067
#[unstable(feature = "variant_count", issue = "73662")]
10551068
#[rustc_const_unstable(feature = "variant_count", issue = "73662")]
10561069
#[rustc_diagnostic_item = "mem_variant_count"]

library/core/src/ptr/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
204204
/// assert!(p.is_null());
205205
/// ```
206206
#[inline(always)]
207+
#[must_use]
207208
#[stable(feature = "rust1", since = "1.0.0")]
208209
#[rustc_promotable]
209210
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
@@ -223,6 +224,7 @@ pub const fn null<T>() -> *const T {
223224
/// assert!(p.is_null());
224225
/// ```
225226
#[inline(always)]
227+
#[must_use]
226228
#[stable(feature = "rust1", since = "1.0.0")]
227229
#[rustc_promotable]
228230
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]

library/core/src/ptr/non_null.rs

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl<T: Sized> NonNull<T> {
8383
/// ```
8484
#[stable(feature = "nonnull", since = "1.25.0")]
8585
#[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.36.0")]
86+
#[must_use]
8687
#[inline]
8788
pub const fn dangling() -> Self {
8889
// SAFETY: mem::align_of() returns a non-zero usize which is then casted
@@ -419,6 +420,7 @@ impl<T> NonNull<[T]> {
419420
/// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.)
420421
#[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")]
421422
#[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")]
423+
#[must_use]
422424
#[inline]
423425
pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self {
424426
// SAFETY: `data` is a `NonNull` pointer which is necessarily non-null

library/core/src/ptr/unique.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl<T: Sized> Unique<T> {
6868
/// a `T`, which means this must not be used as a "not yet initialized"
6969
/// sentinel value. Types that lazily allocate must track initialization by
7070
/// some other means.
71+
#[must_use]
7172
#[inline]
7273
pub const fn dangling() -> Self {
7374
// SAFETY: mem::align_of() returns a valid, non-null pointer. The

src/test/ui/issues/issue-31299.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct PtrBack<T: Front>(Vec<T::Back>);
2929

3030
struct M(PtrBack<Vec<M>>);
3131

32+
#[allow(unused_must_use)]
3233
fn main() {
3334
std::mem::size_of::<M>();
3435
}

0 commit comments

Comments
 (0)