rust-lang / rust-lang/rust-clippy

Option unwrap or outer loop continue can be pattern match

Open
#8,909 2 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

let a is Option<T>.
If both condition is met, the pattern can be written in pattern match:

  • a == None then continue outer loop
  • If not, t = a.unwrap() then using t later
Lint Name

loop_option_unwrap_with_continue

Category

pedantic

Advantage
  • Removing call to Option<T>#unwrap which can be written in more idiomatic way
Drawbacks

No response

Example
for a in b {
    let c: Option<T> = a;
    if c.is_none() {
        continue;
    }
    let e = c.unwrap();
    // using e
}

Could be written as:

for a in b {
    let c: Option<T> = a;
    match c {
        Some(e) => {
            // using e
        }
        None => continue;
    }
}

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 searching Clippy's existing lint implementations for loop and Option::unwrap patterns, along with their associated lint tests. Use the issue's examples to define the target pattern and verify that the new lint recognizes the Some/None control flow without flagging unrelated code.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.