Add #[must_use] to remaining core functions
This commit is contained in:
parent
e1e9319d93
commit
68b0d86294
25 changed files with 52 additions and 1 deletions
|
@ -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]
|
||||
|
|
|
@ -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: ?Sized>(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: ?Sized>(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<u8> {
|
||||
// SAFETY: align is guaranteed to be non-zero
|
||||
|
|
|
@ -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<T: ?Sized + 'static>() -> TypeId {
|
||||
|
@ -492,6 +493,7 @@ impl TypeId {
|
|||
/// "core::option::Option<alloc::string::String>",
|
||||
/// );
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[stable(feature = "type_name", since = "1.38.0")]
|
||||
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
|
||||
pub const fn type_name<T: ?Sized>() -> &'static str {
|
||||
|
@ -534,6 +536,7 @@ pub const fn type_name<T: ?Sized>() -> &'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<T: ?Sized>(_val: &T) -> &'static str {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1333,6 +1333,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() }
|
||||
|
|
|
@ -128,6 +128,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
|
|||
|
||||
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
|
||||
|
|
|
@ -155,6 +155,7 @@ pub trait Default: Sized {
|
|||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "default_free_fn", issue = "73014")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn default<T: Default>() -> T {
|
||||
Default::default()
|
||||
|
|
|
@ -1618,6 +1618,7 @@ impl<'a> Formatter<'a> {
|
|||
}
|
||||
|
||||
/// Flags for formatting
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_deprecated(
|
||||
since = "1.24.0",
|
||||
|
@ -1655,6 +1656,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
|
||||
|
@ -1691,6 +1693,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<Alignment> {
|
||||
match self.align {
|
||||
|
@ -1725,6 +1728,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<usize> {
|
||||
self.width
|
||||
|
@ -1755,6 +1759,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<usize> {
|
||||
self.precision
|
||||
|
@ -1785,6 +1790,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
|
||||
|
@ -1813,6 +1819,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
|
||||
|
@ -1840,6 +1847,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
|
||||
|
@ -1865,6 +1873,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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -25,6 +25,7 @@ pub const fn empty<T>() -> Empty<T> {
|
|||
/// 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<T>(marker::PhantomData<T>);
|
||||
|
||||
|
|
|
@ -114,6 +114,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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -743,6 +743,7 @@ impl<T: Clone> 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<T> {
|
||||
match self {
|
||||
|
|
|
@ -1450,6 +1450,7 @@ impl<T: Copy> 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<T> {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1714,6 +1714,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
|
||||
|
@ -2143,6 +2144,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
|
||||
|
@ -2718,6 +2720,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
|
||||
|
|
|
@ -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<usize> {
|
||||
self.error_len.map(|len| len as usize)
|
||||
|
|
|
@ -211,6 +211,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
|
||||
|
@ -223,6 +224,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<slice::Iter<'a, u8>>);
|
||||
|
|
|
@ -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()]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -357,6 +357,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
|
||||
|
@ -379,6 +380,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
|
||||
|
@ -401,6 +403,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
|
||||
|
|
Loading…
Add table
Reference in a new issue