Skip to content

Commit 95750ae

Browse files
authored
Rollup merge of #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: #89692 r? ```@joshtriplett```
2 parents e79e9f5 + 68b0d86 commit 95750ae

File tree

25 files changed

+52
-1
lines changed

25 files changed

+52
-1
lines changed

library/alloc/tests/str.rs

+1-1
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

+4
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

+3
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

+1
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

+1
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

+1
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

+1
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

+9
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

+1
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

+1
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

library/core/src/num/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub enum IntErrorKind {
115115

116116
impl ParseIntError {
117117
/// Outputs the detailed cause of parsing an integer failing.
118+
#[must_use]
118119
#[stable(feature = "int_error_matching", since = "1.55.0")]
119120
pub fn kind(&self) -> &IntErrorKind {
120121
&self.kind

library/core/src/num/f32.rs

+1
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ impl f32 {
980980
/// # .all(|(a, b)| a.to_bits() == b.to_bits()))
981981
/// ```
982982
#[unstable(feature = "total_cmp", issue = "72599")]
983+
#[must_use]
983984
#[inline]
984985
pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering {
985986
let mut left = self.to_bits() as i32;

library/core/src/num/f64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ impl f64 {
996996
/// # .all(|(a, b)| a.to_bits() == b.to_bits()))
997997
/// ```
998998
#[unstable(feature = "total_cmp", issue = "72599")]
999+
#[must_use]
9991000
#[inline]
10001001
pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering {
10011002
let mut left = self.to_bits() as i64;

library/core/src/ops/range.rs

+1
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ impl<T: Clone> Bound<&T> {
743743
/// assert_eq!((1..12).start_bound(), Included(&1));
744744
/// assert_eq!((1..12).start_bound().cloned(), Included(1));
745745
/// ```
746+
#[must_use = "`self` will be dropped if the result is not used"]
746747
#[stable(feature = "bound_cloned", since = "1.55.0")]
747748
pub fn cloned(self) -> Bound<T> {
748749
match self {

library/core/src/option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@ impl<T: Copy> Option<&T> {
14511451
/// let copied = opt_x.copied();
14521452
/// assert_eq!(copied, Some(12));
14531453
/// ```
1454+
#[must_use = "`self` will be dropped if the result is not used"]
14541455
#[stable(feature = "copied", since = "1.35.0")]
14551456
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
14561457
pub const fn copied(self) -> Option<T> {

library/core/src/panic/location.rs

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl<'a> Location<'a> {
7979
/// assert_ne!(this_location.line(), another_location.line());
8080
/// assert_ne!(this_location.column(), another_location.column());
8181
/// ```
82+
#[must_use]
8283
#[stable(feature = "track_caller", since = "1.46.0")]
8384
#[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
8485
#[track_caller]
@@ -119,6 +120,7 @@ impl<'a> Location<'a> {
119120
///
120121
/// panic!("Normal panic");
121122
/// ```
123+
#[must_use]
122124
#[stable(feature = "panic_hooks", since = "1.10.0")]
123125
pub fn file(&self) -> &str {
124126
self.file
@@ -141,6 +143,7 @@ impl<'a> Location<'a> {
141143
///
142144
/// panic!("Normal panic");
143145
/// ```
146+
#[must_use]
144147
#[stable(feature = "panic_hooks", since = "1.10.0")]
145148
pub fn line(&self) -> u32 {
146149
self.line
@@ -163,6 +166,7 @@ impl<'a> Location<'a> {
163166
///
164167
/// panic!("Normal panic");
165168
/// ```
169+
#[must_use]
166170
#[stable(feature = "panic_col", since = "1.25.0")]
167171
pub fn column(&self) -> u32 {
168172
self.col

library/core/src/panic/panic_info.rs

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl<'a> PanicInfo<'a> {
8181
///
8282
/// panic!("Normal panic");
8383
/// ```
84+
#[must_use]
8485
#[stable(feature = "panic_hooks", since = "1.10.0")]
8586
pub fn payload(&self) -> &(dyn Any + Send) {
8687
self.payload
@@ -89,6 +90,7 @@ impl<'a> PanicInfo<'a> {
8990
/// If the `panic!` macro from the `core` crate (not from `std`)
9091
/// was used with a formatting string and some additional arguments,
9192
/// returns that message ready to be used for example with [`fmt::write`]
93+
#[must_use]
9294
#[unstable(feature = "panic_info_message", issue = "66745")]
9395
pub fn message(&self) -> Option<&fmt::Arguments<'_>> {
9496
self.message
@@ -118,6 +120,7 @@ impl<'a> PanicInfo<'a> {
118120
///
119121
/// panic!("Normal panic");
120122
/// ```
123+
#[must_use]
121124
#[stable(feature = "panic_hooks", since = "1.10.0")]
122125
pub fn location(&self) -> Option<&Location<'_>> {
123126
// NOTE: If this is changed to sometimes return None,

library/core/src/pin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
705705
///
706706
/// ["pinning projections"]: self#projections-and-structural-pinning
707707
#[inline(always)]
708+
#[must_use]
708709
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
709710
#[stable(feature = "pin", since = "1.33.0")]
710711
pub const fn get_ref(self) -> &'a T {

library/core/src/slice/iter.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,7 @@ impl<'a, T> ChunksExact<'a, T> {
17241724
/// Returns the remainder of the original slice that is not going to be
17251725
/// returned by the iterator. The returned slice has at most `chunk_size-1`
17261726
/// elements.
1727+
#[must_use]
17271728
#[stable(feature = "chunks_exact", since = "1.31.0")]
17281729
pub fn remainder(&self) -> &'a [T] {
17291730
self.rem
@@ -2153,6 +2154,7 @@ impl<'a, T, const N: usize> ArrayChunks<'a, T, N> {
21532154
/// Returns the remainder of the original slice that is not going to be
21542155
/// returned by the iterator. The returned slice has at most `N-1`
21552156
/// elements.
2157+
#[must_use]
21562158
#[unstable(feature = "array_chunks", issue = "74985")]
21572159
pub fn remainder(&self) -> &'a [T] {
21582160
self.rem
@@ -2728,6 +2730,7 @@ impl<'a, T> RChunksExact<'a, T> {
27282730
/// Returns the remainder of the original slice that is not going to be
27292731
/// returned by the iterator. The returned slice has at most `chunk_size-1`
27302732
/// elements.
2733+
#[must_use]
27312734
#[stable(feature = "rchunks", since = "1.31.0")]
27322735
pub fn remainder(&self) -> &'a [T] {
27332736
self.rem

library/core/src/str/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ impl Utf8Error {
7272
/// assert_eq!(1, error.valid_up_to());
7373
/// ```
7474
#[stable(feature = "utf8_error", since = "1.5.0")]
75+
#[must_use]
7576
#[inline]
7677
pub fn valid_up_to(&self) -> usize {
7778
self.valid_up_to
@@ -93,6 +94,7 @@ impl Utf8Error {
9394
///
9495
/// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
9596
#[stable(feature = "utf8_error_error_len", since = "1.20.0")]
97+
#[must_use]
9698
#[inline]
9799
pub fn error_len(&self) -> Option<usize> {
98100
self.error_len.map(|len| len as usize)

library/core/src/str/iter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl<'a> CharIndices<'a> {
213213
/// assert_eq!(chars.next(), None);
214214
/// ```
215215
#[inline]
216+
#[must_use]
216217
#[unstable(feature = "char_indices_offset", issue = "83871")]
217218
pub fn offset(&self) -> usize {
218219
self.front_offset
@@ -225,6 +226,7 @@ impl<'a> CharIndices<'a> {
225226
/// See its documentation for more.
226227
///
227228
/// [`bytes`]: str::bytes
229+
#[must_use = "iterators are lazy and do nothing unless consumed"]
228230
#[stable(feature = "rust1", since = "1.0.0")]
229231
#[derive(Clone, Debug)]
230232
pub struct Bytes<'a>(pub(super) Copied<slice::Iter<'a, u8>>);

library/core/src/str/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ impl str {
498498
/// ```
499499
#[stable(feature = "rust1", since = "1.0.0")]
500500
#[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked(begin..end)` instead")]
501+
#[must_use]
501502
#[inline]
502503
pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
503504
// SAFETY: the caller must uphold the safety contract for `get_unchecked`;
@@ -570,6 +571,7 @@ impl str {
570571
/// assert_eq!(" Martin-Löf", last);
571572
/// ```
572573
#[inline]
574+
#[must_use]
573575
#[stable(feature = "str_split_at", since = "1.4.0")]
574576
pub fn split_at(&self, mid: usize) -> (&str, &str) {
575577
// is_char_boundary checks that the index is in [0, .len()]
@@ -613,6 +615,7 @@ impl str {
613615
/// assert_eq!("PER Martin-Löf", s);
614616
/// ```
615617
#[inline]
618+
#[must_use]
616619
#[stable(feature = "str_split_at", since = "1.4.0")]
617620
pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
618621
// is_char_boundary checks that the index is in [0, .len()]

library/core/src/str/validations.rs

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ static UTF8_CHAR_WIDTH: [u8; 256] = [
251251

252252
/// Given a first byte, determines how many bytes are in this UTF-8 character.
253253
#[unstable(feature = "str_internals", issue = "none")]
254+
#[must_use]
254255
#[inline]
255256
pub fn utf8_char_width(b: u8) -> usize {
256257
UTF8_CHAR_WIDTH[b as usize] as usize

library/core/src/task/wake.rs

+2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl<'a> Context<'a> {
167167

168168
/// Returns a reference to the `Waker` for the current task.
169169
#[stable(feature = "futures_api", since = "1.36.0")]
170+
#[must_use]
170171
#[inline]
171172
pub fn waker(&self) -> &'a Waker {
172173
&self.waker
@@ -242,6 +243,7 @@ impl Waker {
242243
///
243244
/// This function is primarily used for optimization purposes.
244245
#[inline]
246+
#[must_use]
245247
#[stable(feature = "futures_api", since = "1.36.0")]
246248
pub fn will_wake(&self, other: &Waker) -> bool {
247249
self.waker == other.waker

0 commit comments

Comments
 (0)