-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[wip] library: mark more functions that return String as #[must_use] #50462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,6 +422,7 @@ impl String { | |
/// // ...but this may make the vector reallocate | ||
/// s.push('a'); | ||
/// ``` | ||
#[must_use] | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn with_capacity(capacity: usize) -> String { | ||
|
@@ -432,6 +433,7 @@ impl String { | |
// required for this method definition, is not available. Since we don't | ||
// require this method for testing purposes, I'll just stub it | ||
// NB see the slice::hack module in slice.rs for more information | ||
#[must_use] | ||
#[inline] | ||
#[cfg(test)] | ||
pub fn from_str(_: &str) -> String { | ||
|
@@ -643,6 +645,7 @@ impl String { | |
/// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"), | ||
/// String::from_utf16_lossy(v)); | ||
/// ``` | ||
#[must_use] | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn from_utf16_lossy(v: &[u16]) -> String { | ||
|
@@ -690,6 +693,7 @@ impl String { | |
/// assert_eq!(String::from("hello"), s); | ||
/// } | ||
/// ``` | ||
#[must_use] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd disagree with this one — this is used for FFI when passing back a decomposed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't the code be more readable if one is forced to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in my opinion, no. Idiomatic Rust doesn't tend to use explicit calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am just a stranger here, but I have some experience with Python, which mantra is "explicit is better than implicit". Idiomatic Rust doesn't tend to use unsafe blocks frequently, either. I would even say that explicitness is even more valuable in unsafe code. |
||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String { | ||
|
@@ -724,6 +728,7 @@ impl String { | |
/// | ||
/// assert_eq!("💖", sparkle_heart); | ||
/// ``` | ||
#[must_use] | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String { | ||
|
@@ -1420,6 +1425,7 @@ impl String { | |
/// assert_eq!(world, "World!"); | ||
/// # } | ||
/// ``` | ||
#[must_use] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function does have side effects ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I ignore the result of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to customize the error message provided by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attribute can take a string literal with an additional message: usage, output. (The output format may change soon.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one I like provided it has the message. It sets a nice "if it allows redirecting to a cheaper method" precedent (which, in retrospect, (Should probably be on |
||
#[inline] | ||
#[stable(feature = "string_split_off", since = "1.16.0")] | ||
pub fn split_off(&mut self, at: usize) -> String { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one feels insufficiently justified. It's not allocating. It consumes the input so if you didn't need it you'll probably know. It does almost nothing, so will probably disappear on its own if unused.
I agree that it could be
must_use
, but as I understand it the goal isn't to put it everywhere possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we should apply so hardly-defined rules to where
must_use
should be used and where it shouldn't. I suggest the following reasoning: if the function call doesn't make sense without consuming the result, it should be marked withmust_use
.Why does Rust warn about unused
use
s then? They do nothing and just "disappear" completely if not used.