Skip to content

Commit 88df718

Browse files
authored
Rollup merge of rust-lang#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: rust-lang#89692 r? ``@joshtriplett``
2 parents a6ab501 + 06e625f commit 88df718

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

library/alloc/src/collections/linked_list.rs

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

library/alloc/src/rc.rs

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

library/alloc/src/string.rs

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

library/alloc/src/sync.rs

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

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)