diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs index d3a87c056cfb1..dc7d0bff9a404 100644 --- a/library/alloc/tests/str.rs +++ b/library/alloc/tests/str.rs @@ -1031,7 +1031,7 @@ fn test_split_at_mut() { #[should_panic] fn test_split_at_boundscheck() { let s = "ศไทย中华Việt Nam"; - s.split_at(1); + let _ = s.split_at(1); } #[test] diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 780f82d8afaee..cc32d5223b49f 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -104,6 +104,7 @@ impl Layout { /// The minimum size in bytes for a memory block of this layout. #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_stable(feature = "const_alloc_layout", since = "1.50.0")] + #[must_use] #[inline] pub const fn size(&self) -> usize { self.size_ @@ -137,6 +138,7 @@ impl Layout { /// allocate backing structure for `T` (which could be a trait /// or other unsized type like a slice). #[stable(feature = "alloc_layout", since = "1.28.0")] + #[must_use] #[inline] pub fn for_value(t: &T) -> Self { let (size, align) = (mem::size_of_val(t), mem::align_of_val(t)); @@ -171,6 +173,7 @@ impl Layout { /// [trait object]: ../../book/ch17-02-trait-objects.html /// [extern type]: ../../unstable-book/language-features/extern-types.html #[unstable(feature = "layout_for_ptr", issue = "69835")] + #[must_use] pub unsafe fn for_value_raw(t: *const T) -> Self { // SAFETY: we pass along the prerequisites of these functions to the caller let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) }; @@ -187,6 +190,7 @@ impl Layout { /// some other means. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[rustc_const_unstable(feature = "alloc_layout_extra", issue = "55724")] + #[must_use] #[inline] pub const fn dangling(&self) -> NonNull { // SAFETY: align is guaranteed to be non-zero diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 19652106b3d01..1fd5aa27fce46 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -458,6 +458,7 @@ impl TypeId { /// assert_eq!(is_string(&0), false); /// assert_eq!(is_string(&"cookie monster".to_string()), true); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_type_id", issue = "77125")] pub const fn of() -> TypeId { @@ -492,6 +493,7 @@ impl TypeId { /// "core::option::Option", /// ); /// ``` +#[must_use] #[stable(feature = "type_name", since = "1.38.0")] #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] pub const fn type_name() -> &'static str { @@ -534,6 +536,7 @@ pub const fn type_name() -> &'static str { /// let y = 1.0; /// println!("{}", type_name_of_val(&y)); /// ``` +#[must_use] #[unstable(feature = "type_name_of_val", issue = "66359")] #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] pub const fn type_name_of_val(_val: &T) -> &'static str { diff --git a/library/core/src/ascii.rs b/library/core/src/ascii.rs index 0a456ee1eb2d5..532208e41afa2 100644 --- a/library/core/src/ascii.rs +++ b/library/core/src/ascii.rs @@ -18,6 +18,7 @@ use crate::str::from_utf8_unchecked; /// /// This `struct` is created by the [`escape_default`] function. See its /// documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct EscapeDefault { diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index ed464700cd357..d154bb3583c2d 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -1335,6 +1335,7 @@ impl<'b, T: ?Sized> Ref<'b, T> { /// with the widespread use of `r.borrow().clone()` to clone the contents of /// a `RefCell`. #[stable(feature = "cell_extras", since = "1.15.0")] + #[must_use] #[inline] pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> { Ref { value: orig.value, borrow: orig.borrow.clone() } diff --git a/library/core/src/char/decode.rs b/library/core/src/char/decode.rs index 4784418f98c50..5dd8c5ef78941 100644 --- a/library/core/src/char/decode.rs +++ b/library/core/src/char/decode.rs @@ -128,6 +128,7 @@ impl> Iterator for DecodeUtf16 { impl DecodeUtf16Error { /// Returns the unpaired surrogate which caused this error. + #[must_use] #[stable(feature = "decode_utf16", since = "1.9.0")] pub fn unpaired_surrogate(&self) -> u16 { self.code diff --git a/library/core/src/default.rs b/library/core/src/default.rs index 0ee8cd59ba4ff..fb862f7df947b 100644 --- a/library/core/src/default.rs +++ b/library/core/src/default.rs @@ -155,6 +155,7 @@ pub trait Default: Sized { /// } /// ``` #[unstable(feature = "default_free_fn", issue = "73014")] +#[must_use] #[inline] pub fn default() -> T { Default::default() diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 9258b4d0818f5..80d3270d73cc8 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1604,6 +1604,7 @@ impl<'a> Formatter<'a> { } /// Flags for formatting + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated( since = "1.24.0", @@ -1641,6 +1642,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:G>3}", Foo), "GGG"); /// assert_eq!(&format!("{:t>6}", Foo), "tttttt"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn fill(&self) -> char { self.fill @@ -1677,6 +1679,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:^}", Foo), "center"); /// assert_eq!(&format!("{}", Foo), "into the void"); /// ``` + #[must_use] #[stable(feature = "fmt_flags_align", since = "1.28.0")] pub fn align(&self) -> Option { match self.align { @@ -1711,6 +1714,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:10}", Foo(23)), "Foo(23) "); /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn width(&self) -> Option { self.width @@ -1741,6 +1745,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:.4}", Foo(23.2)), "Foo(23.2000)"); /// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn precision(&self) -> Option { self.precision @@ -1771,6 +1776,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)"); /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 @@ -1799,6 +1805,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:-}", Foo(23)), "-Foo(23)"); /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 @@ -1826,6 +1833,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:#}", Foo(23)), "Foo(23)"); /// assert_eq!(&format!("{}", Foo(23)), "23"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 @@ -1851,6 +1859,7 @@ impl<'a> Formatter<'a> { /// /// assert_eq!(&format!("{:04}", Foo(23)), "23"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_aware_zero_pad(&self) -> bool { self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0 diff --git a/library/core/src/future/mod.rs b/library/core/src/future/mod.rs index cdde094147012..7a3af70d6d97c 100644 --- a/library/core/src/future/mod.rs +++ b/library/core/src/future/mod.rs @@ -90,6 +90,7 @@ where #[lang = "get_context"] #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] +#[must_use] #[inline] pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> { // SAFETY: the caller must guarantee that `cx.0` is a valid pointer diff --git a/library/core/src/iter/sources/empty.rs b/library/core/src/iter/sources/empty.rs index a7d4646f5c597..7abe01d17c90b 100644 --- a/library/core/src/iter/sources/empty.rs +++ b/library/core/src/iter/sources/empty.rs @@ -25,6 +25,7 @@ pub const fn empty() -> Empty { /// An iterator that yields nothing. /// /// This `struct` is created by the [`empty()`] function. See its documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "iter_empty", since = "1.2.0")] pub struct Empty(marker::PhantomData); diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs index 2af61a0748236..8a9ecbe98dff6 100644 --- a/library/core/src/num/error.rs +++ b/library/core/src/num/error.rs @@ -115,6 +115,7 @@ pub enum IntErrorKind { impl ParseIntError { /// Outputs the detailed cause of parsing an integer failing. + #[must_use] #[stable(feature = "int_error_matching", since = "1.55.0")] pub fn kind(&self) -> &IntErrorKind { &self.kind diff --git a/library/core/src/num/f32.rs b/library/core/src/num/f32.rs index 83a922ae34891..905b0c4245801 100644 --- a/library/core/src/num/f32.rs +++ b/library/core/src/num/f32.rs @@ -980,6 +980,7 @@ impl f32 { /// # .all(|(a, b)| a.to_bits() == b.to_bits())) /// ``` #[unstable(feature = "total_cmp", issue = "72599")] + #[must_use] #[inline] pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { let mut left = self.to_bits() as i32; diff --git a/library/core/src/num/f64.rs b/library/core/src/num/f64.rs index 4267260eea38c..112a239a145f6 100644 --- a/library/core/src/num/f64.rs +++ b/library/core/src/num/f64.rs @@ -996,6 +996,7 @@ impl f64 { /// # .all(|(a, b)| a.to_bits() == b.to_bits())) /// ``` #[unstable(feature = "total_cmp", issue = "72599")] + #[must_use] #[inline] pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { let mut left = self.to_bits() as i64; diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs index 347a346359f8c..b74ba92c76eb6 100644 --- a/library/core/src/ops/range.rs +++ b/library/core/src/ops/range.rs @@ -743,6 +743,7 @@ impl Bound<&T> { /// assert_eq!((1..12).start_bound(), Included(&1)); /// assert_eq!((1..12).start_bound().cloned(), Included(1)); /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "bound_cloned", since = "1.55.0")] pub fn cloned(self) -> Bound { match self { diff --git a/library/core/src/option.rs b/library/core/src/option.rs index f4ce7d1dfb334..8a6be7f2b356a 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1451,6 +1451,7 @@ impl Option<&T> { /// let copied = opt_x.copied(); /// assert_eq!(copied, Some(12)); /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "copied", since = "1.35.0")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn copied(self) -> Option { diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index a482414caaf9a..714e9b73c78a6 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -79,6 +79,7 @@ impl<'a> Location<'a> { /// assert_ne!(this_location.line(), another_location.line()); /// assert_ne!(this_location.column(), another_location.column()); /// ``` + #[must_use] #[stable(feature = "track_caller", since = "1.46.0")] #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")] #[track_caller] @@ -119,6 +120,7 @@ impl<'a> Location<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn file(&self) -> &str { self.file @@ -141,6 +143,7 @@ impl<'a> Location<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn line(&self) -> u32 { self.line @@ -163,6 +166,7 @@ impl<'a> Location<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_col", since = "1.25.0")] pub fn column(&self) -> u32 { self.col diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 649bc3e44ad21..d8e421df5de5d 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -81,6 +81,7 @@ impl<'a> PanicInfo<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn payload(&self) -> &(dyn Any + Send) { self.payload @@ -89,6 +90,7 @@ impl<'a> PanicInfo<'a> { /// If the `panic!` macro from the `core` crate (not from `std`) /// was used with a formatting string and some additional arguments, /// returns that message ready to be used for example with [`fmt::write`] + #[must_use] #[unstable(feature = "panic_info_message", issue = "66745")] pub fn message(&self) -> Option<&fmt::Arguments<'_>> { self.message @@ -118,6 +120,7 @@ impl<'a> PanicInfo<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn location(&self) -> Option<&Location<'_>> { // NOTE: If this is changed to sometimes return None, diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 34fc874ada09b..09fc6df542975 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -705,6 +705,7 @@ impl<'a, T: ?Sized> Pin<&'a T> { /// /// ["pinning projections"]: self#projections-and-structural-pinning #[inline(always)] + #[must_use] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin", since = "1.33.0")] pub const fn get_ref(self) -> &'a T { diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 07ecae7830351..ad1d6b8b846a0 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -1724,6 +1724,7 @@ impl<'a, T> ChunksExact<'a, T> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. The returned slice has at most `chunk_size-1` /// elements. + #[must_use] #[stable(feature = "chunks_exact", since = "1.31.0")] pub fn remainder(&self) -> &'a [T] { self.rem @@ -2153,6 +2154,7 @@ impl<'a, T, const N: usize> ArrayChunks<'a, T, N> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. The returned slice has at most `N-1` /// elements. + #[must_use] #[unstable(feature = "array_chunks", issue = "74985")] pub fn remainder(&self) -> &'a [T] { self.rem @@ -2728,6 +2730,7 @@ impl<'a, T> RChunksExact<'a, T> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. The returned slice has at most `chunk_size-1` /// elements. + #[must_use] #[stable(feature = "rchunks", since = "1.31.0")] pub fn remainder(&self) -> &'a [T] { self.rem diff --git a/library/core/src/str/error.rs b/library/core/src/str/error.rs index aa735a14cbd8f..b6460d72fef32 100644 --- a/library/core/src/str/error.rs +++ b/library/core/src/str/error.rs @@ -72,6 +72,7 @@ impl Utf8Error { /// assert_eq!(1, error.valid_up_to()); /// ``` #[stable(feature = "utf8_error", since = "1.5.0")] + #[must_use] #[inline] pub fn valid_up_to(&self) -> usize { self.valid_up_to @@ -93,6 +94,7 @@ impl Utf8Error { /// /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html #[stable(feature = "utf8_error_error_len", since = "1.20.0")] + #[must_use] #[inline] pub fn error_len(&self) -> Option { self.error_len.map(|len| len as usize) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 3efb9560b15e5..94a534c6e797e 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -213,6 +213,7 @@ impl<'a> CharIndices<'a> { /// assert_eq!(chars.next(), None); /// ``` #[inline] + #[must_use] #[unstable(feature = "char_indices_offset", issue = "83871")] pub fn offset(&self) -> usize { self.front_offset @@ -225,6 +226,7 @@ impl<'a> CharIndices<'a> { /// See its documentation for more. /// /// [`bytes`]: str::bytes +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone, Debug)] pub struct Bytes<'a>(pub(super) Copied>); diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 9c76b350d8342..8f02ecbd009c4 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -498,6 +498,7 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked(begin..end)` instead")] + #[must_use] #[inline] pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str { // SAFETY: the caller must uphold the safety contract for `get_unchecked`; @@ -570,6 +571,7 @@ impl str { /// assert_eq!(" Martin-Löf", last); /// ``` #[inline] + #[must_use] #[stable(feature = "str_split_at", since = "1.4.0")] pub fn split_at(&self, mid: usize) -> (&str, &str) { // is_char_boundary checks that the index is in [0, .len()] @@ -613,6 +615,7 @@ impl str { /// assert_eq!("PER Martin-Löf", s); /// ``` #[inline] + #[must_use] #[stable(feature = "str_split_at", since = "1.4.0")] pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) { // is_char_boundary checks that the index is in [0, .len()] diff --git a/library/core/src/str/validations.rs b/library/core/src/str/validations.rs index f2d1c73780880..9a1cf905e3b02 100644 --- a/library/core/src/str/validations.rs +++ b/library/core/src/str/validations.rs @@ -251,6 +251,7 @@ static UTF8_CHAR_WIDTH: [u8; 256] = [ /// Given a first byte, determines how many bytes are in this UTF-8 character. #[unstable(feature = "str_internals", issue = "none")] +#[must_use] #[inline] pub fn utf8_char_width(b: u8) -> usize { UTF8_CHAR_WIDTH[b as usize] as usize diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 620bff5387994..6cba781c2ed8f 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -167,6 +167,7 @@ impl<'a> Context<'a> { /// Returns a reference to the `Waker` for the current task. #[stable(feature = "futures_api", since = "1.36.0")] + #[must_use] #[inline] pub fn waker(&self) -> &'a Waker { &self.waker @@ -242,6 +243,7 @@ impl Waker { /// /// This function is primarily used for optimization purposes. #[inline] + #[must_use] #[stable(feature = "futures_api", since = "1.36.0")] pub fn will_wake(&self, other: &Waker) -> bool { self.waker == other.waker diff --git a/library/core/src/time.rs b/library/core/src/time.rs index 68b969a65c1b8..38639ace80ffa 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -358,6 +358,7 @@ impl Duration { /// ``` #[stable(feature = "duration_extras", since = "1.27.0")] #[rustc_const_stable(feature = "duration_extras", since = "1.32.0")] + #[must_use] #[inline] pub const fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI @@ -380,6 +381,7 @@ impl Duration { /// ``` #[stable(feature = "duration_extras", since = "1.27.0")] #[rustc_const_stable(feature = "duration_extras", since = "1.32.0")] + #[must_use] #[inline] pub const fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO @@ -402,6 +404,7 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[rustc_const_stable(feature = "duration", since = "1.32.0")] + #[must_use] #[inline] pub const fn subsec_nanos(&self) -> u32 { self.nanos