Skip to content

manual_map suggestion cannot be applied: if let.. else if let .. chain #6847

@matthiaskrgr

Description

@matthiaskrgr

Lint name: manual_map

I tried this code:

fn fun(a: Option<i32>) -> Option<i32> {
    if let Some(i) = a {
        Some(1 + i)
    } else if let Some(j) = a {
        Some(3 + j)
    } else {
        None
    }
}

Clippy suggests this:

warning: manual implementation of `Option::map`
  --> src/main.rs:8:12
   |
8  |       } else if let Some(j) = a {
   |  ____________^
9  | |         Some(3 + j)
10 | |     } else {
11 | |         None
12 | |     }
   | |_____^ help: try this: `a.map(|j| 3 + j)`
   |
   = 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: 2 warnings emitted

but it fails to apply because we have an if .. else if .. else chain which is not taken into account and the suggestion causes syntax errors.

The following errors were reported:
error: expected `{`, found `a`
 --> src/main.rs:8:12
  |
8 |     } else a.map(|j| 3 + j)
  |            ^---------------
  |            |
  |            expected `{`
  |            help: try placing this code inside a block: `{ a.map(|j| 3 + j) }`

error: aborting due to previous error

cc @Jarcho ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions