Skip to content

manual_map suggests a snippet that would not compile for chained if #6969

Closed
@magurotuna

Description

@magurotuna

I tried this code:

fn main() {
    let a = Some(42);
    let b = Some(0);

    let _c = if let Some(val) = b {
        Some(val + 99)
    } else if let Some(val) = a {
        Some(val * 2)
    } else {
        None
    };
}

Running clippy on the above code, clippy says:

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

Following the help message, the code changes to:

let _c = if let Some(val) = b {
    Some(val + 99)
} else a.map(|val| val * 2);

Of course, it doesn't compile:

error: expected `{`, found `a`
  --> src/main.rs:14:12
   |
14 |     } else a.map(|val| val * 2);
   |            ^-------------------
   |            |
   |            expected `{`
   |            help: try placing this code inside a block: `{ a.map(|val| val * 2) }`

error: aborting due to previous error

Meta

  • cargo clippy -V: clippy 0.1.52 (07e0e2e 2021-03-24)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions