Skip to content

Use #[must_use] determination from the compiler#16633

Open
samueltardieu wants to merge 2 commits into
rust-lang:masterfrom
samueltardieu:features/must-use-from-compiler
Open

Use #[must_use] determination from the compiler#16633
samueltardieu wants to merge 2 commits into
rust-lang:masterfrom
samueltardieu:features/must-use-from-compiler

Conversation

@samueltardieu

@samueltardieu samueltardieu commented Feb 24, 2026

Copy link
Copy Markdown
Member

View all comments

changelog: [double_must_use, let_underscore_must_use, must_use_candidates]: better determination of #[must_use] needs by directly using the compiler algorithm

For example, Box<T> is considered #[must_use] when T is.

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch from 35d95f3 to 7392cc1 Compare March 5, 2026 16:57
@github-actions

github-actions Bot commented Mar 5, 2026

Copy link
Copy Markdown

Lintcheck changes for d4b1ee5

Lint Added Removed Changed
clippy::double_must_use 4 0 0
clippy::let_underscore_must_use 0 0 174
clippy::must_use_candidate 1 4 2

This comment will be updated if you push new changes

@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch from 7392cc1 to e083a22 Compare March 5, 2026 22:19
@WaffleLapkin

Copy link
Copy Markdown
Member

Hey! Since rust-lang/rust#153018 merged a while ago, can this be revived?

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch 2 times, most recently from 62ad561 to b0ba9bd Compare June 3, 2026 11:37
@samueltardieu samueltardieu marked this pull request as ready for review June 3, 2026 11:38
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 3, 2026
@rustbot

rustbot commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

r? @dswij

rustbot has assigned @dswij.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, dswij, llogiq

@samueltardieu

Copy link
Copy Markdown
Member Author

@WaffleLapkin Could you have a look at the second commit in this PR, and see if it would be possible to get out the MustUsePath description like I had to do in describe_must_use_type()? The emit_must_use_untranslated() in the compiler's rustc_lint/src/unused/must_use.rs mixes computing the message and emitting the lint, while I just need the type description.

@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch from b0ba9bd to d284340 Compare June 3, 2026 11:44
@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch from d284340 to 0914507 Compare June 3, 2026 17:58
@rustbot

This comment has been minimized.

--> tests/ui/double_must_use.rs:40:37
|
LL | async fn async_must_use_result() -> Result<(), ()> {
| ^^^^^^^^^^^^^^

@WaffleLapkin WaffleLapkin Jun 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe special casing this to say "async function returns a future which is always considered as #[must_use]" or something like that could make sense.

View changes since the review

LL | pub fn must_use_result_with_uninhabited_2() -> Result<T, !> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the return type is `T` in a `Result` with an uninhabited error

@WaffleLapkin WaffleLapkin Jun 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is worded nicely... The return type is not T... it's Result<...>. But then must-useness depends on T... mh.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to parse this as "the return type is (T in a Result with an uninhabited error)". Note that the part in "()" is generated by the code lifted up from the compiler to describe the Result<T, !> type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think that the wording could be improved, but that's not a blocker for this PR.

@WaffleLapkin

WaffleLapkin commented Jun 9, 2026

Copy link
Copy Markdown
Member

@samueltardieu I think you can refactor compiler's emit_must_use_untranslated such that there is a separate function for computing the description (that can then be used in clippy). If you want I can push a commit here doing that (assuming I can push to your branch, not sure how permissions work here...)

@samueltardieu

samueltardieu commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

@samueltardieu I think you can refactor compiler's emit_must_use_untranslated such that there is a separate function for computing the description (that can then be used in clippy). If you want I can push a commit here doing that (assuming I can push to your branch, not sure how permissions work here...)

We use a particular version of the compiler through the rustc-dev package, so this refactoring should be done in the compiler repository, then can be used in Clippy after a sync.

@WaffleLapkin

Copy link
Copy Markdown
Member

Would you be interested in doing that refactor yourself / should I do it?

Either way I think it might be easier to merge this as-is and refactor later.

@samueltardieu

Copy link
Copy Markdown
Member Author

Would you be interested in doing that refactor yourself / should I do it?

I'll do it.

Either way I think it might be easier to merge this as-is and refactor later.

Agreed, this way I will be able to do the refactor + switch to the refactored compiler version once this one is merged and synced back into the compiler repository.

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch from 0914507 to 9c65d0f Compare June 26, 2026 07:39
@rustbot

This comment has been minimized.

@samueltardieu

Copy link
Copy Markdown
Member Author

Friendly ping @dswij

@rustbot

This comment has been minimized.

This is inspired by the compiler. Ideally, this function should be
provided by the compiler itself.
@samueltardieu samueltardieu force-pushed the features/must-use-from-compiler branch from 9c65d0f to d4b1ee5 Compare July 9, 2026 18:19
@rustbot

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@samueltardieu

Copy link
Copy Markdown
Member Author

r? clippy

@rustbot rustbot assigned llogiq and unassigned dswij Jul 9, 2026

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from a small nit, this looks like a good improvement.

View changes since this review

/// payload is an uninhabited type, the `Ok`/`Continue` payload type will be used instead.
///
/// The [`MustUsePath`] can be used to describe the type through [`describe_must_use_type`].
pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<MustUsePath> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename the function to better match the intent. Perhaps opt_must_use_path?

LL | pub fn must_use_result_with_uninhabited_2() -> Result<T, !> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the return type is `T` in a `Result` with an uninhabited error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think that the wording could be improved, but that's not a blocker for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants