Skip to content

Commit 96474a7

Browse files
authored
Rollup merge of #99270 - rhysd:issue-99269, r=Mark-Simulacrum
Add `#[must_use]` to `Box::from_raw` Fixes #99269
2 parents 8a64529 + fa3156e commit 96474a7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Diff for: 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
}

Diff for: src/test/ui/lint/unused/must-use-box-from-raw.rs

+11
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() {}

Diff for: src/test/ui/lint/unused/must-use-box-from-raw.stderr

+15
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)