Skip to content

New lint: missing_must_use - #16919

Open
Kokoro2336 wants to merge 1 commit into
rust-lang:masterfrom
Kokoro2336:feat/type-must-use
Open

New lint: missing_must_use#16919
Kokoro2336 wants to merge 1 commit into
rust-lang:masterfrom
Kokoro2336:feat/type-must-use

Conversation

@Kokoro2336

@Kokoro2336 Kokoro2336 commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

View all comments

Closes #16858

changelog: New lint: missing_must_use

checklist:

  • Followed lint naming conventions
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Apr 27, 2026
@Kokoro2336 Kokoro2336 changed the title [type_must_use]: new lint [missing_must_use]: new lint Apr 27, 2026
@Kokoro2336 Kokoro2336 changed the title [missing_must_use]: new lint [missing_must_use]: new restriction-type lint Apr 27, 2026
@github-actions

github-actions Bot commented Apr 27, 2026

Copy link
Copy Markdown

Lintcheck changes for c8f3e8f

Lint Added Removed Changed
clippy::missing_must_use 4412 0 0

This comment will be updated if you push new changes

@Kokoro2336 Kokoro2336 changed the title [missing_must_use]: new restriction-type lint New lint: missing_must_use Apr 27, 2026
@Kokoro2336
Kokoro2336 marked this pull request as ready for review April 27, 2026 11:59
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 27, 2026
@rustbot

rustbot commented Apr 27, 2026

Copy link
Copy Markdown
Collaborator

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

Why was this reviewer chosen?

The reviewer was selected based on:

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

@rustbot

This comment has been minimized.

Comment thread clippy_lints/src/missing_must_use.rs Outdated
@rustbot

This comment has been minimized.

@Kokoro2336

Kokoro2336 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

@LebedevRI I've added support for Trait/Impl/Fn, with testcases supplied.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026
@Kokoro2336
Kokoro2336 force-pushed the feat/type-must-use branch from 045bdb1 to 91fc83e Compare June 3, 2026 06:41
@rustbot

rustbot commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in clippy_lints/src/doc

cc @notriddle

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) has-merge-commits PR has merge commits, merge with caution. labels Jun 3, 2026
@rustbot

This comment has been minimized.

@Kokoro2336

Copy link
Copy Markdown
Contributor Author

Oh no, it seems that there're some problems in rebasing. I'll fix it later.

