You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
#[must_use] on associated function definition when trait declaration does not have it should still trigger warning when calling function concretely #48486
This is expected (or at least, known) behavior; the #[must_use] attribute needs to go on the function signature in the trait definition, not the implementation. I argue that the current behavior makes sense: the semantics of a trait method should be the same across implementations. We should document this better, though—thanks for starting on that!
Then there should be a warning when #[must_use] is on a trait method.
But while it is known behavior, I didn't expect it. I expect that concretely calling a trait method should work the same as concretely calling an inherent method. Of course, when called generically, it should only look at the trait declaration.
Reacted by Esteban Kuber, varkor and Ian Chamberlain
warning: `#[must_use]` attribute cannot be used on trait methods in impl blocks
--> src/main.rs:13:5
|
13 | #[must_use]
| ^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[must_use]` can be applied to data types, foreign functions, functions, inherent methods, provided trait methods, required trait methods, traits, and unions
= note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default
warning: unused return value of `Trait::the_must_use_fn` that must be used
--> src/main.rs:3:5
|
3 | s.the_must_use_fn();
| ^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default
help: use `let _ = ...` to ignore the resulting value
|
3 | let _ = s.the_must_use_fn();
| +++++++
Activity
zackmdavis commentedon Feb 23, 2018
This is expected (or at least, known) behavior; the
#[must_use]attribute needs to go on the function signature in the trait definition, not the implementation. I argue that the current behavior makes sense: the semantics of a trait method should be the same across implementations. We should document this better, though—thanks for starting on that!Havvy commentedon Feb 23, 2018
Then there should be a warning when
#[must_use]is on a trait method.But while it is known behavior, I didn't expect it. I expect that concretely calling a trait method should work the same as concretely calling an inherent method. Of course, when called generically, it should only look at the trait declaration.
estebank commentedon Nov 3, 2025
Current output:
When setting it in the trait: