Skip to content

Commit 06e625f

Browse files
committed
Add #[must_use] to as_type conversions
1 parent 86d6d2b commit 06e625f

File tree

19 files changed

+54
-0
lines changed

19 files changed

+54
-0
lines changed

library/alloc/src/collections/binary_heap.rs

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

library/alloc/src/collections/linked_list.rs

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

library/alloc/src/rc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,7 @@ impl<T: ?Sized> Weak<T> {
20872087
/// ```
20882088
///
20892089
/// [`null`]: ptr::null
2090+
#[must_use]
20902091
#[stable(feature = "rc_as_ptr", since = "1.45.0")]
20912092
pub fn as_ptr(&self) -> *const T {
20922093
let ptr: *mut RcBox<T> = NonNull::as_ptr(self.ptr);

library/alloc/src/string.rs

+5
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ impl String {
800800
/// assert_eq!("foo", s.as_str());
801801
/// ```
802802
#[inline]
803+
#[must_use]
803804
#[stable(feature = "string_as_str", since = "1.7.0")]
804805
pub fn as_str(&self) -> &str {
805806
self
@@ -820,6 +821,7 @@ impl String {
820821
/// assert_eq!("FOOBAR", s_mut_str);
821822
/// ```
822823
#[inline]
824+
#[must_use]
823825
#[stable(feature = "string_as_str", since = "1.7.0")]
824826
pub fn as_mut_str(&mut self) -> &mut str {
825827
self
@@ -1160,6 +1162,7 @@ impl String {
11601162
/// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
11611163
/// ```
11621164
#[inline]
1165+
#[must_use]
11631166
#[stable(feature = "rust1", since = "1.0.0")]
11641167
pub fn as_bytes(&self) -> &[u8] {
11651168
&self.vec
@@ -1763,6 +1766,7 @@ impl FromUtf8Error {
17631766
///
17641767
/// assert_eq!(&[0, 159], value.unwrap_err().as_bytes());
17651768
/// ```
1769+
#[must_use]
17661770
#[stable(feature = "from_utf8_error_as_bytes", since = "1.26.0")]
17671771
pub fn as_bytes(&self) -> &[u8] {
17681772
&self.bytes[..]
@@ -2779,6 +2783,7 @@ impl<'a> Drain<'a> {
27792783
/// let _ = drain.next().unwrap();
27802784
/// assert_eq!(drain.as_str(), "bc");
27812785
/// ```
2786+
#[must_use]
27822787
#[stable(feature = "string_drain_as_str", since = "1.55.0")]
27832788
pub fn as_str(&self) -> &str {
27842789
self.iter.as_str()

library/alloc/src/sync.rs

+2
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ impl<T: ?Sized> Arc<T> {
822822
/// assert_eq!(x_ptr, Arc::as_ptr(&y));
823823
/// assert_eq!(unsafe { &*x_ptr }, "hello");
824824
/// ```
825+
#[must_use]
825826
#[stable(feature = "rc_as_ptr", since = "1.45.0")]
826827
pub fn as_ptr(this: &Self) -> *const T {
827828
let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
@@ -1718,6 +1719,7 @@ impl<T: ?Sized> Weak<T> {
17181719
/// ```
17191720
///
17201721
/// [`null`]: core::ptr::null "ptr::null"
1722+
#[must_use]
17211723
#[stable(feature = "weak_into_raw", since = "1.45.0")]
17221724
pub fn as_ptr(&self) -> *const T {
17231725
let ptr: *mut ArcInner<T> = NonNull::as_ptr(self.ptr);

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()

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) {

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`.

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`.

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

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()

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 {

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

library/core/src/time.rs

+6
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ impl Duration {
329329
/// [`subsec_nanos`]: Duration::subsec_nanos
330330
#[stable(feature = "duration", since = "1.3.0")]
331331
#[rustc_const_stable(feature = "duration", since = "1.32.0")]
332+
#[must_use]
332333
#[inline]
333334
pub const fn as_secs(&self) -> u64 {
334335
self.secs
@@ -412,6 +413,7 @@ impl Duration {
412413
/// ```
413414
#[stable(feature = "duration_as_u128", since = "1.33.0")]
414415
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
416+
#[must_use]
415417
#[inline]
416418
pub const fn as_millis(&self) -> u128 {
417419
self.secs as u128 * MILLIS_PER_SEC as u128 + (self.nanos / NANOS_PER_MILLI) as u128
@@ -429,6 +431,7 @@ impl Duration {
429431
/// ```
430432
#[stable(feature = "duration_as_u128", since = "1.33.0")]
431433
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
434+
#[must_use]
432435
#[inline]
433436
pub const fn as_micros(&self) -> u128 {
434437
self.secs as u128 * MICROS_PER_SEC as u128 + (self.nanos / NANOS_PER_MICRO) as u128
@@ -446,6 +449,7 @@ impl Duration {
446449
/// ```
447450
#[stable(feature = "duration_as_u128", since = "1.33.0")]
448451
#[rustc_const_stable(feature = "duration_as_u128", since = "1.33.0")]
452+
#[must_use]
449453
#[inline]
450454
pub const fn as_nanos(&self) -> u128 {
451455
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128
@@ -669,6 +673,7 @@ impl Duration {
669673
/// assert_eq!(dur.as_secs_f64(), 2.7);
670674
/// ```
671675
#[stable(feature = "duration_float", since = "1.38.0")]
676+
#[must_use]
672677
#[inline]
673678
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
674679
pub const fn as_secs_f64(&self) -> f64 {
@@ -687,6 +692,7 @@ impl Duration {
687692
/// assert_eq!(dur.as_secs_f32(), 2.7);
688693
/// ```
689694
#[stable(feature = "duration_float", since = "1.38.0")]
695+
#[must_use]
690696
#[inline]
691697
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
692698
pub const fn as_secs_f32(&self) -> f32 {

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
}
@@ -616,6 +617,7 @@ impl CString {
616617
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
617618
/// ```
618619
#[inline]
620+
#[must_use]
619621
#[stable(feature = "rust1", since = "1.0.0")]
620622
pub fn as_bytes(&self) -> &[u8] {
621623
// SAFETY: CString has a length at least 1
@@ -635,6 +637,7 @@ impl CString {
635637
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
636638
/// ```
637639
#[inline]
640+
#[must_use]
638641
#[stable(feature = "rust1", since = "1.0.0")]
639642
pub fn as_bytes_with_nul(&self) -> &[u8] {
640643
&self.inner
@@ -653,6 +656,7 @@ impl CString {
653656
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
654657
/// ```
655658
#[inline]
659+
#[must_use]
656660
#[stable(feature = "as_c_str", since = "1.20.0")]
657661
pub fn as_c_str(&self) -> &CStr {
658662
&*self
@@ -1308,6 +1312,7 @@ impl CStr {
13081312
/// This way, the lifetime of the [`CString`] in `hello` encompasses
13091313
/// the lifetime of `ptr` and the `unsafe` block.
13101314
#[inline]
1315+
#[must_use]
13111316
#[stable(feature = "rust1", since = "1.0.0")]
13121317
#[rustc_const_stable(feature = "const_str_as_ptr", since = "1.32.0")]
13131318
pub const fn as_ptr(&self) -> *const c_char {

library/std/src/ffi/os_str.rs

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

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)