pull Bot pushed a commit to xtqqczze/rust-lang-rust-clippy that referenced this pull request Jun 3, 2026
…bute case (rust-lang#17144)

changelog: [`double_must_use`]: make the lint machine-applicable in
single-attribute case

After rust-lang#16919, not having
this fix-it will be unfortunate,
since that lint will cause many new instances of this diag.

Refs rust-lang#16856
@LebedevRI

Copy link
Copy Markdown
Contributor

The PR description should also Close #16859,
at first glance i hadn't realized this wasn't just handling types, but functions too. Nice!

Looking at clippy_lints/src/functions/must_use.rs,
i notice that there are some ignored cases:

  • proc macros
  • #[no_mangle] functions

I would suggest having tests for those cases.

I would also suggest adding tests with macros (especially external ones),
there may be unhandled situations there.

@Kokoro2336
Kokoro2336 force-pushed the feat/type-must-use branch from 91fc83e to 4b239de Compare June 28, 2026 11:05
@rustbot

rustbot commented Jun 28, 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.

@Kokoro2336
Kokoro2336 force-pushed the feat/type-must-use branch from 4b239de to 933f21d Compare June 28, 2026 11:24
@Kokoro2336

Kokoro2336 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

@LebedevRI I've fixed the rebasing

@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.

This is a good start, but I think the lint is way too trigger happy. I left some suggestions to reduce the churn.

View changes since this review

Comment thread clippy_lints/src/missing_must_use.rs Outdated
Comment thread clippy_lints/src/missing_must_use.rs Outdated
Comment thread tests/ui/missing_must_use.rs
Comment thread tests/ui/missing_must_use.rs
Comment thread tests/ui/missing_must_use.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 29, 2026
@rustbot

rustbot commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Kokoro2336
Kokoro2336 force-pushed the feat/type-must-use branch 2 times, most recently from f231267 to e14357c Compare June 29, 2026 14:42
refactor: renaming.

chore: cargo dev fmt

chore: cargo dev update_lints

feat: add help.

fix: ignore external macro.

test: add test.

doc: update documentation.

fix: lint message format.

enhance: add suggestion.

fix: lint Impl/Trait/Fn.
@Kokoro2336
Kokoro2336 force-pushed the feat/type-must-use branch from e14357c to c8f3e8f Compare June 29, 2026 14:49
@Kokoro2336

Copy link
Copy Markdown
Contributor Author

@llogiq Done @rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 29, 2026
@Kokoro2336
Kokoro2336 requested a review from llogiq June 29, 2026 14:59
Comment on lines +58 to +66
// Check if the item is public. If it's not public, we don't need to check it for `#[must_use]`.
if !cx.tcx.visibility(item.owner_id).is_public() {
return;
}

match item.kind {
ItemKind::Struct(_, _, data) if !data.fields().is_empty() => check_item(cx, item.hir_id(), item.span),
ItemKind::Enum(_, _, def) if !def.variants.is_empty() => check_item(cx, item.hir_id(), item.span),
ItemKind::Union(..) => check_item(cx, item.hir_id(), item.span),

@LebedevRI LebedevRI Jun 29, 2026

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.

FWIW, this doesn't solve #16858.
I've very explicitly noted that it should diagnose everything.
It's fine if such a behaviour can be toggled off (on?) via config, but it should be possible.

View changes since the review

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.

Just so I understand your use case better, why do you want the lint to mark every 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.

Thank you for taking a look!

I do think this check is an improvement even if it only diagnoses non-empty public types,
however, i really do feel like such dichotomy should be configurable,
since then it obviously serves more use-cases that way.

We can of course argue about the defaults,
i'd think it should be more noisy by default, since it's easier to opt-out of something
that you know is there than opt-in into something you don't nessesairly know about.
And it's a restrict check anyways.

Likewise, i've intentionally filed #16858 and #16859 as a separate issues,
so i don't nessesairly think that this check should deal with the fns,
only type/trait definitions.

But to answer the question, mainly, i'm simply of an opinion that Rust got this default wrong,
must-use-ness should have been the other way around.
How often does one have fns that return something that is always discarded at all callsites?
I think that is not a very common pattern...

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.

While I personally like must-use (which is why I wrote the must_use_candidate lint), I disagree about your assertion that the default is wrong. I believe that defaulting to must-use would have led to the false sense of security with regards to the running of drop impls.

Given that those annotations carry a cost, even if it is just one more thing to ignore while reading the code, we should aim to make the most of them. Therefore my aim was to restrict the lint to cases that matter. That is why I asked for those changes. We can always make the lint more configurable, but the default should carefully balance costs and benefits.

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 don't disagree.

Given that those annotations carry a cost, even if it is just one more thing to ignore while reading the code, we should aim to make the most of them. Therefore my aim was to restrict the lint to cases that matter. That is why I asked for those changes. We can always make the lint more configurable, but the default should carefully balance costs and benefits.

Note: in previous comment, by opt-out of, i was of course talking about the config option. Of course, silencing each particular lint site would be a problematic solution, given that they'd all have the same root-cause (=empty type, etc)

Just please don't make the current behaviour (no diag) non-configurable.

I would say, discovering that half of a lint is off by default and is hidden
behing a config option is a bit hard to discover, though.
But i don't suppose there's much appetite for issuing those edge-cases
under their own lint names either?
(public-sized; public-empty, non-public-sized, non-public-empty)

@rustbot

rustbot commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17329) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP 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.

Stylistic lint suggestion: #[must_use] for types

4 participants