Skip to content

Commit 54d807c

Browse files
committed
Add #[must_use] to string/char transformation methods
These methods could be misconstrued as modifying their arguments instead of returning new values. Where possible I made the note recommend a method that does mutate in place.
1 parent f875143 commit 54d807c

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

library/alloc/src/slice.rs

+4
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ impl [u8] {
662662
///
663663
/// [`make_ascii_uppercase`]: slice::make_ascii_uppercase
664664
#[cfg(not(no_global_oom_handling))]
665+
#[must_use = "this returns the uppercase bytes as a new Vec, \
666+
without modifying the original"]
665667
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
666668
#[inline]
667669
pub fn to_ascii_uppercase(&self) -> Vec<u8> {
@@ -680,6 +682,8 @@ impl [u8] {
680682
///
681683
/// [`make_ascii_lowercase`]: slice::make_ascii_lowercase
682684
#[cfg(not(no_global_oom_handling))]
685+
#[must_use = "this returns the lowercase bytes as a new Vec, \
686+
without modifying the original"]
683687
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
684688
#[inline]
685689
pub fn to_ascii_lowercase(&self) -> Vec<u8> {

library/alloc/src/str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ impl str {
367367
/// assert_eq!(new_year, new_year.to_lowercase());
368368
/// ```
369369
#[cfg(not(no_global_oom_handling))]
370+
#[must_use = "this returns the lowercase string as a new String, \
371+
without modifying the original"]
370372
#[stable(feature = "unicode_case_mapping", since = "1.2.0")]
371373
pub fn to_lowercase(&self) -> String {
372374
let mut s = String::with_capacity(self.len());
@@ -447,6 +449,8 @@ impl str {
447449
/// assert_eq!("TSCHÜSS", s.to_uppercase());
448450
/// ```
449451
#[cfg(not(no_global_oom_handling))]
452+
#[must_use = "this returns the uppercase string as a new String, \
453+
without modifying the original"]
450454
#[stable(feature = "unicode_case_mapping", since = "1.2.0")]
451455
pub fn to_uppercase(&self) -> String {
452456
let mut s = String::with_capacity(self.len());
@@ -534,6 +538,7 @@ impl str {
534538
/// [`make_ascii_uppercase`]: str::make_ascii_uppercase
535539
/// [`to_uppercase`]: #method.to_uppercase
536540
#[cfg(not(no_global_oom_handling))]
541+
#[must_use = "to uppercase the value in-place, use `make_ascii_lowercase()`"]
537542
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
538543
#[inline]
539544
pub fn to_ascii_uppercase(&self) -> String {
@@ -565,6 +570,7 @@ impl str {
565570
/// [`make_ascii_lowercase`]: str::make_ascii_lowercase
566571
/// [`to_lowercase`]: #method.to_lowercase
567572
#[cfg(not(no_global_oom_handling))]
573+
#[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
568574
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
569575
#[inline]
570576
pub fn to_ascii_lowercase(&self) -> String {

library/core/src/char/methods.rs

+6
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,8 @@ impl char {
949949
/// // convert into themselves.
950950
/// assert_eq!('山'.to_lowercase().to_string(), "山");
951951
/// ```
952+
#[must_use = "this returns the lowercase character as a new iterator, \
953+
without modifying the original"]
952954
#[stable(feature = "rust1", since = "1.0.0")]
953955
#[inline]
954956
pub fn to_lowercase(self) -> ToLowercase {
@@ -1039,6 +1041,8 @@ impl char {
10391041
/// ```
10401042
///
10411043
/// holds across languages.
1044+
#[must_use = "this returns the uppercase character as a new iterator, \
1045+
without modifying the original"]
10421046
#[stable(feature = "rust1", since = "1.0.0")]
10431047
#[inline]
10441048
pub fn to_uppercase(self) -> ToUppercase {
@@ -1085,6 +1089,7 @@ impl char {
10851089
///
10861090
/// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase
10871091
/// [`to_uppercase()`]: #method.to_uppercase
1092+
#[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
10881093
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
10891094
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
10901095
#[inline]
@@ -1118,6 +1123,7 @@ impl char {
11181123
///
11191124
/// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase
11201125
/// [`to_lowercase()`]: #method.to_lowercase
1126+
#[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
11211127
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
11221128
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
11231129
#[inline]

library/core/src/num/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ impl u8 {
282282
/// ```
283283
///
284284
/// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
285+
#[must_use = "to uppercase the value in-place, use `make_ascii_lowercase()`"]
285286
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
286287
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
287288
#[inline]
@@ -306,6 +307,7 @@ impl u8 {
306307
/// ```
307308
///
308309
/// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
310+
#[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
309311
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
310312
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
311313
#[inline]
@@ -769,6 +771,8 @@ impl u8 {
769771
/// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
770772
/// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
771773
/// ```
774+
#[must_use = "this returns the escaped byte as an iterator, \
775+
without modifying the original"]
772776
#[unstable(feature = "inherent_ascii_escape", issue = "77174")]
773777
#[inline]
774778
pub fn escape_ascii(&self) -> ascii::EscapeDefault {

library/core/src/slice/ascii.rs

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ impl [u8] {
7272
/// let escaped = s.escape_ascii().to_string();
7373
/// assert_eq!(escaped, "0\\t\\r\\n\\'\\\"\\\\\\x9d");
7474
/// ```
75+
#[must_use = "this returns the escaped bytes as an iterator, \
76+
without modifying the original"]
7577
#[unstable(feature = "inherent_ascii_escape", issue = "77174")]
7678
pub fn escape_ascii(&self) -> EscapeAscii<'_> {
7779
EscapeAscii { inner: self.iter().flat_map(EscapeByte) }

library/core/src/str/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ impl str {
798798
///
799799
/// assert_eq!(None, iter.next());
800800
/// ```
801+
#[must_use = "this returns the split string as an iterator, \
802+
without modifying the original"]
801803
#[stable(feature = "split_whitespace", since = "1.1.0")]
802804
#[inline]
803805
pub fn split_whitespace(&self) -> SplitWhitespace<'_> {
@@ -839,6 +841,8 @@ impl str {
839841
///
840842
/// assert_eq!(None, iter.next());
841843
/// ```
844+
#[must_use = "this returns the split string as an iterator, \
845+
without modifying the original"]
842846
#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
843847
#[inline]
844848
pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> {
@@ -914,6 +918,8 @@ impl str {
914918
///
915919
/// assert!(utf16_len <= utf8_len);
916920
/// ```
921+
#[must_use = "this returns the encoded string as an iterator, \
922+
without modifying the original"]
917923
#[stable(feature = "encode_utf16", since = "1.8.0")]
918924
pub fn encode_utf16(&self) -> EncodeUtf16<'_> {
919925
EncodeUtf16 { chars: self.chars(), extra: 0 }
@@ -1840,6 +1846,8 @@ impl str {
18401846
/// let s = " עברית";
18411847
/// assert!(Some('ע') == s.trim_left().chars().next());
18421848
/// ```
1849+
#[must_use = "this returns the trimmed string as a new slice, \
1850+
without modifying the original"]
18431851
#[inline]
18441852
#[stable(feature = "rust1", since = "1.0.0")]
18451853
#[rustc_deprecated(
@@ -1882,6 +1890,8 @@ impl str {
18821890
/// let s = "עברית ";
18831891
/// assert!(Some('ת') == s.trim_right().chars().rev().next());
18841892
/// ```
1893+
#[must_use = "this returns the trimmed string as a new slice, \
1894+
without modifying the original"]
18851895
#[inline]
18861896
#[stable(feature = "rust1", since = "1.0.0")]
18871897
#[rustc_deprecated(
@@ -2346,6 +2356,8 @@ impl str {
23462356
/// ```
23472357
/// assert_eq!("❤\n!".escape_debug().to_string(), "❤\\n!");
23482358
/// ```
2359+
#[must_use = "this returns the escaped string as an iterator, \
2360+
without modifying the original"]
23492361
#[stable(feature = "str_escape", since = "1.34.0")]
23502362
pub fn escape_debug(&self) -> EscapeDebug<'_> {
23512363
let mut chars = self.chars();
@@ -2390,6 +2402,8 @@ impl str {
23902402
/// ```
23912403
/// assert_eq!("❤\n!".escape_default().to_string(), "\\u{2764}\\n!");
23922404
/// ```
2405+
#[must_use = "this returns the escaped string as an iterator, \
2406+
without modifying the original"]
23932407
#[stable(feature = "str_escape", since = "1.34.0")]
23942408
pub fn escape_default(&self) -> EscapeDefault<'_> {
23952409
EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
@@ -2426,6 +2440,8 @@ impl str {
24262440
/// ```
24272441
/// assert_eq!("❤\n!".escape_unicode().to_string(), "\\u{2764}\\u{a}\\u{21}");
24282442
/// ```
2443+
#[must_use = "this returns the escaped string as an iterator, \
2444+
without modifying the original"]
24292445
#[stable(feature = "str_escape", since = "1.34.0")]
24302446
pub fn escape_unicode(&self) -> EscapeUnicode<'_> {
24312447
EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }

library/std/src/ffi/os_str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ impl OsStr {
777777
///
778778
/// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
779779
/// ```
780+
#[must_use = "to lowercase the value in-place, use `make_ascii_lowercase`"]
780781
#[stable(feature = "osstring_ascii", since = "1.53.0")]
781782
pub fn to_ascii_lowercase(&self) -> OsString {
782783
OsString::from_inner(self.inner.to_ascii_lowercase())
@@ -798,6 +799,7 @@ impl OsStr {
798799
///
799800
/// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
800801
/// ```
802+
#[must_use = "to uppercase the value in-place, use `make_ascii_uppercase`"]
801803
#[stable(feature = "osstring_ascii", since = "1.53.0")]
802804
pub fn to_ascii_uppercase(&self) -> OsString {
803805
OsString::from_inner(self.inner.to_ascii_uppercase())

0 commit comments

Comments
 (0)