Skip to content

Commit aa127d8

Browse files
committed
library: mark more functions that return String as #[must_use]
If the return value (String) of the function is not used, the function call is technically in vain (unless there are side effects) and the entire function call could be ommitted. Warn about unused return values of -> String functions. Example code: let mut x = Sting::from("hello"); x.push_str(" world!"); x.to_uppercase(); println!("{}", x); will print "hello world!" instead of "HELLO WORLD!") because the result of .to_uppercase() is not caught in a variable. std::str::to_lowercase() std::str::to_uppercase() std::str::escape_debug() std::str::escape_default() std::str::escape_unicode() std::str::into_string() std::str::repeat() std::str::to_ascii_uppercase() std::str::to_ascii_lowercase() std::String::with_capacity() std::String::from_str() std::String::from_utf16_lossy() std::String::from_raw_parts() std::String::from_utf8_unchecked() std::String::split_off()
1 parent 95d0b9e commit aa127d8

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/liballoc/str.rs

+9
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ impl str {
305305
///
306306
/// assert_eq!(new_year, new_year.to_lowercase());
307307
/// ```
308+
#[must_use]
308309
#[stable(feature = "unicode_case_mapping", since = "1.2.0")]
309310
pub fn to_lowercase(&self) -> String {
310311
let mut s = String::with_capacity(self.len());
@@ -368,6 +369,7 @@ impl str {
368369
///
369370
/// assert_eq!(new_year, new_year.to_uppercase());
370371
/// ```
372+
#[must_use]
371373
#[stable(feature = "unicode_case_mapping", since = "1.2.0")]
372374
pub fn to_uppercase(&self) -> String {
373375
let mut s = String::with_capacity(self.len());
@@ -378,6 +380,7 @@ impl str {
378380
/// Escapes each char in `s` with [`char::escape_debug`].
379381
///
380382
/// [`char::escape_debug`]: primitive.char.html#method.escape_debug
383+
#[must_use]
381384
#[unstable(feature = "str_escape",
382385
reason = "return type may change to be an iterator",
383386
issue = "27791")]
@@ -388,6 +391,7 @@ impl str {
388391
/// Escapes each char in `s` with [`char::escape_default`].
389392
///
390393
/// [`char::escape_default`]: primitive.char.html#method.escape_default
394+
#[must_use]
391395
#[unstable(feature = "str_escape",
392396
reason = "return type may change to be an iterator",
393397
issue = "27791")]
@@ -398,6 +402,7 @@ impl str {
398402
/// Escapes each char in `s` with [`char::escape_unicode`].
399403
///
400404
/// [`char::escape_unicode`]: primitive.char.html#method.escape_unicode
405+
#[must_use]
401406
#[unstable(feature = "str_escape",
402407
reason = "return type may change to be an iterator",
403408
issue = "27791")]
@@ -420,6 +425,7 @@ impl str {
420425
///
421426
/// assert_eq!(boxed_str.into_string(), string);
422427
/// ```
428+
#[must_use]
423429
#[stable(feature = "box_str", since = "1.4.0")]
424430
#[inline]
425431
pub fn into_string(self: Box<str>) -> String {
@@ -438,6 +444,7 @@ impl str {
438444
/// ```
439445
/// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
440446
/// ```
447+
#[must_use]
441448
#[stable(feature = "repeat_str", since = "1.16.0")]
442449
pub fn repeat(&self, n: usize) -> String {
443450
unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) }
@@ -464,6 +471,7 @@ impl str {
464471
///
465472
/// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
466473
/// [`to_uppercase`]: #method.to_uppercase
474+
#[must_use]
467475
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
468476
#[inline]
469477
pub fn to_ascii_uppercase(&self) -> String {
@@ -494,6 +502,7 @@ impl str {
494502
///
495503
/// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
496504
/// [`to_lowercase`]: #method.to_lowercase
505+
#[must_use]
497506
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
498507
#[inline]
499508
pub fn to_ascii_lowercase(&self) -> String {

src/liballoc/string.rs

+6
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ impl String {
422422
/// // ...but this may make the vector reallocate
423423
/// s.push('a');
424424
/// ```
425+
#[must_use]
425426
#[inline]
426427
#[stable(feature = "rust1", since = "1.0.0")]
427428
pub fn with_capacity(capacity: usize) -> String {
@@ -432,6 +433,7 @@ impl String {
432433
// required for this method definition, is not available. Since we don't
433434
// require this method for testing purposes, I'll just stub it
434435
// NB see the slice::hack module in slice.rs for more information
436+
#[must_use]
435437
#[inline]
436438
#[cfg(test)]
437439
pub fn from_str(_: &str) -> String {
@@ -643,6 +645,7 @@ impl String {
643645
/// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),
644646
/// String::from_utf16_lossy(v));
645647
/// ```
648+
#[must_use]
646649
#[inline]
647650
#[stable(feature = "rust1", since = "1.0.0")]
648651
pub fn from_utf16_lossy(v: &[u16]) -> String {
@@ -690,6 +693,7 @@ impl String {
690693
/// assert_eq!(String::from("hello"), s);
691694
/// }
692695
/// ```
696+
#[must_use]
693697
#[inline]
694698
#[stable(feature = "rust1", since = "1.0.0")]
695699
pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String {
@@ -724,6 +728,7 @@ impl String {
724728
///
725729
/// assert_eq!("💖", sparkle_heart);
726730
/// ```
731+
#[must_use]
727732
#[inline]
728733
#[stable(feature = "rust1", since = "1.0.0")]
729734
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
@@ -1420,6 +1425,7 @@ impl String {
14201425
/// assert_eq!(world, "World!");
14211426
/// # }
14221427
/// ```
1428+
#[must_use]
14231429
#[inline]
14241430
#[stable(feature = "string_split_off", since = "1.16.0")]
14251431
pub fn split_off(&mut self, at: usize) -> String {

src/liballoc/tests/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ fn test_split_off_empty() {
244244
fn test_split_off_past_end() {
245245
let orig = "Hello, world!";
246246
let mut split = String::from(orig);
247-
split.split_off(orig.len() + 1);
247+
let _ = split.split_off(orig.len() + 1);
248248
}
249249

250250
#[test]
251251
#[should_panic]
252252
fn test_split_off_mid_char() {
253253
let mut orig = String::from("山");
254-
orig.split_off(1);
254+
let _ = orig.split_off(1);
255255
}
256256

257257
#[test]

0 commit comments

Comments
 (0)