New lint: `unit_as_impl_trait`
This lint implements the suggestion from #13909, and requires that an explicit () is used when a function returns () while declaring an impl trait return type.
I have used specialized messages for three different situations:
- An empty body
- A body ending with a statement
- A body ending with an expression returning
()
One should note that adding the explicit () as suggested will cause the unused_unit lint (default: warn) to trigger. This should be possible to silence it inside functions returning impl traits if needed, to avoid having contradictory suggestions. Thoughts?
Close #13909
changelog: [unit_as_impl_trait]: new lint
r? @llogiq
rustbot has assigned @llogiq. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.
Use r? to explicitly pick a reviewer
This should be possible to silence it inside functions returning impl traits if needed, to avoid having contradictory suggestions. Thoughts?
I feel that having () implement traits is a sufficiently weird technique (it's ok for prototype code, but in production, I'd suggest using different zero-sized structs instead) that warning is apropos.
On the other hand, the suggestion is not too helpful in my opinion, given my reasoning stated above.
I feel that having
()implement traits is a sufficiently weird technique (it's ok for prototype code, but in production, I'd suggest using different zero-sized structs instead) that warning is apropos.
Yes, and as seen in the issue, it exists in production, for example in Axum.
I feel that having
()implement traits is a sufficiently weird technique (it's ok for prototype code, but in production, I'd suggest using different zero-sized structs instead) that warning is apropos.
() implements traits from std, so you can unintentionally use it without any warning, See another example: https://internals.rust-lang.org/t/rust-should-warn-on-impl-trait-returning-unit
And I think it's safe to assume that people in general won't make MyHash and MyEq traits just to avoid this.
@rustbot author
:umbrella: The latest upstream changes (possibly 8eed35023f26bc45ab86d5b8f080c4025faa58cf) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly 7bac114c8645d40291502c0d8028ffd16c7e8c01) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly b379d54c22f9f6b9577075d7f89645ac2498c931) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly 368b2355797b13511776f0a35ec4864468b87ece) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly 5b23bd479e69db6167259c2456f2d8c738dc0796) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly 0a2eeceefcd99185643f7659ce25b6376749c271) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly e121ab877ee1b0d9c8395b70ecab895222c10f57) made this pull request unmergeable. Please resolve the merge conflicts.
:umbrella: The latest upstream changes (possibly a10cafebcdd63d95823ec8bff67a22a71a15a32d) made this pull request unmergeable. Please resolve the merge conflicts.