]> git.lizzy.rs Git - rust.git/commitdiff
Add #[must_use] to alloc functions that would leak memory
authorJohn Kugelman <john@kugelman.name>
Sun, 31 Oct 2021 02:06:31 +0000 (22:06 -0400)
committerJohn Kugelman <john@kugelman.name>
Sun, 31 Oct 2021 02:19:07 +0000 (22:19 -0400)
library/alloc/src/alloc.rs
library/alloc/src/sync.rs

index 4a5b0fcf03709ff944fe4e612327bbeaee3cb70e..66ef92558d8b563c4d149fe5966c54ad60a657ad 100644 (file)
@@ -81,6 +81,7 @@
 /// }
 /// ```
 #[stable(feature = "global_alloc", since = "1.28.0")]
+#[must_use = "losing the pointer will leak memory"]
 #[inline]
 pub unsafe fn alloc(layout: Layout) -> *mut u8 {
     unsafe { __rust_alloc(layout.size(), layout.align()) }
@@ -117,6 +118,7 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
 ///
 /// See [`GlobalAlloc::realloc`].
 #[stable(feature = "global_alloc", since = "1.28.0")]
+#[must_use = "losing the pointer will leak memory"]
 #[inline]
 pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
     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
 /// }
 /// ```
 #[stable(feature = "global_alloc", since = "1.28.0")]
+#[must_use = "losing the pointer will leak memory"]
 #[inline]
 pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
     unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }
index b75e9a2f3c71ea77b0c1cd1ac717020317e79d45..00bd69aad4c3b51b7d051251af05d10eb2fdc307 100644 (file)
@@ -804,6 +804,7 @@ impl<T: ?Sized> Arc<T> {
     /// let x_ptr = Arc::into_raw(x);
     /// assert_eq!(unsafe { &*x_ptr }, "hello");
     /// ```
+    #[must_use = "losing the pointer will leak memory"]
     #[stable(feature = "rc_raw", since = "1.17.0")]
     pub fn into_raw(this: Self) -> *const T {
         let ptr = Self::as_ptr(&this);