rust-lang / rust-lang/rust-clippy

New lint: `unnecessary_option`

Open
#11,612 0 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

This lint would warn against using the Option type for local variables when the None value is unused.

Advantage

Remove the unnecessary Options makes the source code more readable and effective.

Drawbacks
  • To make the lint more effective and simple, the implementation of the lint should not be exhaustive. It makes sense to iterate over HIR and mark an Option variable as "used" if it is used as an argument of a function call, i.e. don't propagate these values across functions.
  • The lint will warn only at the definition point of the local variable. Therefore, the user has to fix all the cases when the Some value is used by himself.
Example
fn test() {
  let a = Some(5);
  // Any statements here, but the `None` value of `a` is unused, e.g.:
  // if a.is_some() { /* ... */ }
  // if let Some(_) = a { /* ... */ }
}

Could be written as:

fn test() {
  let a = 5;
}

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

Start by reviewing Clippy's existing lint implementations and the HIR traversal mentioned in the issue. Define the non-exhaustive scope around local variables and function-call arguments, then verify that the lint identifies the example's unnecessary Option and leaves cases requiring value propagation out of scope.

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.