Skip to content
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

Add #[must_use] to some into_raw* functions. #127586

Merged
merged 3 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
/// ```
///
/// [memory layout]: self#memory-layout
#[must_use = "losing the pointer will leak memory"]
#[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
pub fn into_raw(b: Self) -> *mut T {
Expand Down Expand Up @@ -1150,6 +1151,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
/// ```
///
/// [memory layout]: self#memory-layout
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "allocator_api", issue = "32838")]
#[inline]
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
/// let x = unsafe { Rc::from_raw_in(ptr, alloc) };
/// assert_eq!(&*x, "hello");
/// ```
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn into_raw_with_allocator(this: Self) -> (*const T, A) {
let this = mem::ManuallyDrop::new(this);
Expand Down Expand Up @@ -3100,6 +3101,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
///
/// [`from_raw_in`]: Weak::from_raw_in
/// [`as_ptr`]: Weak::as_ptr
#[must_use = "losing the pointer will leak memory"]
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn into_raw_with_allocator(self) -> (*const T, A) {
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ impl String {
/// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };
/// assert_eq!(rebuilt, "hello");
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
pub fn into_raw_parts(self) -> (*mut u8, usize, usize) {
self.vec.into_raw_parts()
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// };
/// assert_eq!(rebuilt, [4294967295, 0, 1]);
/// ```
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
let mut me = ManuallyDrop::new(self);
Expand Down Expand Up @@ -922,6 +923,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// };
/// assert_eq!(rebuilt, [4294967295, 0, 1]);
/// ```
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "allocator_api", issue = "32838")]
// #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) {
Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/fd/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub trait IntoRawFd {
/// let raw_fd: RawFd = f.into_raw_fd();
/// # Ok::<(), io::Error>(())
/// ```
#[must_use = "losing the raw file descriptor may leak resources"]
#[stable(feature = "into_raw_os", since = "1.4.0")]
fn into_raw_fd(self) -> RawFd;
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/solid/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ pub trait IntoRawFd {
/// This function **transfers ownership** of the underlying file descriptor
/// to the caller. Callers are then the unique owners of the file descriptor
/// and must close the descriptor once it's no longer needed.
#[must_use = "losing the raw file descriptor may leak resources"]
fn into_raw_fd(self) -> RawFd;
}

Expand Down
2 changes: 2 additions & 0 deletions library/std/src/os/windows/io/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub trait IntoRawHandle {
/// However, transferring ownership is not strictly required. Use a
/// `Into<OwnedHandle>::into` implementation for an API which strictly
/// transfers ownership.
#[must_use = "losing the raw handle may leak resources"]
#[stable(feature = "into_raw_os", since = "1.4.0")]
fn into_raw_handle(self) -> RawHandle;
}
Expand Down Expand Up @@ -230,6 +231,7 @@ pub trait IntoRawSocket {
/// However, transferring ownership is not strictly required. Use a
/// `Into<OwnedSocket>::into` implementation for an API which strictly
/// transfers ownership.
#[must_use = "losing the raw socket may leak resources"]
#[stable(feature = "into_raw_os", since = "1.4.0")]
fn into_raw_socket(self) -> RawSocket;
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl Stdio {
Ok(io) => unsafe {
let io = Handle::from_raw_handle(io);
let ret = io.duplicate(0, true, c::DUPLICATE_SAME_ACCESS);
io.into_raw_handle();
let _ = io.into_raw_handle(); // Don't close the handle
ret
},
// If no stdio handle is available, then propagate the null value.
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/pal/windows/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn write(
unsafe {
let handle = Handle::from_raw_handle(handle);
let ret = handle.write(data);
handle.into_raw_handle(); // Don't close the handle
let _ = handle.into_raw_handle(); // Don't close the handle
return ret;
}
}
Expand Down Expand Up @@ -250,7 +250,7 @@ impl io::Read for Stdin {
unsafe {
let handle = Handle::from_raw_handle(handle);
let ret = handle.read(buf);
handle.into_raw_handle(); // Don't close the handle
let _ = handle.into_raw_handle(); // Don't close the handle
return ret;
}
}
Expand Down
Loading