Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign updiscussion/tracking issue for `#[must_use]` functions in the standard library #48926
Comments
zackmdavis
referenced this issue
Mar 11, 2018
Closed
Tracking issue for RFC 1940: allow `#[must_use]` on functions #43302
pietroalbini
added
T-libs
C-tracking-issue
labels
Mar 13, 2018
This comment has been minimized.
This comment has been minimized.
|
I'm not sure "result-like" is broad enough. Maybe something like "things that may have side effects, but which ought never be used for their side effects, especially if often expensive". As a canonical example of such a thing, I definitely don't think this needs to block stabilization, because any |
scottmcm
referenced this issue
Mar 31, 2018
Merged
Add #[must_use] to a few standard library methods #49533
kennytm
added a commit
to kennytm/rust
that referenced
this issue
Apr 3, 2018
kennytm
added a commit
to kennytm/rust
that referenced
this issue
Apr 3, 2018
kennytm
added a commit
to kennytm/rust
that referenced
this issue
Apr 4, 2018
This comment has been minimized.
This comment has been minimized.
|
As of #49533, this is now on |
This comment has been minimized.
This comment has been minimized.
|
I think the non-assigning arithmetic functions are good candidates for this (see #50124). |
zackmdavis
referenced this issue
May 6, 2018
Closed
[wip] library: mark more functions that return String as #[must_use] #50462
This comment has been minimized.
This comment has been minimized.
|
Another metric could be: "things that (are highly likely to) allocate". |
This comment has been minimized.
This comment has been minimized.
|
There's a concrete question around |
This comment has been minimized.
This comment has been minimized.
|
A thought: I agree we don't want to clutter code with |
This comment has been minimized.
This comment has been minimized.
I was starting to think down these lines: Any function which:
And then we opt-out of things with internal mutability? We may still end up with a lot of attributes if we follow @Manishearth's suggestion to have explanatory text on every function. (Not sure if I agree with that or not). |
This comment has been minimized.
This comment has been minimized.
The Rust language already has a concept of side-effect freedom which is MUST is a very strong word. When I see |
This comment has been minimized.
This comment has been minimized.
|
Hmm, I'm not sure that warning on unused |
This comment has been minimized.
This comment has been minimized.
|
HashMap::new doesn't allocate. |
This comment has been minimized.
This comment has been minimized.
|
Sorry, you're right. I guess |
scottmcm
referenced this issue
May 16, 2018
Closed
mark std::string::String::new() and std::vec::Vec::new() as #[must_use]. #50766
This comment has been minimized.
This comment has been minimized.
|
I've made an implementation of my lint idea here: #50805 @jonhoo allocating functions can be Edit: fixed link |
This comment has been minimized.
This comment has been minimized.
|
I've opened an RFC now as it seems that it requires one: rust-lang/rfcs#2450 |
This comment has been minimized.
This comment has been minimized.
|
As discussed in #52201, I think another set of candidates for this is methods like |
This comment has been minimized.
This comment has been minimized.
|
Just to give another example: I just noticed a bug in code that I wrote which could have been caught early with a proper vec.pop();I didn't think about what would happen if the I think marking |
This comment has been minimized.
This comment has been minimized.
|
I recently watched a newcomer to Rust write this shape of code: let mut s = String::new();
stdin.read_line(&mut s);
s.trim();
if s == "hello" { /* ... */ }They either believed that |
This comment has been minimized.
This comment has been minimized.
I think I would lean more toward In contrast, something like calling |
zackmdavis
referenced this issue
Dec 24, 2018
Merged
Mark str::trim.* functions as #[must_use]. #57106
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
BatmanAoD
commented
Feb 13, 2019
•
|
In an internals post, I suggested an alternate heuristic:
CAD97 pointed out that shared-reference is By "a value is returned", I of course meant something other than My opinion is that we should err on the side of more "must-use warnings", because they're trivial to explicitly silence with |
This comment has been minimized.
This comment has been minimized.
|
zackmdavis commentedMar 11, 2018
RFC 1940 authorized use of the
#[must_use]attribute on functions (with the result that invocations of the function behave as if the return type was#[must_use].This has been implemented for a while under a
fn_must_usefeature gate (tracking issue), and PR #48925 proposes stabilization.#[must_use]has been added to the comparison methods in the standard library (.eq,.lt, &c.), but we never got a consensus as to what other standard library methods, if any, should get it. In principle, any function or with no side effects could be must-use (e.g.,.len()on collections), but adding dozens or hundreds of annotations to the standard library feels kind of dramatic: perhaps must-use should instead be reserved for functions with "unusually result-like" semantics—after all, users who want to be really sure they're not uselessly throwing away a return value can always opt in to the (allow-by-default)unused-resultslint.If we wanted to be very conservative, we could refuse to stabilize until we've made a decision on this: if we were to stabilize first, any new
#[must_use]annotations in the standard library would be insta-stable. But, maybe this isn't a big deal (cargopassescap-lints allowto dependencies, so tinkering with lints shouldn't break people's builds).