Skip to content

Commit

Permalink
Rollup merge of rust-lang#89796 - jkugelman:must-use-non-mutating-ver…
Browse files Browse the repository at this point in the history
…b-methods, r=joshtriplett

Add #[must_use] to non-mutating verb methods

These are methods that could be misconstrued to mutate their input, similar to rust-lang#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: rust-lang#89692

r? ``@joshtriplett``
  • Loading branch information
the8472 committed Oct 12, 2021
2 parents 84e3828 + c3f0577 commit efad99d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,8 @@ impl<T: ?Sized> Weak<T> {
///
/// assert!(weak_five.upgrade().is_none());
/// ```
#[must_use = "this returns a new `Rc`, \
without modifying the original weak pointer"]
#[stable(feature = "rc_weak", since = "1.4.0")]
pub fn upgrade(&self) -> Option<Rc<T>> {
let inner = self.inner()?;
Expand Down
6 changes: 5 additions & 1 deletion library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ macro_rules! acquire {
/// use std::sync::Arc;
///
/// let my_arc = Arc::new(());
/// Arc::downgrade(&my_arc);
/// let my_weak = Arc::downgrade(&my_arc);
/// ```
///
/// `Arc<T>`'s implementations of traits like `Clone` may also be called using
Expand Down Expand Up @@ -898,6 +898,8 @@ impl<T: ?Sized> Arc<T> {
///
/// let weak_five = Arc::downgrade(&five);
/// ```
#[must_use = "this returns a new `Weak` pointer, \
without modifying the original `Arc`"]
#[stable(feature = "arc_weak", since = "1.4.0")]
pub fn downgrade(this: &Self) -> Weak<T> {
// This Relaxed is OK because we're checking the value in the CAS
Expand Down Expand Up @@ -1863,6 +1865,8 @@ impl<T: ?Sized> Weak<T> {
///
/// assert!(weak_five.upgrade().is_none());
/// ```
#[must_use = "this returns a new `Arc`, \
without modifying the original weak pointer"]
#[stable(feature = "arc_weak", since = "1.4.0")]
pub fn upgrade(&self) -> Option<Arc<T>> {
// We use a CAS loop to increment the strong count instead of a
Expand Down
6 changes: 6 additions & 0 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ impl Layout {
/// The minimum byte alignment for a memory block of this layout.
#[stable(feature = "alloc_layout", since = "1.28.0")]
#[rustc_const_stable(feature = "const_alloc_layout", since = "1.50.0")]
#[must_use = "this returns the minimum alignment, \
without modifying the layout"]
#[inline]
pub const fn align(&self) -> usize {
self.align_.get()
Expand Down Expand Up @@ -229,6 +231,8 @@ impl Layout {
/// satisfy this constraint is to ensure `align <= self.align()`.
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
#[must_use = "this returns the padding needed, \
without modifying the `Layout`"]
#[inline]
pub const fn padding_needed_for(&self, align: usize) -> usize {
let len = self.size();
Expand Down Expand Up @@ -262,6 +266,8 @@ impl Layout {
/// This is equivalent to adding the result of `padding_needed_for`
/// to the layout's current size.
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
#[must_use = "this returns a new `Layout`, \
without modifying the original"]
#[inline]
pub fn pad_to_align(&self) -> Layout {
let pad = self.padding_needed_for(self.align());
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,8 @@ impl Path {
/// println!("{}", path.display());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "this does not display the path, \
it returns an object that can be displayed"]
#[inline]
pub fn display(&self) -> Display<'_> {
Display { path: self }
Expand Down

0 comments on commit efad99d

Please sign in to comment.