rust-lang / rust-lang/rust-clippy

Suggest removing `if let` mapping to identity instead of suggesting the use of `map` and the remval in separate steps

Open
#8,230 0 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

This lint suggests the removal of an unnecessary if let statement that just reconstructs the original value.

It is essentially the combination of the clippy::manual_map and clippy::map_identity. Instead of having the user perform the refactor in two separate steps, invoking cargo clippy in-between, the user could directly replace the code with the simpler version.

A minimal reproducible example can be found at https://github.com/TimJentzsch/clippy_option_map.

Lint Name

if_let_identity

Category

style, complexity

Advantage
  • Easier to read
  • Less complexity
  • Having one lint instead of two separate ones will make it easier for the user to refactor the code.
Drawbacks
  • An additional lint, which is covered by two separate lints already
Example
fn foo(bar: Option<u8>) -> Option<u8> {
    if let Some(val) = bar {
        Some(val)
    } else {
        None
    }
}

Could be written as:

fn foo(bar: Option<u8>) -> Option<u8> {
    bar
}

Instead, Clippy suggests:

warning: manual implementation of `Option::map`
  --> src/main.rs:7:5
   |
7  | /     if let Some(val) = bar {
8  | |         Some(val)
9  | |     } else {
10 | |         None
11 | |     }
   | |_____^ help: try this: `bar.map(|val| val)`
   |
   = note: `#[warn(clippy::manual_map)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map

warning: `clippy_option_map` (bin "clippy_option_map") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

After applying this refactor and invoking Clippy again, it will suggest:

warning: unnecessary map of the identity function
 --> src/main.rs:7:8
  |
7 |     bar.map(|val| val)
  |        ^^^^^^^^^^^^^^^ help: remove the call to `map`
  |
  = note: `#[warn(clippy::map_identity)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_identity

warning: `clippy_option_map` (bin "clippy_option_map") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s

Arriving at the same solution, but with an additional step.

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 with the minimal reproducible example and inspect Clippy's existing manual_map and map_identity lints to understand their matching and diagnostics. Add the proposed if_let_identity lint so this pattern is handled directly, and verify that the example produces the simpler replacement without requiring two lint passes.

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.