Skip to content

Commit 4924fea

Browse files
committed
Rollup merge of #50511 - Manishearth:must-use, r=QuietMisdreavus
Add some explanations for #[must_use] `#[must_use]` can be given a string argument which is shown whilst warning for things. We should add a string argument to most of the user-exposed ones. I added these for everything but the operators, mostly because I'm not sure what to write there or if we need anything there.
2 parents 4c3ab33 + a72a080 commit 4924fea

File tree

6 files changed

+14
-12
lines changed

6 files changed

+14
-12
lines changed

src/liballoc/str.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ impl str {
207207
/// let s = "this is old";
208208
/// assert_eq!(s, s.replace("cookie monster", "little lamb"));
209209
/// ```
210-
#[must_use]
210+
#[must_use = "this returns the replaced string as a new allocation, \
211+
without modifying the original"]
211212
#[stable(feature = "rust1", since = "1.0.0")]
212213
#[inline]
213214
pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
@@ -247,7 +248,8 @@ impl str {
247248
/// let s = "this is old";
248249
/// assert_eq!(s, s.replacen("cookie monster", "little lamb", 10));
249250
/// ```
250-
#[must_use]
251+
#[must_use = "this returns the replaced string as a new allocation, \
252+
without modifying the original"]
251253
#[stable(feature = "str_replacen", since = "1.16.0")]
252254
pub fn replacen<'a, P: Pattern<'a>>(&'a self, pat: P, to: &str, count: usize) -> String {
253255
// Hope to reduce the times of re-allocation

src/libcore/fmt/builders.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a> fmt::Write for PadAdapter<'a> {
8484
/// // prints "Foo { bar: 10, baz: "Hello World" }"
8585
/// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
8686
/// ```
87-
#[must_use]
87+
#[must_use = "must eventually call `finish()` on Debug builders"]
8888
#[allow(missing_debug_implementations)]
8989
#[stable(feature = "debug_builders", since = "1.2.0")]
9090
pub struct DebugStruct<'a, 'b: 'a> {
@@ -181,7 +181,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
181181
/// // prints "Foo(10, "Hello World")"
182182
/// println!("{:?}", Foo(10, "Hello World".to_string()));
183183
/// ```
184-
#[must_use]
184+
#[must_use = "must eventually call `finish()` on Debug builders"]
185185
#[allow(missing_debug_implementations)]
186186
#[stable(feature = "debug_builders", since = "1.2.0")]
187187
pub struct DebugTuple<'a, 'b: 'a> {
@@ -319,7 +319,7 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> {
319319
/// // prints "{10, 11}"
320320
/// println!("{:?}", Foo(vec![10, 11]));
321321
/// ```
322-
#[must_use]
322+
#[must_use = "must eventually call `finish()` on Debug builders"]
323323
#[allow(missing_debug_implementations)]
324324
#[stable(feature = "debug_builders", since = "1.2.0")]
325325
pub struct DebugSet<'a, 'b: 'a> {
@@ -390,7 +390,7 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
390390
/// // prints "[10, 11]"
391391
/// println!("{:?}", Foo(vec![10, 11]));
392392
/// ```
393-
#[must_use]
393+
#[must_use = "must eventually call `finish()` on Debug builders"]
394394
#[allow(missing_debug_implementations)]
395395
#[stable(feature = "debug_builders", since = "1.2.0")]
396396
pub struct DebugList<'a, 'b: 'a> {
@@ -461,7 +461,7 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> {
461461
/// // prints "{"A": 10, "B": 11}"
462462
/// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
463463
/// ```
464-
#[must_use]
464+
#[must_use = "must eventually call `finish()` on Debug builders"]
465465
#[allow(missing_debug_implementations)]
466466
#[stable(feature = "debug_builders", since = "1.2.0")]
467467
pub struct DebugMap<'a, 'b: 'a> {

src/libcore/result.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ use ops;
251251
/// [`Ok`]: enum.Result.html#variant.Ok
252252
/// [`Err`]: enum.Result.html#variant.Err
253253
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
254-
#[must_use]
254+
#[must_use = "this `Result` may be an `Err` variant, which should be handled"]
255255
#[stable(feature = "rust1", since = "1.0.0")]
256256
pub enum Result<T, E> {
257257
/// Contains the success value

src/libstd/sync/mutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ unsafe impl<T: ?Sized + Send> Sync for Mutex<T> { }
150150
/// [`lock`]: struct.Mutex.html#method.lock
151151
/// [`try_lock`]: struct.Mutex.html#method.try_lock
152152
/// [`Mutex`]: struct.Mutex.html
153-
#[must_use]
153+
#[must_use = "if unused the Mutex will immediately unlock"]
154154
#[stable(feature = "rust1", since = "1.0.0")]
155155
pub struct MutexGuard<'a, T: ?Sized + 'a> {
156156
// funny underscores due to how Deref/DerefMut currently work (they

src/libstd/sync/rwlock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
9494
/// [`read`]: struct.RwLock.html#method.read
9595
/// [`try_read`]: struct.RwLock.html#method.try_read
9696
/// [`RwLock`]: struct.RwLock.html
97-
#[must_use]
97+
#[must_use = "if unused the RwLock will immediately unlock"]
9898
#[stable(feature = "rust1", since = "1.0.0")]
9999
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
100100
__lock: &'a RwLock<T>,
@@ -115,7 +115,7 @@ unsafe impl<'a, T: ?Sized + Sync> Sync for RwLockReadGuard<'a, T> {}
115115
/// [`write`]: struct.RwLock.html#method.write
116116
/// [`try_write`]: struct.RwLock.html#method.try_write
117117
/// [`RwLock`]: struct.RwLock.html
118-
#[must_use]
118+
#[must_use = "if unused the RwLock will immediately unlock"]
119119
#[stable(feature = "rust1", since = "1.0.0")]
120120
pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
121121
__lock: &'a RwLock<T>,

src/libstd/sys_common/remutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unsafe impl<T: Send> Sync for ReentrantMutex<T> {}
4141
/// because implementation of the trait would violate Rust’s reference aliasing
4242
/// rules. Use interior mutability (usually `RefCell`) in order to mutate the
4343
/// guarded data.
44-
#[must_use]
44+
#[must_use = "if unused the ReentrantMutex will immediately unlock"]
4545
pub struct ReentrantMutexGuard<'a, T: 'a> {
4646
// funny underscores due to how Deref currently works (it disregards field
4747
// privacy).

0 commit comments

Comments
 (0)