rust-lang / rust-lang/rust-clippy
New lint: `unnecessary_option`
Open
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
Optionvariable 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
Somevalue 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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