about summary refs log tree commit diff
diff options
context:
space:
mode:
authorrhysd <lin90162@yahoo.co.jp>2022-07-15 17:05:50 +0900
committerrhysd <lin90162@yahoo.co.jp>2022-07-15 17:05:50 +0900
commitfa3156ec427192d3ebe3992f1c49b542d3d1179a (patch)
treeb28c8d2bded02cc665c4c7091a92dedb585bab97
parent30243dd87e7dbd17d68e1a1d16ba066a7d2cebe5 (diff)
downloadrust-fa3156ec427192d3ebe3992f1c49b542d3d1179a.tar.gz
rust-fa3156ec427192d3ebe3992f1c49b542d3d1179a.zip
add `#[must_use]` to `Box::from_raw`
-rw-r--r--library/alloc/src/boxed.rs1
-rw-r--r--src/test/ui/lint/unused/must-use-box-from-raw.rs11
-rw-r--r--src/test/ui/lint/unused/must-use-box-from-raw.stderr15
3 files changed, 27 insertions, 0 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs
index d83bab7bbbd..c1ceeb0deb8 100644
--- a/library/alloc/src/boxed.rs
+++ b/library/alloc/src/boxed.rs
@@ -949,6 +949,7 @@ impl<T: ?Sized> Box<T> {
     /// [`Layout`]: crate::Layout
     #[stable(feature = "box_raw", since = "1.4.0")]
     #[inline]
+    #[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `Box`"]
     pub unsafe fn from_raw(raw: *mut T) -> Self {
         unsafe { Self::from_raw_in(raw, Global) }
     }
diff --git a/src/test/ui/lint/unused/must-use-box-from-raw.rs b/src/test/ui/lint/unused/must-use-box-from-raw.rs
new file mode 100644
index 00000000000..9ea7726894c
--- /dev/null
+++ b/src/test/ui/lint/unused/must-use-box-from-raw.rs
@@ -0,0 +1,11 @@
+// #99269
+
+// check-pass
+
+#![warn(unused_must_use)]
+
+unsafe fn free<T>(ptr: *mut T) {
+    Box::from_raw(ptr); //~ WARNING unused return value
+}
+
+fn main() {}
diff --git a/src/test/ui/lint/unused/must-use-box-from-raw.stderr b/src/test/ui/lint/unused/must-use-box-from-raw.stderr
new file mode 100644
index 00000000000..7769f09aa52
--- /dev/null
+++ b/src/test/ui/lint/unused/must-use-box-from-raw.stderr
@@ -0,0 +1,15 @@
+warning: unused return value of `Box::<T>::from_raw` that must be used
+  --> $DIR/must-use-box-from-raw.rs:8:5
+   |
+LL |     Box::from_raw(ptr);
+   |     ^^^^^^^^^^^^^^^^^^^
+   |
+note: the lint level is defined here
+  --> $DIR/must-use-box-from-raw.rs:5:9
+   |
+LL | #![warn(unused_must_use)]
+   |         ^^^^^^^^^^^^^^^
+   = note: call `drop(from_raw(ptr))` if you intend to drop the `Box`
+
+warning: 1 warning emitted
+