Skip to content

Commit a1bdd48

Browse files
authored
Rollup merge of #89796 - jkugelman:must-use-non-mutating-verb-methods, r=joshtriplett
Add #[must_use] to non-mutating verb methods These are methods that could be misconstrued to mutate their input, similar to #89694. I gave each one a different custom message. I wrote that `upgrade` and `downgrade` don't modify the input pointers. Logically they don't, but technically they do... Parent issue: #89692 r? ```@joshtriplett```
2 parents 4ce1ce1 + c3f0577 commit a1bdd48

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

library/alloc/src/rc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,8 @@ impl<T: ?Sized> Weak<T> {
22302230
///
22312231
/// assert!(weak_five.upgrade().is_none());
22322232
/// ```
2233+
#[must_use = "this returns a new `Rc`, \
2234+
without modifying the original weak pointer"]
22332235
#[stable(feature = "rc_weak", since = "1.4.0")]
22342236
pub fn upgrade(&self) -> Option<Rc<T>> {
22352237
let inner = self.inner()?;

library/alloc/src/sync.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ macro_rules! acquire {
146146
/// use std::sync::Arc;
147147
///
148148
/// let my_arc = Arc::new(());
149-
/// Arc::downgrade(&my_arc);
149+
/// let my_weak = Arc::downgrade(&my_arc);
150150
/// ```
151151
///
152152
/// `Arc<T>`'s implementations of traits like `Clone` may also be called using
@@ -898,6 +898,8 @@ impl<T: ?Sized> Arc<T> {
898898
///
899899
/// let weak_five = Arc::downgrade(&five);
900900
/// ```
901+
#[must_use = "this returns a new `Weak` pointer, \
902+
without modifying the original `Arc`"]
901903
#[stable(feature = "arc_weak", since = "1.4.0")]
902904
pub fn downgrade(this: &Self) -> Weak<T> {
903905
// This Relaxed is OK because we're checking the value in the CAS
@@ -1863,6 +1865,8 @@ impl<T: ?Sized> Weak<T> {
18631865
///
18641866
/// assert!(weak_five.upgrade().is_none());
18651867
/// ```
1868+
#[must_use = "this returns a new `Arc`, \
1869+
without modifying the original weak pointer"]
18661870
#[stable(feature = "arc_weak", since = "1.4.0")]
18671871
pub fn upgrade(&self) -> Option<Arc<T>> {
18681872
// We use a CAS loop to increment the strong count instead of a

library/core/src/alloc/layout.rs

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ impl Layout {
112112
/// The minimum byte alignment for a memory block of this layout.
113113
#[stable(feature = "alloc_layout", since = "1.28.0")]
114114
#[rustc_const_stable(feature = "const_alloc_layout", since = "1.50.0")]
115+
#[must_use = "this returns the minimum alignment, \
116+
without modifying the layout"]
115117
#[inline]
116118
pub const fn align(&self) -> usize {
117119
self.align_.get()
@@ -229,6 +231,8 @@ impl Layout {
229231
/// satisfy this constraint is to ensure `align <= self.align()`.
230232
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
231233
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
234+
#[must_use = "this returns the padding needed, \
235+
without modifying the `Layout`"]
232236
#[inline]
233237
pub const fn padding_needed_for(&self, align: usize) -> usize {
234238
let len = self.size();
@@ -262,6 +266,8 @@ impl Layout {
262266
/// This is equivalent to adding the result of `padding_needed_for`
263267
/// to the layout's current size.
264268
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
269+
#[must_use = "this returns a new `Layout`, \
270+
without modifying the original"]
265271
#[inline]
266272
pub fn pad_to_align(&self) -> Layout {
267273
let pad = self.padding_needed_for(self.align());

library/std/src/path.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,8 @@ impl Path {
25162516
/// println!("{}", path.display());
25172517
/// ```
25182518
#[stable(feature = "rust1", since = "1.0.0")]
2519+
#[must_use = "this does not display the path, \
2520+
it returns an object that can be displayed"]
25192521
#[inline]
25202522
pub fn display(&self) -> Display<'_> {
25212523
Display { path: self }

0 commit comments

Comments
 (0)