rust-lang/rust-clippy

Add lint for `bare_must_use` - `#[must_use]` without a note

Aperta

#16.466 aperta il 25 gen 2026

 (5 commenti) (1 reazione) (1 assegnatario)Rust (1391 fork)batch import
A-lintgood first issue

Metriche repository

Star
 (10.406 stelle)
Metriche merge PR
 (Merge medio 19g 22h) (113 PR mergiate in 30 g)

Descrizione

What it does

Stylistic lint that warns on bare #[must_use] attributes without a #[must_use = "note of some sort"]. Was actually surprised it didn't exist already.

Advantage

  • For the types of projects where maximum documentation is desired (and clippy is already turned up to 11), this adds another means by which code intent can be captured.
  • Adds a note: clause in the compiler error that explains this intent at the callsite.

Drawbacks

None that I can think of. Probably shouldn't be enabled by default though.

Example

#[must_use]                                 // <- would warn if `clippy::bare_must_use = "warn"`
fn get_foo() -> usize {
    // ... some expensive operation ...
    42
}

Could be written as:

#[must_use = "getting foo is very expensive"]
fn get_fo() -> usize {
    // ... some expensive operation ...
    42
}

Comparison with existing lints

There exist already a bunch of #[must_use]-related lints, but seemingly none of them check adding the notes.

Additional Context

This is a safety-critical project whereby as much documentation, verbosity in linting and testing, etc. is preferred and desired. I didn't even know #[must_use] took a note string until I saw it in the standard library sources a few minutes ago, and discovered clippy didn't have a lint to enforce that as I think it's better (and more enforceable) than asking people to add a // reason comment next to the #[must_use] in all cases.

Guida contributor