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 upAllow `#[must_use]` on functions, rather than just types. Mark `Result::{ok,err}` `#[must_use]`. #886
Conversation
huonw
added some commits
Feb 19, 2015
Gankro
reviewed
Feb 19, 2015
|
|
||
| This adds a little more complexity to the `#[must_use]` system. | ||
|
|
||
| The rule stated doesn't cover every instance were a `#[must_use]` |
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
Feb 19, 2015
| function is ignored (it does cover all instances of a `#[must_use]` | ||
| type), e.g. `(foo());` and `{ ...; foo() };` will not be picked up | ||
| (that is, passing the result through another piece of syntax). This | ||
| could be tweaked. |
This comment has been minimized.
This comment has been minimized.
Gankro
Feb 19, 2015
Contributor
This paragraph is kinda hard to parse (two asides in the same "sentence")
This comment has been minimized.
This comment has been minimized.
huonw
added some commits
Feb 19, 2015
Gankro
reviewed
Feb 19, 2015
| code-bases: 2 instances in the rust-lang/rust codebase (vs. nearly 400 | ||
| text matches for `let _ =`) and 4 in the servo/servo (vs. 55 `let _ | ||
| =`). Yet another way to write this is `drop(foo())`, although neither | ||
| this nor `let _ =` have the method chaining style. |
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
Feb 19, 2015
|
|
||
| - Provide an additional method on `Result`, e.g. `fn ignore(self) {}`, so | ||
| that users who wish to ignore `Result`s can do so in the method | ||
| chaining style: `foo().ignore();`. |
This comment has been minimized.
This comment has been minimized.
Gankro
Feb 19, 2015
Contributor
This comment has been minimized.
This comment has been minimized.
reem
Feb 19, 2015
Agreed. I like this solution, though am also in favor of the RFC as a generally useful construct.
This comment has been minimized.
This comment has been minimized.
Gankro
Feb 19, 2015
Contributor
Although ignore as a method is a bit wanting if any return type can be marked as must_use. Still need a general convention (unless Result is the only one that "needs" ignore-ability).
This comment has been minimized.
This comment has been minimized.
reem
Mar 6, 2015
We could add an ignore method to all types (with a blanket impl) that's just a method form of drop.
Gankro
reviewed
Feb 19, 2015
|
|
||
| - Adjust the rule to propagate `#[must_used]`ness through parentheses | ||
| and blocks, so that `(foo());`, `{ foo() };` and even `if cond { | ||
| foo() } else { 0 };` are linted. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
eddyb
Feb 19, 2015
Member
I think the easiest thing to do would be to track whether the result of evaluating the current expression is obviously unused - that would propagate down through (foo()), { foo() }, if/else, match, etc.
This comment has been minimized.
This comment has been minimized.
oli-obk
Feb 19, 2015
Contributor
So basically automatic derivation of must_use for all types (if you have a sub-element that has must_use, you are must_use)? any expression doing a no-op on the object will also return a must_use object -> error message is forwarded outside the expression.
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
Feb 19, 2015
| # Unresolved questions | ||
|
|
||
| - Are there many other functions in the standard library/compiler | ||
| would benefit from `#[must_use]`? |
This comment has been minimized.
This comment has been minimized.
Gankro
Feb 19, 2015
Contributor
Its been suggested elsewhere that the comparison operators should be linted thusly. No reasonable code should have x == y; as a standalone expression.
Would it be possible to mark a trait's method as must_use, affecting all implementers?
reem
reviewed
Feb 19, 2015
| call is intentional or just an accidental oversight. | ||
|
|
||
| Rust has got a lot of mileage out connecting the `#[must_use]` lint to | ||
| specific types: types like `Result`, `MutexGuard` (any guard, ina |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
-1. I, and most code-bases, use ok() to ignore Result. |
This comment has been minimized.
This comment has been minimized.
|
As stated in the RFC, there's not a huge amount of evidence for "most". |
This comment has been minimized.
This comment has been minimized.
|
You know what they say: Absence of evidence is not evidence of absence. |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@mahkoh I have looked, I have found evidence of absence. If you can find a wealth of libraries from a variety of authors that use Even then, I would prefer having an explicit |
This comment has been minimized.
This comment has been minimized.
That's great! I'm looking forward to your analysis of most code-bases on crates.io. Btw: Looking at two trees and not finding any squirrels does not extrapolate to "there are no squirrels in the forrest."
Let's not try to move the burden of proof. You're the one proposing a change and making claims about "ok" not being used for this purpose in most code-bases.
Then we agree. Unfortunately there doesn't seem to be a consensus for changing |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
Is that your detailed analysis of most code-bases on crates.io? |
This comment has been minimized.
This comment has been minimized.
|
Clearly not, but the claim "most code-bases use ok() to ignore Result." is looking pretty unlikely. |
This comment has been minimized.
This comment has been minimized.
Please focus on proving your claims for now. In case you have forgotten:
|
This comment has been minimized.
This comment has been minimized.
|
If the first part of this RFC (#[must_use] on functions) is implemented, then #[must_use] should be removed from Result. |
This comment has been minimized.
This comment has been minimized.
|
I think it's irrelevant whether |
This comment has been minimized.
This comment has been minimized.
That would require every function that returns a Result to be annotated. Some might be missed. The Result type exists explicitly for two things: error reporting and ensuring errors don't get lost. |
This comment has been minimized.
This comment has been minimized.
That's obviously not true. It only requires functions where the return value must be used to be annotated. E.g. if a function is changed from fn f() -> Option<T>;to fn f() -> Result<T, ()>;then that does not imply that ignoring the return value suddenly becomes more or less dangerous. |
huonw
added some commits
Feb 19, 2015
This comment has been minimized.
This comment has been minimized.
AFAIK, that is the code of a single author who is clearly very attached to using If "most" code bases use it for this purpose, it shouldn't be too hard for you to find a few on github to point me to. I did my own random sample of 50 projects on Rust CI, and added a write up, with statistical analysis, to the end of the RFC. I'm now convinced that this will have minimal impact on most code. Unless I see some very strong evidence to the contrary, I'm considering the objection that
I'm slow, and your customarily terse comments are not at all self-explanatory. Please expand. |
huonw
referenced this pull request
Mar 7, 2016
Closed
Add warning about side-effect free expression-statement #32108
petrochenkov
referenced this pull request
Dec 8, 2016
Closed
Warn by default when encountering a statement which only consists of an equality comparison #1812
This comment has been minimized.
This comment has been minimized.
fenhl
commented
Dec 14, 2016
|
See #1812 for some motivation. |
This comment has been minimized.
This comment has been minimized.
Philipp91
commented
Dec 15, 2016
|
More motivation: In an FFI, you get lots of |
This comment has been minimized.
This comment has been minimized.
|
This came up again in the context of #1812, which led to a proposal to close #1812 and re-open this. Anyone interested in working on a revived RFC? I would suggest focusing first on support for |
withoutboats
reopened this
Jan 11, 2017
withoutboats
self-assigned this
Jan 11, 2017
This comment has been minimized.
This comment has been minimized.
|
We have found a motivation for this in #1812 - |
withoutboats
removed
the
final-comment-period
label
Jan 11, 2017
This comment has been minimized.
This comment has been minimized.
ssokolow
commented
Jan 11, 2017
|
@withoutboats Wow... if that doesn't have prohibitive impacts, it'd feel like a direct act of one-upmanship to Python's decision to sacrifice (And if it is blocked by the stability promise, I think it's good reason to start explicitly maintaining a list of "we wish we'd thought of this sooner" features that people writing "Rust-inspired languages" (ie. Rust 2.0 without the Perl6/Py3k/etc. drama) should consider.) Also, I think the point made by @Philipp91 deserves more attention. Even without automatic translation of With automatic translation, it would help binding generators to have that "go above and beyond" feel that already draws praise in areas like |
This comment has been minimized.
This comment has been minimized.
|
@ssokolow since a) its a warn and not an error, b) you can fix it with We'd need an implementation and a crater run. I agree with @joshtriplett that we should accept & implement this without specifying which functions to apply it to, and then just do crater runs on applying it things (IMO without RFCs but YMMV). |
This comment has been minimized.
This comment has been minimized.
jmacdonald
commented
Jan 28, 2017
|
I'd love to see this, especially as a means to make a new |
This comment has been minimized.
This comment has been minimized.
Thiez
commented
Jan 29, 2017
|
So you would especially like to have this feature so that a new method could be introduced specifically to avoid it? That doesn't sound very convincing to me What exactly would make |
This comment has been minimized.
This comment has been minimized.
jmacdonald
commented
Jan 29, 2017
•
|
No, the benefit of this feature is discouraging the use of As for the Great point about the |
This comment has been minimized.
This comment has been minimized.
|
I'm intrigued by the idea of This RFC needs some edits to get it into the current idea behind this proposal:
However, @huonw is no longer a contributor, even if someone else makes the edits I don't think we can get them onto this branch without his merging them. I don't know what the procedure here is, we may need to open a new PR after all. If anyone wants to volunteer to make the edits (possibly @crumblingstatue ?) we could probably get this into FCP soon; based on the discussion here and on #1812, this seems pretty well-discussed & without outstanding issues. |
This comment has been minimized.
This comment has been minimized.
Wyverald
commented
Feb 4, 2017
•
Isn't that exactly the source of mistakes, though? (because the semi-colon is easily used to discard return values that we normally wouldn't want to discard) A more radical way to look at this, IMO, would be to enforce that the expression preceding |
This comment has been minimized.
This comment has been minimized.
There are several functions that return an "optional" result (IIRC some versions of |
This comment has been minimized.
This comment has been minimized.
Wyverald
commented
Feb 7, 2017
|
Indeed, the "no implicit discard" rule wouldn't work very well with this kind of APIs, but maybe those APIs are the exception instead of the norm, and we should be annotating them with Just to clarify, I'm not pushing for the adoption of this rule -- it would be such a breaking change, after all. But I still think we might be overestimating how often return values need to be discarded implicitly, and if we were to design everything differently, maybe this rule could've been considered. |
briansmith
referenced this pull request
Feb 7, 2017
Closed
Mark C function return values as `#[must_use]` #451
This comment has been minimized.
This comment has been minimized.
|
I also came here from #1812 - it's great motivation. I just saw a bug in Android source code in the form of I realized Rust does NOT do better in this case |
This comment has been minimized.
This comment has been minimized.
|
I'd like to avoid derailing this RFC with even stricter proposals. I think this RFC makes sense as a concrete step that would have minimal impact or invasiveness for existing code and projects. Please, by all means, go ahead and propose a separate RFC (or perhaps a pre-RFC first) on a more universal "never ignore return values" mechanism; at a minimum, that might make sense as an off-by-default lint, even if not an on-by-default one. |
This comment has been minimized.
This comment has been minimized.
|
As I said before, I'd like to fcp this rfc as soon as it's been amended to reflect the current status. If anyone can write up an amendment it would be great. |
This comment has been minimized.
This comment has been minimized.
|
Historical note:
Many of us have been in favor of this in the past, but (imo) we definitively decided against it when we allowed APIs like |
This comment has been minimized.
This comment has been minimized.
|
Is anyone interested in updating this RFC to reflect the learnings from #1812 ? @crumblingstatue ? @joshtriplett ? |
iopq
referenced this pull request
Mar 2, 2017
Merged
Updated RFC #886 with lessons learned from #1812 #1940
carols10cents
added this to Merge proposed
in Tracker
Apr 26, 2017
This comment has been minimized.
This comment has been minimized.
|
Just to make things simpler I'm closing this and making #1940 the PR for tracking this RFC. |
huonw commentedFeb 19, 2015
Rendered.