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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

0 commit comments

Comments
 (0)