rust-lang / rust-lang/rust-clippy

Simplify if-let-Some-else-None using question mark operator

Open
#13,626 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Consider the code below.

pub fn f(x: Option<u32>) -> Option<u32> {
    if let Some(x) = x {
        dbg!(x);
        Some(x * 2)
    } else {
        None
    }
}

Standard Clippy produces no warning. More exhaustive options suggest code that is no significantly better:

$ cargo clippy --all -- -W clippy::all -W clippy::nursery
error: use Option::map_or instead of an if let/else
 --> src/lib.rs:2:5
  |
2 | /     if let Some(x) = x {
3 | |         dbg!(x);
4 | |         Some(x * 2)
5 | |     } else {
6 | |         None
7 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
  = note: `-D clippy::option-if-let-else` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(clippy::option_if_let_else)]`
help: try
  |
2 ~     x.map_or(None, |x| {
3 +         dbg!(x);
4 +         Some(x * 2)
5 +     })
  |

However, the code can be written with less nesting, using the question mark operator:

pub fn f(x: Option<u32>) -> Option<u32> {
    let x = x?;
    dbg!(x);
    Some(x * 2)
}

Note that Clippy does suggest that for the code below:

pub fn f(x: Option<u32>) -> Option<u32> {
    let Some(x) = x else {
        return None;
    };
    dbg!(x);
    Some(x * 2)
}
error: this `let...else` may be rewritten with the `?` operator
 --> src/lib.rs:2:5
  |
2 | /     let Some(x) = x else {
3 | |         return None;
4 | |     };
  | |______^ help: replace it with: `let x = x?;`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
  = note: `-D clippy::question-mark` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(clippy::question_mark)]`

This pattern is particularly frequent when implementing iterators.

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 comparing the existing question_mark and option_if_let_else lints and their tests. Determine how the if let/else None pattern can be recognized without changing cases that need map_or; done means Clippy suggests the question-mark form for the reported example and the relevant lint tests pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.