rust-lang/rust-clippy

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

Open

#16.466 aberto em 25 de jan. de 2026

Ver no GitHub
 (5 comments) (1 reaction) (1 assignee)Rust (1.391 forks)batch import
A-lintgood first issue

Métricas do repositório

Stars
 (10.406 stars)
Métricas de merge de PR
 (Mesclagem média 16d 6h) (79 fundiu PRs em 30d)

Description

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.

Guia do colaborador