Skip to content

Commit 86d6d2b

Browse files
committed
Auto merge of rust-lang#89755 - jkugelman:must-use-conversions-that-move-self, r=joshtriplett
Add #[must_use] to conversions that move self Everything here got the same message. Is the wording okay? ```rust #[must_use = "`self` will be dropped if the result is not used"] ``` I want to draw attention to these methods in particular: ```rust alloc::sync::Arc<MaybeUninit<T>> unsafe fn assume_init(self) -> Arc<T>; alloc::sync::Arc<[MaybeUninit<T>]> unsafe fn assume_init(self) -> Arc<[T]>; core::pin::Pin<&'a mut T> const fn into_ref(self) -> Pin<&'a T>; core::pin::Pin<&'a mut T> const fn get_mut(self) -> &'a mut T; core::pin::Pin<&'a mut T> const unsafe fn get_unchecked_mut(self) -> &'a mut T; core::pin::Pin<&'a mut T> unsafe fn map_unchecked_mut(self, func: F) -> Pin<&'a mut U>; core::pin::Pin<&'a mut Pin<P>> fn as_deref_mut(self) -> Pin<&'a mut P::Target>; ``` Parent issue: rust-lang#89692 r? `@joshtriplett`
2 parents 9a75781 + b115781 commit 86d6d2b

File tree

19 files changed

+54
-11
lines changed

19 files changed

+54
-11
lines changed

