-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| - Feature Name: Let `Option` derive `#[must_use]` | ||
| - Start Date: 2026-01-07 | ||
| - RFC PR: [rust-lang/rfcs#3906](https://github.com/rust-lang/rfcs/pull/3906) | ||
| - Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) | ||
|
|
||
| # Summary | ||
| [summary]: #summary | ||
|
|
||
| Let `Option` and `Box` derive `#[must_use]` from their generic parameter `T`. | ||
|
|
||
| # Motivation | ||
| [motivation]: #motivation | ||
|
|
||
| If we write: | ||
| ```rust | ||
| #[must_use] | ||
| struct Redraw; | ||
|
|
||
| fn do_thing() -> Option<Redraw> { | ||
| // Do some thing which requires a redraw... | ||
| Some(Redraw) | ||
| } | ||
| ``` | ||
| then `do_thing` should be `#[must_use]`, and while we can apply the `#[must_use]` attribute to the function `do_thing`, we shouldn't have to (remember to do so). | ||
|
|
||
| # 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since
Use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, i thought i was clear in the original comment that "the
i think users should have access to more fine-grained i don't want to overlitigate, but i still reckon this RFC doesn't really justify the "magic" design as written: it's clearly conceptually easier to special case these two generic types, but i don't see it as better from a maintenance perspective if we do want to rip it out for something holistic. for instance
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Attribute on type parameter is already in use since #3621 (ref), it is no longer a novel syntax. #[derive(CoercePointee)]
#[repr(transparent)]
struct MySmartPointer<#[pointee] T: ?Sized, U> {
ptr: Box<T>,
_phantom: PhantomData<U>,
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Something like this would be clearer (though I do find #[derive(must_use)]
enum Option<T> { /* ... */ }That may be besides the point though if this RFC adds more complexity than value. (I had no idea that |
||
|
|
||
| # Reference-level explanation | ||
| [reference-level-explanation]: #reference-level-explanation | ||
|
|
||
| This will be an internal detail of the standard library. It may use another special attribute like `#[derive_must_use_from(T)]`, but for the purposes of this RFC, the `derive_must_use_from` attribute may remain unstable forever. | ||
|
|
||
| # Drawbacks | ||
| [drawbacks]: #drawbacks | ||
|
|
||
| I see no drawbacks besides the small amount of complexity involved. | ||
|
|
||
| # Rationale and alternatives | ||
| [rationale-and-alternatives]: #rationale-and-alternatives | ||
|
|
||
| The only obvious (non-empty) alternative is to add (and stabilise) a new `#[derive_must_use_from(T)]` attribute and apply this to `Option<T>` (and [other types](#future-possibilities)). | ||
|
|
||
| This would not be a strict alternative in that nothing prevents this from being done later. | ||
|
|
||
| # Prior art | ||
| [prior-art]: #prior-art | ||
|
|
||
| [RFC #3737](https://github.com/rust-lang/rfcs/pull/3737) is vaguely related (only in that it also pertains to `#[must_use]`). | ||
|
|
||
| `#[must_use`] is already tracked through tuples ([example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=488fc81eba51a4aded6faeab7ee9bf44)), though strictly speaking this does not apply `#[must_use]` to the tuple type. | ||
|
|
||
| # Unresolved questions | ||
| [unresolved-questions]: #unresolved-questions | ||
|
|
||
| # Future possibilities | ||
| [future-possibilities]: #future-possibilities | ||
|
|
||
| Possibly a few other standard library types would benefit from this derivation of `#[must_use]`: | ||
|
|
||
| - `Box<T>` can do so (included in this RFC, though motivation is weaker) | ||
| - `RefCell<T>` and `Mutex<T>` *could* do so but it is unlikely of any use | ||
| - `Rc<T>`, `Arc<T>` and various reference types *should not* since they do/may not have exclusive ownership of the value | ||
| - `Vec<T>` and other containers *could* do so | ||
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.
2cents: I think "inherit" may be a better term ot use than "derive". For me at least, in reading this RFC title I expected something like
#[derive(MustUse)]to be involved.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.
derivemay cause confusion with derive macro.but
inheritmay also cause confusion with that in OOP sense.I think
propagateis a better word.