Skip to content

Commit 01da8fe

Browse files
committed
Auto merge of #57106 - matthiaskrgr:trim_must_use, r=sfackler
mark str::string::String.trim.* functions as #[must_use]. The functions return a reference to a new object and do not modify in-place as the following code shows: ```` let s = String::from(" hello "); s.trim(); assert_eq!(s, " hello "); ```` The new reference should be bound to a variable as now indicated by #[must_use].
2 parents d3d0bf0 + 74e9057 commit 01da8fe

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libcore/str/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3489,6 +3489,8 @@ impl str {
34893489
///
34903490
/// assert_eq!("Hello\tworld", s.trim());
34913491
/// ```
3492+
#[must_use = "this returns the trimmed string as a slice, \
3493+
without modifying the original"]
34923494
#[stable(feature = "rust1", since = "1.0.0")]
34933495
pub fn trim(&self) -> &str {
34943496
self.trim_matches(|c: char| c.is_whitespace())
@@ -3524,6 +3526,8 @@ impl str {
35243526
/// let s = " עברית ";
35253527
/// assert!(Some('ע') == s.trim_start().chars().next());
35263528
/// ```
3529+
#[must_use = "this returns the trimmed string as a new slice, \
3530+
without modifying the original"]
35273531
#[stable(feature = "trim_direction", since = "1.30.0")]
35283532
pub fn trim_start(&self) -> &str {
35293533
self.trim_start_matches(|c: char| c.is_whitespace())
@@ -3559,6 +3563,8 @@ impl str {
35593563
/// let s = " עברית ";
35603564
/// assert!(Some('ת') == s.trim_end().chars().rev().next());
35613565
/// ```
3566+
#[must_use = "this returns the trimmed string as a new slice, \
3567+
without modifying the original"]
35623568
#[stable(feature = "trim_direction", since = "1.30.0")]
35633569
pub fn trim_end(&self) -> &str {
35643570
self.trim_end_matches(|c: char| c.is_whitespace())
@@ -3661,6 +3667,8 @@ impl str {
36613667
/// ```
36623668
/// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
36633669
/// ```
3670+
#[must_use = "this returns the trimmed string as a new slice, \
3671+
without modifying the original"]
36643672
#[stable(feature = "rust1", since = "1.0.0")]
36653673
pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
36663674
where P::Searcher: DoubleEndedSearcher<'a>
@@ -3706,6 +3714,8 @@ impl str {
37063714
/// let x: &[_] = &['1', '2'];
37073715
/// assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12");
37083716
/// ```
3717+
#[must_use = "this returns the trimmed string as a new slice, \
3718+
without modifying the original"]
37093719
#[stable(feature = "trim_direction", since = "1.30.0")]
37103720
pub fn trim_start_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
37113721
let mut i = self.len();
@@ -3749,6 +3759,8 @@ impl str {
37493759
/// ```
37503760
/// assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo");
37513761
/// ```
3762+
#[must_use = "this returns the trimmed string as a new slice, \
3763+
without modifying the original"]
37523764
#[stable(feature = "trim_direction", since = "1.30.0")]
37533765
pub fn trim_end_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
37543766
where P::Searcher: ReverseSearcher<'a>

0 commit comments

Comments
 (0)