Skip to content

Add #[must_use] to Box::from_raw #99270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
@@ -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) }
}
11 changes: 11 additions & 0 deletions src/test/ui/lint/unused/must-use-box-from-raw.rs
Original file line number Diff line number Diff line change
@@ -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() {}
15 changes: 15 additions & 0 deletions src/test/ui/lint/unused/must-use-box-from-raw.stderr
Original file line number Diff line number Diff line change
@@ -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