From fa3156ec427192d3ebe3992f1c49b542d3d1179a Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 15 Jul 2022 17:05:50 +0900 Subject: [PATCH] add `#[must_use]` to `Box::from_raw` --- library/alloc/src/boxed.rs | 1 + src/test/ui/lint/unused/must-use-box-from-raw.rs | 11 +++++++++++ .../ui/lint/unused/must-use-box-from-raw.stderr | 15 +++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/test/ui/lint/unused/must-use-box-from-raw.rs create mode 100644 src/test/ui/lint/unused/must-use-box-from-raw.stderr diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index d83bab7bbbdad..c1ceeb0deb837 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -949,6 +949,7 @@ impl Box { /// [`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 0000000000000..9ea7726894cbc --- /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(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 0000000000000..7769f09aa5203 --- /dev/null +++ b/src/test/ui/lint/unused/must-use-box-from-raw.stderr @@ -0,0 +1,15 @@ +warning: unused return value of `Box::::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 +