Skip to content

Must use without note#16592

Open
skatromb wants to merge 2 commits intorust-lang:masterfrom
skatromb:must-use-without-note
Open

Must use without note#16592
skatromb wants to merge 2 commits intorust-lang:masterfrom
skatromb:must-use-without-note

Conversation

@skatromb
Copy link

@skatromb skatromb commented Feb 19, 2026

PR addresses proposal for adding a lint that warns about #[must_use] attribute that lacks a note.


Request for a help

I feel I need some opinions here. I stuck on dogfood tests after running cargo test --test dogfood.

The issue is that I received a lot of lint warnings regarding the #[must_use] attribute without a note (which was what we wanted). However, the more I looked into them, the more I realized that most of the attributes do not make much sense. For example, they are set on getters and formatters. As I started deleting this attribute from these functions, I immediately received a "must_use_candidate" lint warning. One could argue that this is correct because if you call a "pure" function and don't use its result, you've done something wrong, so they're perfect candidates for must_use. On the other hand, setting this attribute on almost every function bloats the code and devalues lint. Adding a note "just because the function is pure" on must_use worsens the situation even more.

I ended up in a situation where I couldn't remove the "must_use" attribute without turning off the "must_use_candidate" lint. On the other hand, I feel it should be up to the developer to decide whether to mark a function as #[must_use] or not, and it shouldn't be so formal. A computational-heavy function or the necessity to handle important state could be good reasons, but they shouldn't be the only formal reasons for function purity. As far as I can tell, the only situation prevented by "must_use_candidate" is calling a function without the "let = must_use_func()" assignment: must_use_func(). Other cases, such as assigning to a variable and not using it, are covered by other lints.

I also found a standard library developers guide: when to add #[must_use], which suggests "The #[must_use] attribute can be applied to types or functions when failing to explicitly consider them or their output is almost certainly a bug."

So what do you think, should I fix all must_use attributes adding some formal note, or should we reconsider must_use_candidate lint?


changelog: [must_use_without_note]: add a lint that warns on #[must_use] attributes without a note

@github-actions
Copy link

github-actions bot commented Feb 19, 2026

Lintcheck changes for 3a164cc

Lint Added Removed Changed
clippy::must_use_candidate 0 0 4657
clippy::must_use_without_note 534 0 0

This comment will be updated if you push new changes

@rustbot

This comment has been minimized.

/// ```
#[clippy::version = "1.95.0"]
pub MUST_USE_WITHOUT_NOTE,
style,
Copy link
Member

Choose a reason for hiding this comment

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

Note that restriction would probably be a better choice for such a lint.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Can you elaborate on why do you think so?
I am doubting it should be restriction, looking at the docs, they state "lints which prevent the use of Cargo features", while #[must_use] attribute could be used anywhere.

Copy link
Author

@skatromb skatromb Feb 27, 2026

Choose a reason for hiding this comment

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

Hm, but now I found another doc, which says different about this group. However, I am not sure this is applicable either

The clippy::restriction group contains lints that will restrict you from using certain parts of the Rust language. It is not recommended to enable the whole group, but rather cherry-pick lints that are useful for your code base and your use case.
Lints from this group will restrict you in some way. If you enable a restriction lint for your crate it is recommended to also fix code that this lint triggers on. However, those lints are really strict by design, and you might want to #[allow] them in some special cases, with a comment justifying that.

Copy link
Member

Choose a reason for hiding this comment

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

This lint is restricting the use of #[must_use] unless it is accompanied with a message. This is an opiniated lint which, I think, belongs to restriction.

@skatromb skatromb force-pushed the must-use-without-note branch 2 times, most recently from 9dc4ba7 to dd76a0b Compare February 26, 2026 08:08
@skatromb skatromb force-pushed the must-use-without-note branch from dd76a0b to 3a164cc Compare February 27, 2026 17:36
@skatromb skatromb marked this pull request as ready for review February 27, 2026 21:18
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 27, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 27, 2026

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: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

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

Labels

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

4 participants