rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

New lint: `unit_as_impl_trait`

Open samueltardieu opened this issue 11 months ago • 13 comments

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

samueltardieu avatar Jan 01 '25 22:01 samueltardieu

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

rustbot avatar Jan 01 '25 22:01 rustbot

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.

llogiq avatar Jan 06 '25 19:01 llogiq

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.

samueltardieu avatar Jan 06 '25 21:01 samueltardieu

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.

Pzixel avatar Jan 07 '25 13:01 Pzixel

@rustbot author

samueltardieu avatar Jan 07 '25 13:01 samueltardieu

:umbrella: The latest upstream changes (possibly 8eed35023f26bc45ab86d5b8f080c4025faa58cf) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Apr 16 '25 06:04 rustbot

:umbrella: The latest upstream changes (possibly 7bac114c8645d40291502c0d8028ffd16c7e8c01) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar May 13 '25 05:05 rustbot

:umbrella: The latest upstream changes (possibly b379d54c22f9f6b9577075d7f89645ac2498c931) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Jun 05 '25 19:06 rustbot

:umbrella: The latest upstream changes (possibly 368b2355797b13511776f0a35ec4864468b87ece) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Sep 08 '25 08:09 rustbot

:umbrella: The latest upstream changes (possibly 5b23bd479e69db6167259c2456f2d8c738dc0796) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 06 '25 21:10 rustbot

:umbrella: The latest upstream changes (possibly 0a2eeceefcd99185643f7659ce25b6376749c271) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 07 '25 16:10 rustbot

:umbrella: The latest upstream changes (possibly e121ab877ee1b0d9c8395b70ecab895222c10f57) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Nov 02 '25 09:11 rustbot

:umbrella: The latest upstream changes (possibly a10cafebcdd63d95823ec8bff67a22a71a15a32d) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Dec 02 '25 19:12 rustbot