rust-lang / rust-lang/rust-clippy

Pedantic lint: unhandled_must_use

Open
#13,997 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does
#[must_use]
struct MyStruct;
fn x() -> MyStruct {
  MyStruct
}

fn main() {
  let x = x();
  if core::hint::black_box(false) {
    return;
  }
  drop(x);
}

For the above snippet, must_use will not be emitted as a warning because the variable binding is considered usage. The proposed lint would consider any bound must_use types at the termination of scope unused. The above example includes the edge-case where the scope has multiple points of potential termination and the binding is used at one location but not the other.

I will note this may be hard to implement on any must_use type which also implements Copy. This either needs to not trigger on must_use + Copy, ignoring that class of types, or determine if the variable was copied, and if so, only continue evaluation for the copies' bindings (if any exist).

Alternative name suggestions for the lint welcome. in_scope_must_use? unmoved_must_use?

Advantage
  • Ensures must_use types are 'handled', not just bound.
  • My provocation for this is a DbTxn type I have. I once lost days of developer time because I forgot to call commit, despite the must_use present. I would like my clippy to error if I ever don't call commit or explicitly drop any DbTxn.
Drawbacks
  • Pedantic lint, making it low priority
Example
  let x: Result<(), ()> = Err(());
  if core::hint::black_box(false) {
    return;
  }
  drop(x);

Could be written as:

  let x: Result<(), ()> = Err(());
  if core::hint::black_box(false) {
    drop(x);
    return;
  }
  drop(x);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are identified in the issue. Start by reproducing the Rust snippets and examining how scope termination, multiple exits, and must_use plus Copy should behave. Done means the proposed lint reliably reports bound must_use values that are not handled while avoiding false positives for valid uses.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.