Skip to content

Commit fa3156e

Browse files
committed
add #[must_use] to Box::from_raw
1 parent 30243dd commit fa3156e

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

library/alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ impl<T: ?Sized> Box<T> {
949949
/// [`Layout`]: crate::Layout
950950
#[stable(feature = "box_raw", since = "1.4.0")]
951951
#[inline]
952+
#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `Box`"]
952953
pub unsafe fn from_raw(raw: *mut T) -> Self {
953954
unsafe { Self::from_raw_in(raw, Global) }
954955
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// #99269
2+
3+
// check-pass
4+
5+
#![warn(unused_must_use)]
6+
7+
unsafe fn free<T>(ptr: *mut T) {
8+
Box::from_raw(ptr); //~ WARNING unused return value
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
warning: unused return value of `Box::<T>::from_raw` that must be used
2+
--> $DIR/must-use-box-from-raw.rs:8:5
3+
|
4+
LL | Box::from_raw(ptr);
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/must-use-box-from-raw.rs:5:9
9+
|
10+
LL | #![warn(unused_must_use)]
11+
| ^^^^^^^^^^^^^^^
12+
= note: call `drop(from_raw(ptr))` if you intend to drop the `Box`
13+
14+
warning: 1 warning emitted
15+

0 commit comments

Comments
 (0)