-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow #[must_use]
on functions, rather than just types. Mark Result::{ok,err}
#[must_use]
.
#886
Conversation
`Result::{ok,err}` `#[must_use]`.
|
||
This adds a little more complexity to the `#[must_use]` system. | ||
|
||
The rule stated doesn't cover every instance were a `#[must_use]` |
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.
s/were/where
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. |
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.
Is it within scope for this RFC to try to settle on a convention?
-1. I, and most code-bases, use ok() to ignore Result. |
As stated in the RFC, there's not a huge amount of evidence for "most". |
You know what they say: Absence of evidence is not evidence of absence. |
👍 Seeing |
@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 |
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 |
|
Is that your detailed analysis of most code-bases on crates.io? |
Clearly not, but the claim "most code-bases use ok() to ignore Result." is looking pretty unlikely. |
Please focus on proving your claims for now. In case you have forgotten:
|
If the first part of this RFC (#[must_use] on functions) is implemented, then #[must_use] should be removed from Result. |
I think it's irrelevant whether |
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. |
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. |
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. |
See #1812 for some motivation. |
More motivation: In an FFI, you get lots of |
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 |
We have found a motivation for this in #1812 - |
@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 |
@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). |
I'd love to see this, especially as a means to make a new |
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 |
No, the benefit of this feature is discouraging the use of As for the Great point about the |
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. |
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 |
There are several functions that return an "optional" result (IIRC some versions of |
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. |
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 |
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. |
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. |
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 |
Is anyone interested in updating this RFC to reflect the learnings from #1812 ? @crumblingstatue ? @joshtriplett ? |
Just to make things simpler I'm closing this and making #1940 the PR for tracking this RFC. |
Rendered.