-
Notifications
You must be signed in to change notification settings - Fork 13.3k
#[must_use] on traits, e.g. on Future #55506
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
cc @varkor |
(Also see previous discussion on #51560.) |
Allow #[must_use] on traits Addresses #55506, but we'll probably want to add it to some library traits like `Iterator` before the issue is considered fixed. Fixes #51560. `#[must_use]` is already permitted on traits, with no effect, so this seems like a bug fix, but I might be overlooking something. This currently warns for `impl Trait` or `dyn Trait` when the `Trait` is `#[must_use]` (although I don't think the latter is currently possible, so it's simply future-proofed).
This is now possible. It should be easy to make a pull request adding We probably want to also consider what other traits would benefit from being |
|
@F001 Can you elaborate? If |
@Centril Well, I think this warning means this: let c : impl Fn(i32) = ... ;
c(); // `c` must be used. If `c` is discarded, it probably indicates something wrong.
let r = c(); // I think this attribute does not care `r` is used or not |
@F001 Yeah that makes sense. |
#[must_use] on traits in stdlib Based on #55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
Has this issue been taken? |
@FrankSD: this appears to have been completed in #56677. However, at least one of the traits could do with a more informative I'm going to close this issue, though, as I think it's been completed satisfactorily. |
The OP mentions both #[must_use]
trait Foo {}
impl Foo for () {}
fn foo() -> impl Foo {
()
}
fn bar() -> Box<dyn Foo> {
Box::new(())
}
fn main() {
foo();
bar();
} only gives (playground)
|
This was an oversight on my part; I'll open up a PR soon to fix this. |
It does apply to As for |
This is related to (or is maybe just a subissue of) #39524. It seems reasonable to special-case |
@varkor If you're going for a hack, an attribute based hack seems better, e.g. pub struct Box<#[rustc_propagate_must_use] T: ?Sized>(Unique<T>); |
@Centril: I'm not really going for a hack — |
…tril Extend the #[must_use] lint to boxed types Fixes rust-lang#55506 (comment) (cc @Nemo157). This should have been included as part of rust-lang#55663, but was overlooked.
…tril Extend the #[must_use] lint to boxed types Fixes rust-lang#55506 (comment) (cc @Nemo157). This should have been included as part of rust-lang#55663, but was overlooked.
@varkor is this going to propagate |
I'd like to bring this back up a bit. I've been doing some introductory async work, and I've been bitten by the lack of hints to use a boxed future ( |
@shepmaster new issue for that appears to be #67387 |
Putting
#[must_use]
on a trait is currently a no-op (it doesn't seem to do anything, but it is allowed). See example on playground.One important use-case for this would be to have
#[must_use]
onstd::Future
. When returningimpl Future<…>
orBox<dyn Future<…>>
from a function, it should lead to a warning if that future is not used, as that is almost certainly a bug.The text was updated successfully, but these errors were encountered: