-
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
Updated RFC #886 with lessons learned from #1812 #1940
Conversation
`Result::{ok,err}` `#[must_use]`.
Must use
I like the idea in general. But i see two other alternatives:
Have these options been considered? |
Those are both already implemented, |
Ah, so IIUC, the argument of this RFC is that there are types for which In reading the RFC, I didn't understand why existing solutions are insufficient... |
There are quite a lot of APIs that return things you don't always care about, e.g. |
Ah, ok. That's what I thought, but it's good to be sure 😄 Perhaps the RFC can explain this more explicitly. |
One specific example of why making A similar example against making What this RFC allows for are cases where a function is returning a more general type like those, but you still want to force the user to do something with the result. The example from #1812 is that by applying |
this typo has enabled an attacker to create 548,000 Zcoins this should motivate this RFC - right now Rust doesn't warn about this |
Well, actually, the amount of motivation is proportional to the cost of a zcoin 😜 Seriously, though, I does anyone know how common such mistakes actually are in the wild? I already agree with the RFC, but I am curious. |
It is estimated that the attacker made ~$750,000 when he sold his Zcoins. Those are some expensive bugs. |
@iopq Rather than trying to wrangle merging two PR branches together, I think I want to propose we just merge this branch as RFC 886. However, there are a few more amendments I think it needs to get it in line with the current consensus I'm going to list them as concerns and simultaneously propose a merge of this branch. |
@rfcbot fcp merge |
@rfcbot concern "partial_eq" The detailed design doesn't mention attaching this attribute to |
@rfcbot concern "Result::ok" The drawback and motivation sections still contain a lot of the original commentary about |
@rfcbot concern "traits" One thing this RFC doesn't mention is how this plays out with traits and impls - clearly we want to support applying this attribute to trait declarations, but can you apply it to methods in a trait impl block, so that only that particular impl warns? I don't know what implementation concerns that would raise and I'd like to hear from someone about whether or not it would be hard to allow that. |
Team member @withoutboats has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot resolved "Result::ok" |
@rfcbot resolved "traits" |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
The final comment period is now complete. |
Huzzah! This RFC has been merged! Tracking issue. Thanks, everyone who helped push this idea through! |
I know the final comment period is complete but (Sorry for wading into a pile of well-considered discussion and dropping this, but I feel it's very important for languages that value correctness to have the default behavior of warning on unused return value, which would make things like |
If it were changed, I'd recommend something like |
If I read your comment right I think it means I wasn't clear... I'm saying that |
@olson-dan What you're arguing for is implementing full-blown linear typing as a lint and this is the only way that's really a viable option. See The Pain Of Real Linear Types in Rust for details. As for phrasing, my point about intuitiveness still stands. |
That would be a breaking change anyway & is just not on the table for that reason. |
By breaking you mean it would break 1.0 code? If it was not enabled by default it would not. Although it should definitely be default behavior a warning that can be opted-into globally is preferable to local, opt-ins. Or more specifically: I can't rely on developers to locally opt-in to behavior that helps them write code correctly, but I can control compiler options across my organization. This is a major frustration in C++ because "Just add [[nodiscard]] to every function" is not an enforceable solution in that language. Rust is going this route, it seems, and while |
@olson-dan see the existing |
Thanks, that solves it for me. Sorry to derail the issue. |
#[must_use] for functions This implements [RFC 1940](rust-lang/rfcs#1940). The RFC and discussion thereof seem to suggest that tagging `PartialEq::eq` and friends as `#[must_use]` would automatically lint for unused comparisons, but it doesn't work out that way (at least the way I've implemented it): unused `.eq` method calls get linted, but not `==` expressions. (The lint operates on the HIR, which sees binary operations as their own thing, even if they ultimately just call `.eq` _&c._.) What do _you_ think?? Resolves #43302.
See #886 and #1812
Rendered
Took out
ok()
parts