rust-lang / rust-lang/rust-clippy
`option_vec`, a lint that checks for `Option<Vec<T>>`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Checks for instances of Option<Vec<T>>, and recommends that they be converted to Vec<T>.
Advantage
Option<Vec<T>> holds very little advantage over Vec<T>, as a Vec<T> can already hold zero items.
If the user wishes to enforce that a Vec<T> have some number of values, they can use Rust's type system to enforce this:
pub struct MyAst {
/// Must have at least one value
identifiers: Vec<String>,
}
// ...can be rewritten as...
pub struct MyAst {
first_identifier: String,
other_identifiers: Vec<String>,
}
(Although this may result in more complex code when consuming structs made this way)
Drawbacks
I do not know of any drawbacks to this lint. If I think of any, I will edit them into this section.
Example
pub struct MyAst {
identifiers: Option<Vec<String>>,
}
Could be written as:
pub struct MyAst {
identifiers: Vec<String>,
}
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 in the rust-clippy repository by locating existing lints that inspect Rust type structure, then review their associated tests and documentation patterns. Implement a lint for Option<Vec> that recommends Vec, and verify that the example pattern is detected without unwanted matches.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100