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] is permitted on functions without return type #54828
#[must_use = "function with no return type"]fnfoo(){}fnmain(){foo();}
Is this intended? Shouldn't it produce a lint warning on the attribute? This is a good mentoring issue, I can bang out instructions once I get an answer (fortunately this doesn't seem to involve hygiene this time so hopefully I should get it right... @petrochenkov)
The fact that unused-must-use doesn't fire on the foo(); is a consequence of the lint pass returning early for () and !.
I agree that it's useless to put #[must_use] on a function that returns (), but I don't think anyone thought ahead and decided it should (or shouldn't) be an error. (I wouldn't want to write a dedicated lint for such an edge-case, and I think these days we tend to frown on non-lint warnings on account of their unsilenceability.)
#48486 is a similar issue with trait implementations.
By "unit type" do you mean struct Foo;? I'd say no, must_use on user-defined types is always potentially sensible, e.g. ZSTs can be used as tokens for something that the caller should not usually forget to do.
Probably, because those types typically don't have any meaning or semantics that can be added to them by the user. I guess it's possible with extension traits but that's going to be a very weird corner case.
I was skeptical about linting even () but forgetting to fill in the return type is at least a plausible scenario where such a lint could help. Going any further does not seem likely to help anyone in any scenario. Types like ((), ()) are rarely even constructed much less written in function signatures.
@zackmdavis yes but @Centril is talking about the exact opposite behavior, that the unused_must_use lint should trigger regardless of the function's return type.
As @zackmdavis I'm suggesting that if the #[must_use] annotation has no effect, then a warning should be emitted. But a way to achieve that is for #[must_use] fn foo() {} have an effect. The other way is for #[must_use] fn foo() {} to trigger unused_attributes.
We could pull this code out into a common function [...] so that the behavior of #[must_use] and unused-attribute-policing-of-#[must_use] never got out of sync.
Unfortunately, this turns out to not be easy because the function signature gives us hir::Ty, whereas the UnusedResults pass is looking for a ty::Ty. 💔 😿
I'd rather functions returning () would be handled by #[must_use] just like any other type.
Uninhabited types are irrelevant to the warning, but the current check is wrong regardless and should apply to any uninhabited types: not just ! and empty enums.
(), ! and empty enums are special cased here so that the #![deny(unused_results)] lint wouldn't fire in cases that don't really make sense for it to fire (Unused results lint fails on trivial program #43806).
#[must_use] uses the same method to detect unused results, so the same types end up getting special-cased, unintentionally.
Therefore, we should just remove the special casing for these types for #[must_use], making this a straightforward bug fix.
Activity
zackmdavis commentedon Oct 5, 2018
The fact that
unused-must-usedoesn't fire on thefoo();is a consequence of the lint pass returning early for()and!.I agree that it's useless to put
#[must_use]on a function that returns(), but I don't think anyone thought ahead and decided it should (or shouldn't) be an error. (I wouldn't want to write a dedicated lint for such an edge-case, and I think these days we tend to frown on non-lint warnings on account of their unsilenceability.)#48486 is a similar issue with trait implementations.
zackmdavis commentedon Oct 5, 2018
... well, come to think of it, I see a good case that this should trigger
unused_attributes, as @petrochenkov recently argued with respect to empty#[derive()]s.Havvy commentedon Oct 5, 2018
I concur. This should definitely cause
unused_attributesto fire.Centril commentedon Oct 6, 2018
Should the lint
unused_attributesalso fire for an arbitrary unit type in that case?There should be some consistency in the behavior.
hanna-kruppe commentedon Oct 6, 2018
By "unit type" do you mean
struct Foo;? I'd say no,must_useon user-defined types is always potentially sensible, e.g. ZSTs can be used as tokens for something that the caller should not usually forget to do.Centril commentedon Oct 6, 2018
@rkruppe I mean any terminal object in the category of Rust types, so
struct Foo;,((), ()),enum Foo { Bar }, and so on.abonander commentedon Oct 6, 2018
I would say an explicit
()and any arity of tuple that's all().Centril commentedon Oct 6, 2018
@abonander including recursively? e.g
((), ((), ()))abonander commentedon Oct 7, 2018
Probably, because those types typically don't have any meaning or semantics that can be added to them by the user. I guess it's possible with extension traits but that's going to be a very weird corner case.
Centril commentedon Oct 7, 2018
I'm down with that. It seems to me not too arbitrary a rule.
On the other hand, if we could get
#[must_use]on all types to emit a warning that could work as well.abonander commentedon Oct 7, 2018
Like @rkruppe said, custom ZSTs can have semantics attached to them so I don't think it's good to warn on
#[must_use]for those.hanna-kruppe commentedon Oct 7, 2018
I was skeptical about linting even () but forgetting to fill in the return type is at least a plausible scenario where such a lint could help. Going any further does not seem likely to help anyone in any scenario. Types like ((), ()) are rarely even constructed much less written in function signatures.
5 remaining items
abonander commentedon Oct 7, 2018
@zackmdavis yes but @Centril is talking about the exact opposite behavior, that the
unused_must_uselint should trigger regardless of the function's return type.Centril commentedon Oct 7, 2018
@abonander except for
!and the empty enum.As @zackmdavis I'm suggesting that if the
#[must_use]annotation has no effect, then a warning should be emitted. But a way to achieve that is for#[must_use] fn foo() {}have an effect. The other way is for#[must_use] fn foo() {}to triggerunused_attributes.zackmdavis commentedon Oct 7, 2018
Unfortunately, this turns out to not be easy because the function signature gives us
hir::Ty, whereas the UnusedResults pass is looking for aty::Ty. 💔 😿PR forthcoming anyway.
in which we lint `#[must_use]` functions with discardable return type
varkor commentedon Oct 7, 2018
()would be handled by#[must_use]just like any other type.!and empty enums.pnkfelix commentedon Oct 8, 2018
(deleted comment that was based on, I believe, a misunderstanding of @varkor's comment above.)
varkor commentedon Oct 8, 2018
This is my diagnosis of the issue:
(),!and emptyenums are special cased here so that the#![deny(unused_results)]lint wouldn't fire in cases that don't really make sense for it to fire (Unused results lint fails on trivial program #43806).#[must_use]uses the same method to detect unused results, so the same types end up getting special-cased, unintentionally.#[must_use], making this a straightforward bug fix.!and emptyenums is incorrect, we should be using something likety.conservative_is_uninhabited()(Less conservative uninhabitedness check #54125).)Centril commentedon Oct 8, 2018
Per @varkor's notes re. bug-fix I've relabeled to T-compiler instead.
varkor commentedon Oct 8, 2018
I've submitted #54920 with what I think is the right fix here.
Rollup merge of rust-lang#54920 - varkor:must_use-unit, r=estebank
Rollup merge of rust-lang#54920 - varkor:must_use-unit, r=estebank
Rollup merge of rust-lang#54920 - varkor:must_use-unit, r=estebank