Skip to content

Commit 68b0d86

Browse files
committed
Add #[must_use] to remaining core functions
1 parent e1e9319 commit 68b0d86

File tree

25 files changed

+52
-1
lines changed

25 files changed

+52
-1
lines changed

library/alloc/tests/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ fn test_split_at_mut() {
10311031
#[should_panic]
10321032
fn test_split_at_boundscheck() {
10331033
let s = "ศไทย中华Việt Nam";
1034-
s.split_at(1);
1034+
let _ = s.split_at(1);
10351035
}
10361036

10371037
#[test]

library/core/src/alloc/layout.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl Layout {
104104
/// The minimum size in bytes for a memory block of this layout.
105105
#[stable(feature = "alloc_layout", since = "1.28.0")]
106106
#[rustc_const_stable(feature = "const_alloc_layout", since = "1.50.0")]
107+
#[must_use]
107108
#[inline]
108109
pub const fn size(&self) -> usize {
109110
self.size_
@@ -137,6 +138,7 @@ impl Layout {
137138
/// allocate backing structure for `T` (which could be a trait
138139
/// or other unsized type like a slice).
139140
#[stable(feature = "alloc_layout", since = "1.28.0")]
141+
#[must_use]
140142
#[inline]
141143
pub fn for_value<T: ?Sized>(t: &T) -> Self {
142144
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
@@ -171,6 +173,7 @@ impl Layout {
171173
/// [trait object]: ../../book/ch17-02-trait-objects.html
172174
/// [extern type]: ../../unstable-book/language-features/extern-types.html
173175
#[unstable(feature = "layout_for_ptr", issue = "69835")]
176+
#[must_use]
174177
pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
175178
// SAFETY: we pass along the prerequisites of these functions to the caller
176179
let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) };
@@ -187,6 +190,7 @@ impl Layout {
187190
/// some other means.
188191
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
189192
#[rustc_const_unstable(feature = "alloc_layout_extra", issue = "55724")]
193+
#[must_use]
190194
#[inline]
191195
pub const fn dangling(&self) -> NonNull<u8> {
192196
// SAFETY: align is guaranteed to be non-zero

library/core/src/any.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ impl TypeId {
458458
/// assert_eq!(is_string(&0), false);
459459
/// assert_eq!(is_string(&"cookie monster".to_string()), true);
460460
/// ```
461+
#[must_use]
461462
#[stable(feature = "rust1", since = "1.0.0")]
462463
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
463464
pub const fn of<T: ?Sized + 'static>() -> TypeId {
@@ -492,6 +493,7 @@ impl TypeId {
492493
/// "core::option::Option<alloc::string::String>",
493494
/// );
494495
/// ```
496+
#[must_use]
495497
#[stable(feature = "type_name", since = "1.38.0")]
496498
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
497499
pub const fn type_name<T: ?Sized>() -> &'static str {
@@ -534,6 +536,7 @@ pub const fn type_name<T: ?Sized>() -> &'static str {
534536
/// let y = 1.0;
535537
/// println!("{}", type_name_of_val(&y));
536538
/// ```
539+
#[must_use]
537540
#[unstable(feature = "type_name_of_val", issue = "66359")]
538541
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
539542
pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {

library/core/src/ascii.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::str::from_utf8_unchecked;
1818
///
1919
/// This `struct` is created by the [`escape_default`] function. See its
2020
/// documentation for more.
21+
#[must_use = "iterators are lazy and do nothing unless consumed"]
2122
#[stable(feature = "rust1", since = "1.0.0")]
2223
#[derive(Clone)]
2324
pub struct EscapeDefault {

library/core/src/cell.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ impl<'b, T: ?Sized> Ref<'b, T> {
13331333
/// with the widespread use of `r.borrow().clone()` to clone the contents of
13341334
/// a `RefCell`.
13351335
#[stable(feature = "cell_extras", since = "1.15.0")]
1336+
#[must_use]
13361337
#[inline]
13371338
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
13381339
Ref { value: orig.value, borrow: orig.borrow.clone() }

library/core/src/char/decode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
128128

129129
impl DecodeUtf16Error {
130130
/// Returns the unpaired surrogate which caused this error.
131+
#[must_use]
131132
#[stable(feature = "decode_utf16", since = "1.9.0")]
132133
pub fn unpaired_surrogate(&self) -> u16 {
133134
self.code

library/core/src/default.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub trait Default: Sized {
155155
/// }
156156
/// ```
157157
#[unstable(feature = "default_free_fn", issue = "73014")]
158+
#[must_use]
158159
#[inline]
159160
pub fn default<T: Default>() -> T {
160161
Default::default()

library/core/src/fmt/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,7 @@ impl<'a> Formatter<'a> {
16181618
}
16191619

16201620
/// Flags for formatting
1621+
#[must_use]
16211622
#[stable(feature = "rust1", since = "1.0.0")]
16221623
#[rustc_deprecated(
16231624
since = "1.24.0",
@@ -1655,6 +1656,7 @@ impl<'a> Formatter<'a> {
16551656
/// assert_eq!(&format!("{:G>3}", Foo), "GGG");
16561657
/// assert_eq!(&format!("{:t>6}", Foo), "tttttt");
16571658
/// ```
1659+
#[must_use]
16581660
#[stable(feature = "fmt_flags", since = "1.5.0")]
16591661
pub fn fill(&self) -> char {
16601662
self.fill
@@ -1691,6 +1693,7 @@ impl<'a> Formatter<'a> {
16911693
/// assert_eq!(&format!("{:^}", Foo), "center");
16921694
/// assert_eq!(&format!("{}", Foo), "into the void");
16931695
/// ```
1696+
#[must_use]
16941697
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
16951698
pub fn align(&self) -> Option<Alignment> {
16961699
match self.align {
@@ -1725,6 +1728,7 @@ impl<'a> Formatter<'a> {
17251728
/// assert_eq!(&format!("{:10}", Foo(23)), "Foo(23) ");
17261729
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
17271730
/// ```
1731+
#[must_use]
17281732
#[stable(feature = "fmt_flags", since = "1.5.0")]
17291733
pub fn width(&self) -> Option<usize> {
17301734
self.width
@@ -1755,6 +1759,7 @@ impl<'a> Formatter<'a> {
17551759
/// assert_eq!(&format!("{:.4}", Foo(23.2)), "Foo(23.2000)");
17561760
/// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)");
17571761
/// ```
1762+
#[must_use]
17581763
#[stable(feature = "fmt_flags", since = "1.5.0")]
17591764
pub fn precision(&self) -> Option<usize> {
17601765
self.precision
@@ -1785,6 +1790,7 @@ impl<'a> Formatter<'a> {
17851790
/// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)");
17861791
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
17871792
/// ```
1793+
#[must_use]
17881794
#[stable(feature = "fmt_flags", since = "1.5.0")]
17891795
pub fn sign_plus(&self) -> bool {
17901796
self.flags & (1 << FlagV1::SignPlus as u32) != 0
@@ -1813,6 +1819,7 @@ impl<'a> Formatter<'a> {
18131819
/// assert_eq!(&format!("{:-}", Foo(23)), "-Foo(23)");
18141820
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
18151821
/// ```
1822+
#[must_use]
18161823
#[stable(feature = "fmt_flags", since = "1.5.0")]
18171824
pub fn sign_minus(&self) -> bool {
18181825
self.flags & (1 << FlagV1::SignMinus as u32) != 0
@@ -1840,6 +1847,7 @@ impl<'a> Formatter<'a> {
18401847
/// assert_eq!(&format!("{:#}", Foo(23)), "Foo(23)");
18411848
/// assert_eq!(&format!("{}", Foo(23)), "23");
18421849
/// ```
1850+
#[must_use]
18431851
#[stable(feature = "fmt_flags", since = "1.5.0")]
18441852
pub fn alternate(&self) -> bool {
18451853
self.flags & (1 << FlagV1::Alternate as u32) != 0
@@ -1865,6 +1873,7 @@ impl<'a> Formatter<'a> {
18651873
///
18661874
/// assert_eq!(&format!("{:04}", Foo(23)), "23");
18671875
/// ```
1876+
#[must_use]
18681877
#[stable(feature = "fmt_flags", since = "1.5.0")]
18691878
pub fn sign_aware_zero_pad(&self) -> bool {
18701879
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0

library/core/src/future/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ where
9090
#[lang = "get_context"]
9191
#[doc(hidden)]
9292
#[unstable(feature = "gen_future", issue = "50547")]
93+
#[must_use]
9394
#[inline]
9495
pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
9596
// SAFETY: the caller must guarantee that `cx.0` is a valid pointer

library/core/src/iter/sources/empty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub const fn empty<T>() -> Empty<T> {
2525
/// An iterator that yields nothing.
2626
///
2727
/// This `struct` is created by the [`empty()`] function. See its documentation for more.
28+
#[must_use = "iterators are lazy and do nothing unless consumed"]
2829
#[stable(feature = "iter_empty", since = "1.2.0")]
2930
pub struct Empty<T>(marker::PhantomData<T>);
3031

0 commit comments

Comments
 (0)