Skip to content

Commit b55a3c5

Browse files
authored
Rollup merge of #89778 - jkugelman:must-use-as_type-conversions, r=joshtriplett
Add #[must_use] to as_type conversions Clippy missed these: ```rust alloc::string::String fn as_mut_str(&mut self) -> &mut str; core::mem::NonNull<T> unsafe fn as_uninit_mut<'a>(&mut self) -> &'a MaybeUninit<T>; str unsafe fn as_bytes_mut(&mut self) -> &mut [u8]; str fn as_mut_ptr(&mut self) -> *mut u8; ``` Parent issue: #89692 r? ````@joshtriplett````
2 parents 9475e60 + 06e625f commit b55a3c5

File tree

19 files changed

+54
-0
lines changed

19 files changed

+54
-0
lines changed

Diff for: library/alloc/src/collections/binary_heap.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ impl<T> BinaryHeap<T> {
10091009
///
10101010
/// io::sink().write(heap.as_slice()).unwrap();
10111011
/// ```
1012+
#[must_use]
10121013
#[unstable(feature = "binary_heap_as_slice", issue = "83659")]
10131014
pub fn as_slice(&self) -> &[T] {
10141015
self.data.as_slice()

Diff for: library/alloc/src/collections/linked_list.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ impl<'a, T> CursorMut<'a, T> {
13851385
/// The lifetime of the returned `Cursor` is bound to that of the
13861386
/// `CursorMut`, which means it cannot outlive the `CursorMut` and that the
13871387
/// `CursorMut` is frozen for the lifetime of the `Cursor`.
1388+
#[must_use]
13881389
#[unstable(feature = "linked_list_cursors", issue = "58533")]
13891390
pub fn as_cursor(&self) -> Cursor<'_, T> {
13901391
Cursor { list: self.list, current: self.current, index: self.index }

Diff for: library/alloc/src/rc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ impl<T: ?Sized> Weak<T> {
20932093
/// ```
20942094
///
20952095
/// [`null`]: ptr::null
2096+
#[must_use]
20962097
#[stable(feature = "rc_as_ptr", since = "1.45.0")]
20972098
pub fn as_ptr(&self) -> *const T {
20982099
let ptr: *mut RcBox<T> = NonNull::as_ptr(self.ptr);

Diff for: library/alloc/src/string.rs

+5
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ impl String {
803803
/// assert_eq!("foo", s.as_str());
804804
/// ```
805805
#[inline]
806+
#[must_use]
806807
#[stable(feature = "string_as_str", since = "1.7.0")]
807808
pub fn as_str(&self) -> &str {
808809
self
@@ -823,6 +824,7 @@ impl String {
823824
/// assert_eq!("FOOBAR", s_mut_str);
824825
/// ```
825826
#[inline]
827+
#[must_use]
826828
#[stable(feature = "string_as_str", since = "1.7.0")]
827829
pub fn as_mut_str(&mut self) -> &mut str {
828830
self
@@ -1163,6 +1165,7 @@ impl String {
11631165
/// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
11641166
/// ```
11651167
#[inline]
1168+
#[must_use]
11661169
#[stable(feature = "rust1", since = "1.0.0")]
11671170
pub fn as_bytes(&self) -> &[u8] {
11681171
&self.vec
@@ -1766,6 +1769,7 @@ impl FromUtf8Error {
17661769
///
17671770
/// assert_eq!(&[0, 159], value.unwrap_err().as_bytes());
17681771
/// ```
1772+
#[must_use]
17691773
#[stable(feature = "from_utf8_error_as_bytes", since = "1.26.0")]
17701774
pub fn as_bytes(&self) -> &[u8] {
17711775
&self.bytes[..]
@@ -2782,6 +2786,7 @@ impl<'a> Drain<'a> {
27822786
/// let _ = drain.next().unwrap();
27832787
/// assert_eq!(drain.as_str(), "bc");
27842788
/// ```
2789+
#[must_use]
27852790
#[stable(feature = "string_drain_as_str", since = "1.55.0")]
27862791
pub fn as_str(&self) -> &str {
27872792
self.iter.as_str()

Diff for: library/alloc/src/sync.rs

+2
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ impl<T: ?Sized> Arc<T> {
827827
/// assert_eq!(x_ptr, Arc::as_ptr(&y));
828828
/// assert_eq!(unsafe { &*x_ptr }, "hello");
829829
/// ```
830+
#[must_use]
830831
#[stable(feature = "rc_as_ptr", since = "1.45.0")]
831832
pub fn as_ptr(this: &Self) -> *const T {
832833
let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
@@ -1724,6 +1725,7 @@ impl<T: ?Sized> Weak<T> {
17241725
/// ```
17251726
///
17261727
/// [`null`]: core::ptr::null "ptr::null"
1728+
#[must_use]
17271729
#[stable(feature = "weak_into_raw", since = "1.45.0")]
17281730
pub fn as_ptr(&self) -> *const T {
17291731
let ptr: *mut ArcInner<T> = NonNull::as_ptr(self.ptr);

Diff for: library/alloc/src/vec/drain.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
5252
/// let _ = drain.next().unwrap();
5353
/// assert_eq!(drain.as_slice(), &['b', 'c']);
5454
/// ```
55+
#[must_use]
5556
#[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
5657
pub fn as_slice(&self) -> &[T] {
5758
self.iter.as_slice()

Diff for: library/core/src/fmt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ impl<'a> Arguments<'a> {
491491
/// ```
492492
#[stable(feature = "fmt_as_str", since = "1.52.0")]
493493
#[rustc_const_unstable(feature = "const_arguments_as_str", issue = "none")]
494+
#[must_use]
494495
#[inline]
495496
pub const fn as_str(&self) -> Option<&'static str> {
496497
match (self.pieces, self.args) {

Diff for: library/core/src/option.rs

+2
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ impl<T> Option<T> {
657657
///
658658
/// [&]: reference "shared reference"
659659
#[inline]
660+
#[must_use]
660661
#[stable(feature = "pin", since = "1.33.0")]
661662
pub fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>> {
662663
// SAFETY: `x` is guaranteed to be pinned because it comes from `self`
@@ -668,6 +669,7 @@ impl<T> Option<T> {
668669
///
669670
/// [&mut]: reference "mutable reference"
670671
#[inline]
672+
#[must_use]
671673
#[stable(feature = "pin", since = "1.33.0")]
672674
pub fn as_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>> {
673675
// SAFETY: `get_unchecked_mut` is never used to move the `Option` inside `self`.

Diff for: library/core/src/ptr/non_null.rs

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl<T: Sized> NonNull<T> {
119119
///
120120
/// [the module documentation]: crate::ptr#safety
121121
#[inline]
122+
#[must_use]
122123
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
123124
pub unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
124125
// SAFETY: the caller must guarantee that `self` meets all the
@@ -151,6 +152,7 @@ impl<T: Sized> NonNull<T> {
151152
///
152153
/// [the module documentation]: crate::ptr#safety
153154
#[inline]
155+
#[must_use]
154156
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
155157
pub unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
156158
// SAFETY: the caller must guarantee that `self` meets all the
@@ -264,6 +266,7 @@ impl<T: ?Sized> NonNull<T> {
264266
/// ```
265267
#[stable(feature = "nonnull", since = "1.25.0")]
266268
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
269+
#[must_use]
267270
#[inline]
268271
pub const fn as_ptr(self) -> *mut T {
269272
self.pointer as *mut T
@@ -310,6 +313,7 @@ impl<T: ?Sized> NonNull<T> {
310313
///
311314
/// [the module documentation]: crate::ptr#safety
312315
#[stable(feature = "nonnull", since = "1.25.0")]
316+
#[must_use]
313317
#[inline]
314318
pub unsafe fn as_ref<'a>(&self) -> &'a T {
315319
// SAFETY: the caller must guarantee that `self` meets all the
@@ -359,6 +363,7 @@ impl<T: ?Sized> NonNull<T> {
359363
///
360364
/// [the module documentation]: crate::ptr#safety
361365
#[stable(feature = "nonnull", since = "1.25.0")]
366+
#[must_use]
362367
#[inline]
363368
pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
364369
// SAFETY: the caller must guarantee that `self` meets all the
@@ -455,6 +460,7 @@ impl<T> NonNull<[T]> {
455460
/// assert_eq!(slice.as_non_null_ptr(), NonNull::new(1 as *mut i8).unwrap());
456461
/// ```
457462
#[inline]
463+
#[must_use]
458464
#[unstable(feature = "slice_ptr_get", issue = "74265")]
459465
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
460466
pub const fn as_non_null_ptr(self) -> NonNull<T> {
@@ -474,6 +480,7 @@ impl<T> NonNull<[T]> {
474480
/// assert_eq!(slice.as_mut_ptr(), 1 as *mut i8);
475481
/// ```
476482
#[inline]
483+
#[must_use]
477484
#[unstable(feature = "slice_ptr_get", issue = "74265")]
478485
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
479486
pub const fn as_mut_ptr(self) -> *mut T {
@@ -518,6 +525,7 @@ impl<T> NonNull<[T]> {
518525
///
519526
/// [valid]: crate::ptr#safety
520527
#[inline]
528+
#[must_use]
521529
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
522530
pub unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
523531
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
@@ -579,6 +587,7 @@ impl<T> NonNull<[T]> {
579587
/// # Ok::<_, std::alloc::AllocError>(())
580588
/// ```
581589
#[inline]
590+
#[must_use]
582591
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
583592
pub unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
584593
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.

Diff for: library/core/src/ptr/unique.rs

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl<T: ?Sized> Unique<T> {
112112
/// The resulting lifetime is bound to self so this behaves "as if"
113113
/// it were actually an instance of T that is getting borrowed. If a longer
114114
/// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
115+
#[must_use]
115116
#[inline]
116117
pub unsafe fn as_ref(&self) -> &T {
117118
// SAFETY: the caller must guarantee that `self` meets all the
@@ -124,6 +125,7 @@ impl<T: ?Sized> Unique<T> {
124125
/// The resulting lifetime is bound to self so this behaves "as if"
125126
/// it were actually an instance of T that is getting borrowed. If a longer
126127
/// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
128+
#[must_use]
127129
#[inline]
128130
pub unsafe fn as_mut(&mut self) -> &mut T {
129131
// SAFETY: the caller must guarantee that `self` meets all the

Diff for: library/core/src/slice/iter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<'a, T> Iter<'a, T> {
124124
/// // Now `as_slice` returns "[2, 3]":
125125
/// println!("{:?}", iter.as_slice());
126126
/// ```
127+
#[must_use]
127128
#[stable(feature = "iter_to_slice", since = "1.4.0")]
128129
pub fn as_slice(&self) -> &'a [T] {
129130
self.make_slice()
@@ -298,6 +299,7 @@ impl<'a, T> IterMut<'a, T> {
298299
/// // Now `as_slice` returns "[2, 3]":
299300
/// assert_eq!(iter.as_slice(), &[2, 3]);
300301
/// ```
302+
#[must_use]
301303
#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
302304
pub fn as_slice(&self) -> &[T] {
303305
self.make_slice()

Diff for: library/core/src/str/iter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl<'a> Chars<'a> {
109109
/// assert_eq!(chars.as_str(), "");
110110
/// ```
111111
#[stable(feature = "iter_to_slice", since = "1.4.0")]
112+
#[must_use]
112113
#[inline]
113114
pub fn as_str(&self) -> &'a str {
114115
// SAFETY: `Chars` is only made from a str, which guarantees the iter is valid UTF-8.
@@ -185,6 +186,7 @@ impl<'a> CharIndices<'a> {
185186
/// This has the same lifetime as the original slice, and so the
186187
/// iterator can continue to be used while this exists.
187188
#[stable(feature = "iter_to_slice", since = "1.4.0")]
189+
#[must_use]
188190
#[inline]
189191
pub fn as_str(&self) -> &'a str {
190192
self.iter.as_str()
@@ -1247,6 +1249,7 @@ impl<'a> SplitWhitespace<'a> {
12471249
/// assert_eq!(split.as_str(), "");
12481250
/// ```
12491251
#[inline]
1252+
#[must_use]
12501253
#[unstable(feature = "str_split_whitespace_as_str", issue = "77998")]
12511254
pub fn as_str(&self) -> &'a str {
12521255
self.inner.iter.as_str()
@@ -1302,6 +1305,7 @@ impl<'a> SplitAsciiWhitespace<'a> {
13021305
/// assert_eq!(split.as_str(), "");
13031306
/// ```
13041307
#[inline]
1308+
#[must_use]
13051309
#[unstable(feature = "str_split_whitespace_as_str", issue = "77998")]
13061310
pub fn as_str(&self) -> &'a str {
13071311
if self.inner.iter.iter.finished {

Diff for: library/core/src/str/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl str {
230230
/// ```
231231
#[stable(feature = "rust1", since = "1.0.0")]
232232
#[rustc_const_stable(feature = "str_as_bytes", since = "1.39.0")]
233+
#[must_use]
233234
#[inline(always)]
234235
#[allow(unused_attributes)]
235236
pub const fn as_bytes(&self) -> &[u8] {
@@ -274,6 +275,7 @@ impl str {
274275
/// assert_eq!("🍔∈🌏", s);
275276
/// ```
276277
#[stable(feature = "str_mut_extras", since = "1.20.0")]
278+
#[must_use]
277279
#[inline(always)]
278280
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
279281
// SAFETY: the cast from `&str` to `&[u8]` is safe since `str`
@@ -304,6 +306,7 @@ impl str {
304306
/// ```
305307
#[stable(feature = "rust1", since = "1.0.0")]
306308
#[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
309+
#[must_use]
307310
#[inline]
308311
pub const fn as_ptr(&self) -> *const u8 {
309312
self as *const str as *const u8
@@ -318,6 +321,7 @@ impl str {
318321
/// It is your responsibility to make sure that the string slice only gets
319322
/// modified in a way that it remains valid UTF-8.
320323
#[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
324+
#[must_use]
321325
#[inline]
322326
pub fn as_mut_ptr(&mut self) -> *mut u8 {
323327
self as *mut str as *mut u8

Diff for: library/core/src/time.rs

+6
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl Duration {
334334
/// [`subsec_nanos`]: Duration::subsec_nanos
335335
#[stable(feature = "duration", since = "1.3.0")]
336336
#[rustc_const_stable(feature = "duration", since = "1.32.0")]
337+
#[must_use]
337338
#[inline]
338339
pub const fn as_secs(&self) -> u64 {
339340
self.secs
@@ -417,6 +418,7 @@ impl Duration {
417418
/// ```
418419
#[stable(feature = "duration_as_u128", since = "1.33.0")]
419420
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
421+
#[must_use]
420422
#[inline]
421423
pub const fn as_millis(&self) -> u128 {
422424
self.secs as u128 * MILLIS_PER_SEC as u128 + (self.nanos / NANOS_PER_MILLI) as u128
@@ -434,6 +436,7 @@ impl Duration {
434436
/// ```
435437
#[stable(feature = "duration_as_u128", since = "1.33.0")]
436438
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
439+
#[must_use]
437440
#[inline]
438441
pub const fn as_micros(&self) -> u128 {
439442
self.secs as u128 * MICROS_PER_SEC as u128 + (self.nanos / NANOS_PER_MICRO) as u128
@@ -451,6 +454,7 @@ impl Duration {
451454
/// ```
452455
#[stable(feature = "duration_as_u128", since = "1.33.0")]
453456
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
457+
#[must_use]
454458
#[inline]
455459
pub const fn as_nanos(&self) -> u128 {
456460
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128
@@ -674,6 +678,7 @@ impl Duration {
674678
/// assert_eq!(dur.as_secs_f64(), 2.7);
675679
/// ```
676680
#[stable(feature = "duration_float", since = "1.38.0")]
681+
#[must_use]
677682
#[inline]
678683
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
679684
pub const fn as_secs_f64(&self) -> f64 {
@@ -692,6 +697,7 @@ impl Duration {
692697
/// assert_eq!(dur.as_secs_f32(), 2.7);
693698
/// ```
694699
#[stable(feature = "duration_float", since = "1.38.0")]
700+
#[must_use]
695701
#[inline]
696702
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
697703
pub const fn as_secs_f32(&self) -> f32 {

Diff for: library/std/src/ffi/c_str.rs

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl FromVecWithNulError {
297297
///
298298
/// assert_eq!(&bytes[..], value.unwrap_err().as_bytes());
299299
/// ```
300+
#[must_use]
300301
pub fn as_bytes(&self) -> &[u8] {
301302
&self.bytes[..]
302303
}
@@ -618,6 +619,7 @@ impl CString {
618619
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
619620
/// ```
620621
#[inline]
622+
#[must_use]
621623
#[stable(feature = "rust1", since = "1.0.0")]
622624
pub fn as_bytes(&self) -> &[u8] {
623625
// SAFETY: CString has a length at least 1
@@ -637,6 +639,7 @@ impl CString {
637639
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
638640
/// ```
639641
#[inline]
642+
#[must_use]
640643
#[stable(feature = "rust1", since = "1.0.0")]
641644
pub fn as_bytes_with_nul(&self) -> &[u8] {
642645
&self.inner
@@ -655,6 +658,7 @@ impl CString {
655658
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
656659
/// ```
657660
#[inline]
661+
#[must_use]
658662
#[stable(feature = "as_c_str", since = "1.20.0")]
659663
pub fn as_c_str(&self) -> &CStr {
660664
&*self
@@ -1313,6 +1317,7 @@ impl CStr {
13131317
/// This way, the lifetime of the [`CString`] in `hello` encompasses
13141318
/// the lifetime of `ptr` and the `unsafe` block.
13151319
#[inline]
1320+
#[must_use]
13161321
#[stable(feature = "rust1", since = "1.0.0")]
13171322
#[rustc_const_stable(feature = "const_str_as_ptr", since = "1.32.0")]
13181323
pub const fn as_ptr(&self) -> *const c_char {

Diff for: library/std/src/ffi/os_str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl OsString {
137137
/// assert_eq!(os_string.as_os_str(), os_str);
138138
/// ```
139139
#[stable(feature = "rust1", since = "1.0.0")]
140+
#[must_use]
140141
#[inline]
141142
pub fn as_os_str(&self) -> &OsStr {
142143
self

Diff for: library/std/src/os/unix/net/addr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl SocketAddr {
192192
/// }
193193
/// ```
194194
#[stable(feature = "unix_socket", since = "1.10.0")]
195+
#[must_use]
195196
pub fn as_pathname(&self) -> Option<&Path> {
196197
if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
197198
}

0 commit comments

Comments
 (0)