library/alloc/src/collections/binary_heap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ impl<T> BinaryHeap<T> {
848848
///
849849
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
850850
/// ```
851+
#[must_use = "`self` will be dropped if the result is not used"]
851852
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
852853
pub fn into_iter_sorted(self) -> IntoIterSorted<T> {
853854
IntoIterSorted { inner: self }
@@ -1028,6 +1029,7 @@ impl<T> BinaryHeap<T> {
10281029
/// println!("{}", x);
10291030
/// }
10301031
/// ```
1032+
#[must_use = "`self` will be dropped if the result is not used"]
10311033
#[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
10321034
pub fn into_vec(self) -> Vec<T> {
10331035
self.into()

library/alloc/src/collections/btree/map.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,7 @@ impl<K, V> BTreeMap<K, V> {
12641264
/// assert_eq!(keys, [1, 2]);
12651265
/// ```
12661266
#[inline]
1267+
#[must_use = "`self` will be dropped if the result is not used"]
12671268
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
12681269
pub fn into_keys(self) -> IntoKeys<K, V> {
12691270
IntoKeys { inner: self.into_iter() }
@@ -1286,6 +1287,7 @@ impl<K, V> BTreeMap<K, V> {
12861287
/// assert_eq!(values, ["hello", "goodbye"]);
12871288
/// ```
12881289
#[inline]
1290+
#[must_use = "`self` will be dropped if the result is not used"]
12891291
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
12901292
pub fn into_values(self) -> IntoValues<K, V> {
12911293
IntoValues { inner: self.into_iter() }

library/alloc/src/collections/btree/map/entry.rs

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
448448
/// }
449449
/// assert_eq!(map["poneyland"], 22);
450450
/// ```
451+
#[must_use = "`self` will be dropped if the result is not used"]
451452
#[stable(feature = "rust1", since = "1.0.0")]
452453
pub fn into_mut(self) -> &'a mut V {
453454
self.handle.into_val_mut()

library/alloc/src/collections/btree/map/tests.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1755,20 +1755,20 @@ fn test_send() {
17551755
#[test]
17561756
fn test_ord_absence() {
17571757
fn map<K>(mut map: BTreeMap<K, ()>) {
1758-
map.is_empty();
1759-
map.len();
1758+
let _ = map.is_empty();
1759+
let _ = map.len();
17601760
map.clear();
1761-
map.iter();
1762-
map.iter_mut();
1763-
map.keys();
1764-
map.values();
1765-
map.values_mut();
1761+
let _ = map.iter();
1762+
let _ = map.iter_mut();
1763+
let _ = map.keys();
1764+
let _ = map.values();
1765+
let _ = map.values_mut();
17661766
if true {
1767-
map.into_values();
1767+
let _ = map.into_values();
17681768
} else if true {
1769-
map.into_iter();
1769+
let _ = map.into_iter();
17701770
} else {
1771-
map.into_keys();
1771+
let _ = map.into_keys();
17721772
}
17731773
}
17741774

library/alloc/src/rc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,7 @@ impl<T: ?Sized> Weak<T> {
21302130
///
21312131
/// [`from_raw`]: Weak::from_raw
21322132
/// [`as_ptr`]: Weak::as_ptr
2133+
#[must_use = "`self` will be dropped if the result is not used"]
21332134
#[stable(feature = "weak_into_raw", since = "1.45.0")]
21342135
pub fn into_raw(self) -> *const T {
21352136
let result = self.as_ptr();

library/alloc/src/string.rs

+4
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ impl String {
676676
/// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };
677677
/// assert_eq!(rebuilt, "hello");
678678
/// ```
679+
#[must_use = "`self` will be dropped if the result is not used"]
679680
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
680681
pub fn into_raw_parts(self) -> (*mut u8, usize, usize) {
681682
self.vec.into_raw_parts()
@@ -781,6 +782,7 @@ impl String {
781782
/// assert_eq!(&[104, 101, 108, 108, 111][..], &bytes[..]);
782783
/// ```
783784
#[inline]
785+
#[must_use = "`self` will be dropped if the result is not used"]
784786
#[stable(feature = "rust1", since = "1.0.0")]
785787
pub fn into_bytes(self) -> Vec<u8> {
786788
self.vec
@@ -1738,6 +1740,7 @@ impl String {
17381740
/// ```
17391741
#[cfg(not(no_global_oom_handling))]
17401742
#[stable(feature = "box_str", since = "1.4.0")]
1743+
#[must_use = "`self` will be dropped if the result is not used"]
17411744
#[inline]
17421745
pub fn into_boxed_str(self) -> Box<str> {
17431746
let slice = self.vec.into_boxed_slice();
@@ -1783,6 +1786,7 @@ impl FromUtf8Error {
17831786
///
17841787
/// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
17851788
/// ```
1789+
#[must_use = "`self` will be dropped if the result is not used"]
17861790
#[stable(feature = "rust1", since = "1.0.0")]
17871791
pub fn into_bytes(self) -> Vec<u8> {
17881792
self.bytes

library/alloc/src/sync.rs

+3
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ impl<T> Arc<mem::MaybeUninit<T>> {
735735
/// assert_eq!(*five, 5)
736736
/// ```
737737
#[unstable(feature = "new_uninit", issue = "63291")]
738+
#[must_use = "`self` will be dropped if the result is not used"]
738739
#[inline]
739740
pub unsafe fn assume_init(self) -> Arc<T> {
740741
Arc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
@@ -776,6 +777,7 @@ impl<T> Arc<[mem::MaybeUninit<T>]> {
776777
/// assert_eq!(*values, [1, 2, 3])
777778
/// ```
778779
#[unstable(feature = "new_uninit", issue = "63291")]
780+
#[must_use = "`self` will be dropped if the result is not used"]
779781
#[inline]
780782
pub unsafe fn assume_init(self) -> Arc<[T]> {
781783
unsafe { Arc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _) }
@@ -1759,6 +1761,7 @@ impl<T: ?Sized> Weak<T> {
17591761
///
17601762
/// [`from_raw`]: Weak::from_raw
17611763
/// [`as_ptr`]: Weak::as_ptr
1764+
#[must_use = "`self` will be dropped if the result is not used"]
17621765
#[stable(feature = "weak_into_raw", since = "1.45.0")]
17631766
pub fn into_raw(self) -> *const T {
17641767
let result = self.as_ptr();

library/core/src/option.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ impl<T: Copy> Option<&mut T> {
14731473
/// let copied = opt_x.copied();
14741474
/// assert_eq!(copied, Some(12));
14751475
/// ```
1476+
#[must_use = "`self` will be dropped if the result is not used"]
14761477
#[stable(feature = "copied", since = "1.35.0")]
14771478
pub fn copied(self) -> Option<T> {
14781479
self.map(|&mut t| t)
@@ -1492,6 +1493,7 @@ impl<T: Clone> Option<&T> {
14921493
/// let cloned = opt_x.cloned();
14931494
/// assert_eq!(cloned, Some(12));
14941495
/// ```
1496+
#[must_use = "`self` will be dropped if the result is not used"]
14951497
#[stable(feature = "rust1", since = "1.0.0")]
14961498
pub fn cloned(self) -> Option<T> {
14971499
self.map(|t| t.clone())

library/core/src/pin.rs

+5
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
715715
impl<'a, T: ?Sized> Pin<&'a mut T> {
716716
/// Converts this `Pin<&mut T>` into a `Pin<&T>` with the same lifetime.
717717
#[inline(always)]
718+
#[must_use = "`self` will be dropped if the result is not used"]
718719
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
719720
#[stable(feature = "pin", since = "1.33.0")]
720721
pub const fn into_ref(self) -> Pin<&'a T> {
@@ -731,6 +732,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
731732
/// the `Pin` itself. This method allows turning the `Pin` into a reference
732733
/// with the same lifetime as the original `Pin`.
733734
#[inline(always)]
735+
#[must_use = "`self` will be dropped if the result is not used"]
734736
#[stable(feature = "pin", since = "1.33.0")]
735737
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
736738
pub const fn get_mut(self) -> &'a mut T
@@ -751,6 +753,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
751753
/// If the underlying data is `Unpin`, `Pin::get_mut` should be used
752754
/// instead.
753755
#[inline(always)]
756+
#[must_use = "`self` will be dropped if the result is not used"]
754757
#[stable(feature = "pin", since = "1.33.0")]
755758
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
756759
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
@@ -772,6 +775,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
772775
/// not move out of the argument you receive to the interior function.
773776
///
774777
/// [`pin` module]: self#projections-and-structural-pinning
778+
#[must_use = "`self` will be dropped if the result is not used"]
775779
#[stable(feature = "pin", since = "1.33.0")]
776780
pub unsafe fn map_unchecked_mut<U, F>(self, func: F) -> Pin<&'a mut U>
777781
where
@@ -811,6 +815,7 @@ impl<'a, P: DerefMut> Pin<&'a mut Pin<P>> {
811815
/// implementations of `P::DerefMut` are likewise ruled out by the contract of
812816
/// `Pin::new_unchecked`.
813817
#[unstable(feature = "pin_deref_mut", issue = "86918")]
818+
#[must_use = "`self` will be dropped if the result is not used"]
814819
#[inline(always)]
815820
pub fn as_deref_mut(self) -> Pin<&'a mut P::Target> {
816821
// SAFETY: What we're asserting here is that going from

library/core/src/ptr/unique.rs

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl<T: ?Sized> Unique<T> {
101101
}
102102

103103
/// Acquires the underlying `*mut` pointer.
104+
#[must_use = "`self` will be dropped if the result is not used"]
104105
#[inline]
105106
pub const fn as_ptr(self) -> *mut T {
106107
self.pointer as *mut T
@@ -131,6 +132,7 @@ impl<T: ?Sized> Unique<T> {
131132
}
132133

133134
/// Casts to a pointer of another type.
135+
#[must_use = "`self` will be dropped if the result is not used"]
134136
#[inline]
135137
pub const fn cast<U>(self) -> Unique<U> {
136138
// SAFETY: Unique::new_unchecked() creates a new unique and needs

library/core/src/slice/iter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl<'a, T> IterMut<'a, T> {
267267
/// // Now slice is "[2, 2, 3]":
268268
/// println!("{:?}", slice);
269269
/// ```
270+
#[must_use = "`self` will be dropped if the result is not used"]
270271
#[stable(feature = "iter_to_slice", since = "1.4.0")]
271272
pub fn into_slice(self) -> &'a mut [T] {
272273
// SAFETY: the iterator was created from a mutable slice with pointer
@@ -1869,6 +1870,7 @@ impl<'a, T> ChunksExactMut<'a, T> {
18691870
/// Returns the remainder of the original slice that is not going to be
18701871
/// returned by the iterator. The returned slice has at most `chunk_size-1`
18711872
/// elements.
1873+
#[must_use = "`self` will be dropped if the result is not used"]
18721874
#[stable(feature = "chunks_exact", since = "1.31.0")]
18731875
pub fn into_remainder(self) -> &'a mut [T] {
18741876
self.rem
@@ -2264,6 +2266,7 @@ impl<'a, T, const N: usize> ArrayChunksMut<'a, T, N> {
22642266
/// Returns the remainder of the original slice that is not going to be
22652267
/// returned by the iterator. The returned slice has at most `N-1`
22662268
/// elements.
2269+
#[must_use = "`self` will be dropped if the result is not used"]
22672270
#[unstable(feature = "array_chunks", issue = "74985")]
22682271
pub fn into_remainder(self) -> &'a mut [T] {
22692272
self.rem
@@ -2875,6 +2878,7 @@ impl<'a, T> RChunksExactMut<'a, T> {
28752878
/// Returns the remainder of the original slice that is not going to be
28762879
/// returned by the iterator. The returned slice has at most `chunk_size-1`
28772880
/// elements.
2881+
#[must_use = "`self` will be dropped if the result is not used"]
28782882
#[stable(feature = "rchunks", since = "1.31.0")]
28792883
pub fn into_remainder(self) -> &'a mut [T] {
28802884
self.rem

library/std/src/collections/hash/map.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
17201720
/// Converts the entry into a mutable reference to the key in the entry
17211721
/// with a lifetime bound to the map itself.
17221722
#[inline]
1723+
#[must_use = "`self` will be dropped if the result is not used"]
17231724
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17241725
pub fn into_key(self) -> &'a mut K {
17251726
self.base.into_key()
@@ -1735,6 +1736,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
17351736
/// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
17361737
/// with a lifetime bound to the map itself.
17371738
#[inline]
1739+
#[must_use = "`self` will be dropped if the result is not used"]
17381740
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17391741
pub fn into_mut(self) -> &'a mut V {
17401742
self.base.into_mut()
@@ -1764,6 +1766,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
17641766
/// Converts the `OccupiedEntry` into a mutable reference to the key and value in the entry
17651767
/// with a lifetime bound to the map itself.
17661768
#[inline]
1769+
#[must_use = "`self` will be dropped if the result is not used"]
17671770
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17681771
pub fn into_key_value(self) -> (&'a mut K, &'a mut V) {
17691772
self.base.into_key_value()

library/std/src/ffi/c_str.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ impl FromVecWithNulError {
322322
///
323323
/// assert_eq!(bytes, value.unwrap_err().into_bytes());
324324
/// ```
325+
#[must_use = "`self` will be dropped if the result is not used"]
325326
pub fn into_bytes(self) -> Vec<u8> {
326327
self.bytes
327328
}
@@ -524,6 +525,7 @@ impl CString {
524525
/// }
525526
/// ```
526527
#[inline]
528+
#[must_use = "`self` will be dropped if the result is not used"]
527529
#[stable(feature = "cstr_memory", since = "1.4.0")]
528530
pub fn into_raw(self) -> *mut c_char {
529531
Box::into_raw(self.into_inner()) as *mut c_char
@@ -547,7 +549,6 @@ impl CString {
547549
/// let err = cstring.into_string().err().expect("into_string().err() failed");
548550
/// assert_eq!(err.utf8_error().valid_up_to(), 1);
549551
/// ```
550-
551552
#[stable(feature = "cstring_into", since = "1.7.0")]
552553
pub fn into_string(self) -> Result<String, IntoStringError> {
553554
String::from_utf8(self.into_bytes()).map_err(|e| IntoStringError {
@@ -571,6 +572,7 @@ impl CString {
571572
/// let bytes = c_string.into_bytes();
572573
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
573574
/// ```
575+
#[must_use = "`self` will be dropped if the result is not used"]
574576
#[stable(feature = "cstring_into", since = "1.7.0")]
575577
pub fn into_bytes(self) -> Vec<u8> {
576578
let mut vec = self.into_inner().into_vec();
@@ -591,6 +593,7 @@ impl CString {
591593
/// let bytes = c_string.into_bytes_with_nul();
592594
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
593595
/// ```
596+
#[must_use = "`self` will be dropped if the result is not used"]
594597
#[stable(feature = "cstring_into", since = "1.7.0")]
595598
pub fn into_bytes_with_nul(self) -> Vec<u8> {
596599
self.into_inner().into_vec()
@@ -667,6 +670,7 @@ impl CString {
667670
/// assert_eq!(&*boxed,
668671
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
669672
/// ```
673+
#[must_use = "`self` will be dropped if the result is not used"]
670674
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
671675
pub fn into_boxed_c_str(self) -> Box<CStr> {
672676
unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
@@ -1018,6 +1022,7 @@ impl NulError {
10181022
/// let nul_error = CString::new("foo\0bar").unwrap_err();
10191023
/// assert_eq!(nul_error.into_vec(), b"foo\0bar");
10201024
/// ```
1025+
#[must_use = "`self` will be dropped if the result is not used"]
10211026
#[stable(feature = "rust1", since = "1.0.0")]
10221027
pub fn into_vec(self) -> Vec<u8> {
10231028
self.1
@@ -1092,6 +1097,7 @@ impl fmt::Display for FromVecWithNulError {
10921097
impl IntoStringError {
10931098
/// Consumes this error, returning original [`CString`] which generated the
10941099
/// error.
1100+
#[must_use = "`self` will be dropped if the result is not used"]
10951101
#[stable(feature = "cstring_into", since = "1.7.0")]
10961102
pub fn into_cstring(self) -> CString {
10971103
self.inner

library/std/src/ffi/os_str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl OsString {
346346
///
347347
/// let b: Box<OsStr> = s.into_boxed_os_str();
348348
/// ```
349+
#[must_use = "`self` will be dropped if the result is not used"]
349350
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
350351
pub fn into_boxed_os_str(self) -> Box<OsStr> {
351352
let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;

library/std/src/io/buffered/bufwriter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ pub struct WriterPanicked {
476476
impl WriterPanicked {
477477
/// Returns the perhaps-unwritten data. Some of this data may have been written by the
478478
/// panicking call(s) to the underlying writer, so simply writing it again is not a good idea.
479+
#[must_use = "`self` will be dropped if the result is not used"]
479480
#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
480481
pub fn into_inner(self) -> Vec<u8> {
481482
self.buf

library/std/src/io/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ impl Error {
657657
/// }
658658
/// ```
659659
#[stable(feature = "io_error_inner", since = "1.3.0")]
660+
#[must_use = "`self` will be dropped if the result is not used"]
660661
#[inline]
661662
pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
662663
match self.repr {

library/std/src/io/stdio.rs

+1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ impl Stdin {
464464
/// println!("got a line: {}", line.unwrap());
465465
/// }
466466
/// ```
467+
#[must_use = "`self` will be dropped if the result is not used"]
467468
#[unstable(feature = "stdin_forwarders", issue = "87096")]
468469
pub fn lines(self) -> Lines<StdinLock<'static>> {
469470
self.into_locked().lines()

library/std/src/net/tcp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ impl TcpListener {
883883
/// Ok(())
884884
/// }
885885
/// ```
886+
#[must_use = "`self` will be dropped if the result is not used"]
886887
#[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
887888
pub fn into_incoming(self) -> IntoIncoming {
888889
IntoIncoming { listener: self }

library/std/src/path.rs

+3
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ impl<'a> Component<'a> {
532532
/// let components: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
533533
/// assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);
534534
/// ```
535+
#[must_use = "`self` will be dropped if the result is not used"]
535536
#[stable(feature = "rust1", since = "1.0.0")]
536537
pub fn as_os_str(self) -> &'a OsStr {
537538
match self {
@@ -1428,13 +1429,15 @@ impl PathBuf {
14281429
/// let os_str = p.into_os_string();
14291430
/// ```
14301431
#[stable(feature = "rust1", since = "1.0.0")]
1432+
#[must_use = "`self` will be dropped if the result is not used"]
14311433
#[inline]
14321434
pub fn into_os_string(self) -> OsString {
14331435
self.inner
14341436
}
14351437

14361438
/// Converts this `PathBuf` into a [boxed](Box) [`Path`].
14371439
#[stable(feature = "into_boxed_path", since = "1.20.0")]
1440+
#[must_use = "`self` will be dropped if the result is not used"]
14381441
#[inline]
14391442
pub fn into_boxed_path(self) -> Box<Path> {
14401443
let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;

0 commit comments

Comments
 (0)