#[must_use]
on traits should apply to all types that implement the trait, not just for impl Trait
placeholders
#102238
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-lints
Area: Lints (warnings about flaws in source code) such as unused_mut.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
General issue
Currently,
#[must_use]
on a traitFoo
does not extend to types that implementFoo
, it only works onimpl Foo
return types, andBox<dyn Foo>
as a special case:(playground)
This does not make much sense, presumably if a trait must always be used all types that implement that trait must also be used.
Application to
Future
I came across this when dealing with futures. The Future trait has a
#[must_use]
annotation, with the useful reminder to.await
or poll them:rust/library/core/src/future/future.rs
Lines 28 to 37 in b8967b0
I was specifically using specifically
BoxFuture
, a type alias forPin<Box<dyn Future>>
. This type implementsFuture
, but there is no warning message when you forget to use it. This was also the original justification in #67387, where a very different solution is proposed (extendingmust_use
toPin
and other wrapper types).Another effect of this missing warning is that any struct that implements
Future
has to repeat the samemust_use
annotation. Some examples:futures::future::Ready
tokio::time::Sleep
async_std
doesn't expose any public types that implementFuture
, instead they almost always use animpl Future
return type. There is still the internalWriteFmtFuture
.If
must_use
on traits infected any type that implemented it, these duplicate annotations could be removed and there would be no risk of forgetting them for any future types.The text was updated successfully, but these errors were encountered: