rust-lang / rust-lang/rust-clippy

`option_vec`, a lint that checks for `Option<Vec<T>>`

Open
#10,899 13 comments 4 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

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.