Skip to content

Commit f0a8a1d

Browse files
authored
Rollup merge of rust-lang#89897 - jkugelman:must-use-core, r=joshtriplett
Add #[must_use] to remaining core functions I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is everything remaining from the `core` crate. Ignored by clippy for reasons unknown: ```rust core::alloc::Layout unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self; core::any const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str; ``` Ignored by clippy because of `mut`: ```rust str fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str); ``` <del> Ignored by clippy presumably because a caller might want `f` called for side effects. That seems like a bad usage of `map` to me. ```rust core::cell::Ref<'b, T> fn map<U: ?Sized, F>(orig: Ref<'b, T>, f: F) -> Ref<'b, T>; core::cell::Ref<'b, T> fn map_split<U: ?Sized, V: ?Sized, F>(orig: Ref<'b, T>, f: F) -> (Ref<'b, U>, Ref<'b, V>); ``` </del> Parent issue: rust-lang#89692 r? `@joshtriplett`
2 parents 836c04c + 68b0d86 commit f0a8a1d

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
@@ -1335,6 +1335,7 @@ impl<'b, T: ?Sized> Ref<'b, T> {
13351335
/// with the widespread use of `r.borrow().clone()` to clone the contents of
13361336
/// a `RefCell`.
13371337
#[stable(feature = "cell_extras", since = "1.15.0")]
1338+
#[must_use]
13381339
#[inline]
13391340
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
13401341
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
@@ -1604,6 +1604,7 @@ impl<'a> Formatter<'a> {
16041604
}
16051605

16061606
/// Flags for formatting
1607+
#[must_use]
16071608
#[stable(feature = "rust1", since = "1.0.0")]
16081609
#[rustc_deprecated(
16091610
since = "1.24.0",
@@ -1641,6 +1642,7 @@ impl<'a> Formatter<'a> {
16411642
/// assert_eq!(&format!("{:G>3}", Foo), "GGG");
16421643
/// assert_eq!(&format!("{:t>6}", Foo), "tttttt");
16431644
/// ```
1645+
#[must_use]
16441646
#[stable(feature = "fmt_flags", since = "1.5.0")]
16451647
pub fn fill(&self) -> char {
16461648
self.fill
@@ -1677,6 +1679,7 @@ impl<'a> Formatter<'a> {
16771679
/// assert_eq!(&format!("{:^}", Foo), "center");
16781680
/// assert_eq!(&format!("{}", Foo), "into the void");
16791681
/// ```
1682+
#[must_use]
16801683
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
16811684
pub fn align(&self) -> Option<Alignment> {
16821685
match self.align {
@@ -1711,6 +1714,7 @@ impl<'a> Formatter<'a> {
17111714
/// assert_eq!(&format!("{:10}", Foo(23)), "Foo(23) ");
17121715
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
17131716
/// ```
1717+
#[must_use]
17141718
#[stable(feature = "fmt_flags", since = "1.5.0")]
17151719
pub fn width(&self) -> Option<usize> {
17161720
self.width
@@ -1741,6 +1745,7 @@ impl<'a> Formatter<'a> {
17411745
/// assert_eq!(&format!("{:.4}", Foo(23.2)), "Foo(23.2000)");
17421746
/// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)");
17431747
/// ```
1748+
#[must_use]
17441749
#[stable(feature = "fmt_flags", since = "1.5.0")]
17451750
pub fn precision(&self) -> Option<usize> {
17461751
self.precision
@@ -1771,6 +1776,7 @@ impl<'a> Formatter<'a> {
17711776
/// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)");
17721777
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
17731778
/// ```
1779+
#[must_use]
17741780
#[stable(feature = "fmt_flags", since = "1.5.0")]
17751781
pub fn sign_plus(&self) -> bool {
17761782
self.flags & (1 << FlagV1::SignPlus as u32) != 0
@@ -1799,6 +1805,7 @@ impl<'a> Formatter<'a> {
17991805
/// assert_eq!(&format!("{:-}", Foo(23)), "-Foo(23)");
18001806
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
18011807
/// ```
1808+
#[must_use]
18021809
#[stable(feature = "fmt_flags", since = "1.5.0")]
18031810
pub fn sign_minus(&self) -> bool {
18041811
self.flags & (1 << FlagV1::SignMinus as u32) != 0
@@ -1826,6 +1833,7 @@ impl<'a> Formatter<'a> {
18261833
/// assert_eq!(&format!("{:#}", Foo(23)), "Foo(23)");
18271834
/// assert_eq!(&format!("{}", Foo(23)), "23");
18281835
/// ```
1836+
#[must_use]
18291837
#[stable(feature = "fmt_flags", since = "1.5.0")]
18301838
pub fn alternate(&self) -> bool {
18311839
self.flags & (1 << FlagV1::Alternate as u32) != 0
@@ -1851,6 +1859,7 @@ impl<'a> Formatter<'a> {
18511859
///
18521860
/// assert_eq!(&format!("{:04}", Foo(23)), "23");
18531861
/// ```
1862+
#[must_use]
18541863
#[stable(feature = "fmt_flags", since = "1.5.0")]
18551864
pub fn sign_aware_zero_pad(&self) -> bool {
18561865
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)