-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Let Option derive #[must_use]
#3906
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
base: master
Are you sure you want to change the base?
Conversation
|
Is there some reason #[must_use] should not be a marker trait? |
|
I would say that actually must_use is more a property of a particular function, not really of types, and sticking it on types is a shorthanded way to put it on a lot of functions "for free" cognitively. I'd say it's best if we, as an ecosystem, stick to having must_use on functions/methods rather than types. |
So #[must_use] could be a marker trait on the function's type where it is applied (each function has a unique type) and on each type it is applied. Then you add a blanked impl on functions whenever their output type impls the trait. (it could be two traits, one for types you apply #[must_use] and another for functions, so that returning the function doesn't trigger it) |
Yes, but
Perhaps, but that would be quite a significant revision. Definitely out-of-scope for this RFC. |
|
Agreed! For this RFC a trait is not necessary. If extending this feature to custom types is desired, a trait would be useful |
|
Note that it is already possible to mark a trait as |
| # Guide-level explanation | ||
| [guide-level-explanation]: #guide-level-explanation | ||
|
|
||
| The `Option` and `Box` types will "magically" have the `#[must_use]` attribute if and only if their generic parameter `T` does. |
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.
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.
impl<T: MustUse> MustUse for Option {}
#[must_use] is not currently a trait. If you want to make an RFC for that, please go ahead. It may be a good idea, but it is an idea for another RFC, not this one.
Since #[must_use] is not currently a trait, we lack the language to describe derive behaviour, hence use of "magically" here.
Option<#[must_use] T>
Use of #[must_use] on parameters is novel syntax that I don't particularly like.
|
Since there have been a number of suggestions about how to generalise this RFC, let me be clear: this RFC is extremely specific on purpose:
|
Let
OptionandBoxderive#[must_use]from their generic parameterT.Rendered