Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c88a0a7

Browse files
authoredAug 3, 2024
Rollup merge of rust-lang#127586 - zachs18:more-must-use, r=cuviper
Add `#[must_use]` to some `into_raw*` functions. cc rust-lang#121287 r? `@cuviper` Adds `#[must_use = "losing the pointer will leak memory"]`[^1] to `Box::into_raw(_with_allocator)`, `Vec::into_raw_parts(_with_alloc)`, `String::into_raw_parts`[^2], and `rc::{Rc, Weak}::into_raw_with_allocator` (Rc's normal `into_raw` and all of `Arc`'s `into_raw*`s are already `must_use`). Adds `#[must_use = "losing the raw <resource name may leak resources"]` to `IntoRawFd::into_raw_fd`, `IntoRawSocket::into_raw_socket`, and `IntoRawHandle::into_raw_handle`. [^1]: "*will* leak memory" may be too-strong wording (since `Box`/`Vec`/`String`/`rc::Weak` might not have a backing allocation), but I left it as-is for simplicity and consistency. [^2]: `String::into_raw_parts`'s `must_use` message is changed from the previous (possibly misleading) "`self` will be dropped if the result is not used".
2 parents 83ba819 + 84d84da commit c88a0a7

File tree

9 files changed

+14
-4
lines changed

9 files changed

+14
-4
lines changed
 

‎library/alloc/src/boxed.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11731173
/// ```
11741174
///
11751175
/// [memory layout]: self#memory-layout
1176+
#[must_use = "losing the pointer will leak memory"]
11761177
#[stable(feature = "box_raw", since = "1.4.0")]
11771178
#[inline]
11781179
pub fn into_raw(b: Self) -> *mut T {
@@ -1226,6 +1227,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12261227
/// ```
12271228
///
12281229
/// [memory layout]: self#memory-layout
1230+
#[must_use = "losing the pointer will leak memory"]
12291231
#[unstable(feature = "allocator_api", issue = "32838")]
12301232
#[inline]
12311233
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {

‎library/alloc/src/rc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
13721372
/// let x = unsafe { Rc::from_raw_in(ptr, alloc) };
13731373
/// assert_eq!(&*x, "hello");
13741374
/// ```
1375+
#[must_use = "losing the pointer will leak memory"]
13751376
#[unstable(feature = "allocator_api", issue = "32838")]
13761377
pub fn into_raw_with_allocator(this: Self) -> (*const T, A) {
13771378
let this = mem::ManuallyDrop::new(this);
@@ -3107,6 +3108,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
31073108
///
31083109
/// [`from_raw_in`]: Weak::from_raw_in
31093110
/// [`as_ptr`]: Weak::as_ptr
3111+
#[must_use = "losing the pointer will leak memory"]
31103112
#[inline]
31113113
#[unstable(feature = "allocator_api", issue = "32838")]
31123114
pub fn into_raw_with_allocator(self) -> (*const T, A) {

‎library/alloc/src/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ impl String {
900900
/// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };
901901
/// assert_eq!(rebuilt, "hello");
902902
/// ```
903-
#[must_use = "`self` will be dropped if the result is not used"]
903+
#[must_use = "losing the pointer will leak memory"]
904904
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
905905
pub fn into_raw_parts(self) -> (*mut u8, usize, usize) {
906906
self.vec.into_raw_parts()

‎library/alloc/src/vec/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ impl<T, A: Allocator> Vec<T, A> {
878878
/// };
879879
/// assert_eq!(rebuilt, [4294967295, 0, 1]);
880880
/// ```
881+
#[must_use = "losing the pointer will leak memory"]
881882
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
882883
pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
883884
let mut me = ManuallyDrop::new(self);
@@ -921,6 +922,7 @@ impl<T, A: Allocator> Vec<T, A> {
921922
/// };
922923
/// assert_eq!(rebuilt, [4294967295, 0, 1]);
923924
/// ```
925+
#[must_use = "losing the pointer will leak memory"]
924926
#[unstable(feature = "allocator_api", issue = "32838")]
925927
// #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
926928
pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) {

‎library/std/src/os/fd/raw.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub trait IntoRawFd {
138138
/// let raw_fd: RawFd = f.into_raw_fd();
139139
/// # Ok::<(), io::Error>(())
140140
/// ```
141+
#[must_use = "losing the raw file descriptor may leak resources"]
141142
#[stable(feature = "into_raw_os", since = "1.4.0")]
142143
fn into_raw_fd(self) -> RawFd;
143144
}

‎library/std/src/os/solid/io.rs

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ pub trait IntoRawFd {
342342
/// This function **transfers ownership** of the underlying file descriptor
343343
/// to the caller. Callers are then the unique owners of the file descriptor
344344
/// and must close the descriptor once it's no longer needed.
345+
#[must_use = "losing the raw file descriptor may leak resources"]
345346
fn into_raw_fd(self) -> RawFd;
346347
}
347348

‎library/std/src/os/windows/io/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub trait IntoRawHandle {
8585
/// However, transferring ownership is not strictly required. Use a
8686
/// `Into<OwnedHandle>::into` implementation for an API which strictly
8787
/// transfers ownership.
88+
#[must_use = "losing the raw handle may leak resources"]
8889
#[stable(feature = "into_raw_os", since = "1.4.0")]
8990
fn into_raw_handle(self) -> RawHandle;
9091
}
@@ -228,6 +229,7 @@ pub trait IntoRawSocket {
228229
/// However, transferring ownership is not strictly required. Use a
229230
/// `Into<OwnedSocket>::into` implementation for an API which strictly
230231
/// transfers ownership.
232+
#[must_use = "losing the raw socket may leak resources"]
231233
#[stable(feature = "into_raw_os", since = "1.4.0")]
232234
fn into_raw_socket(self) -> RawSocket;
233235
}

‎library/std/src/sys/pal/windows/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl Stdio {
564564
Ok(io) => unsafe {
565565
let io = Handle::from_raw_handle(io);
566566
let ret = io.duplicate(0, true, c::DUPLICATE_SAME_ACCESS);
567-
io.into_raw_handle();
567+
let _ = io.into_raw_handle(); // Don't close the handle
568568
ret
569569
},
570570
// If no stdio handle is available, then propagate the null value.

‎library/std/src/sys/pal/windows/stdio.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn write(handle_id: u32, data: &[u8], incomplete_utf8: &mut IncompleteUtf8) -> i
9494
unsafe {
9595
let handle = Handle::from_raw_handle(handle);
9696
let ret = handle.write(data);
97-
handle.into_raw_handle(); // Don't close the handle
97+
let _ = handle.into_raw_handle(); // Don't close the handle
9898
return ret;
9999
}
100100
}
@@ -243,7 +243,7 @@ impl io::Read for Stdin {
243243
unsafe {
244244
let handle = Handle::from_raw_handle(handle);
245245
let ret = handle.read(buf);
246-
handle.into_raw_handle(); // Don't close the handle
246+
let _ = handle.into_raw_handle(); // Don't close the handle
247247
return ret;
248248
}
249249
}

0 commit comments

Comments
 (0)
Please sign in to comment.