Skip to content

Commit 42e0282

Browse files
committed
Add #[must_use] to alloc functions that would leak memory
1 parent e249ce6 commit 42e0282

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

library/alloc/src/alloc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub use std::alloc::Global;
8181
/// }
8282
/// ```
8383
#[stable(feature = "global_alloc", since = "1.28.0")]
84+
#[must_use = "losing the pointer will leak memory"]
8485
#[inline]
8586
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
8687
unsafe { __rust_alloc(layout.size(), layout.align()) }
@@ -117,6 +118,7 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
117118
///
118119
/// See [`GlobalAlloc::realloc`].
119120
#[stable(feature = "global_alloc", since = "1.28.0")]
121+
#[must_use = "losing the pointer will leak memory"]
120122
#[inline]
121123
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
122124
unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
@@ -150,6 +152,7 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
150152
/// }
151153
/// ```
152154
#[stable(feature = "global_alloc", since = "1.28.0")]
155+
#[must_use = "losing the pointer will leak memory"]
153156
#[inline]
154157
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
155158
unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }

library/alloc/src/sync.rs

+1
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ impl<T: ?Sized> Arc<T> {
804804
/// let x_ptr = Arc::into_raw(x);
805805
/// assert_eq!(unsafe { &*x_ptr }, "hello");
806806
/// ```
807+
#[must_use = "losing the pointer will leak memory"]
807808
#[stable(feature = "rc_raw", since = "1.17.0")]
808809
pub fn into_raw(this: Self) -> *const T {
809810
let ptr = Self::as_ptr(&this);

0 commit comments

Comments
 (0)