Skip to content

map_flatten lint docs should include Option cases #8345

@ParkMyCar

Description

@ParkMyCar

Description

Clippy suggests replacing _.map(_).flatten(_) with .and_then(_) for an Option. For example, this code:

Some(10)
  .map(|val| if val % 2 == 0 { Some(2) } else { None })
  .flatten();

generates this clippy lint

warning: called `map(..).flatten()` on an `Option`
 --> src/main.rs:2:13
  |
2 |       Some(10)
  |  _____________^
3 | |         .map(|val| if val % 2 == 0 { Some(2) } else { None })
4 | |         .flatten();
  | |__________________^ help: try using `and_then` instead: `.and_then(|val| if val % 2 == 0 { Some(2) } else { None })`
  |
  = note: `#[warn(clippy::map_flatten)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten

But the webpage linked in this lint suggests using _.flat_map(_) for Option types, yet .flat_map(_) does not exist on Option, instead the equivalent is .and_then(_). This isn't a huge deal, but it can be confusing when the clippy suggests one thing, and the link with further information suggests something else.

Version

rustc 1.60.0-nightly (777bb86bc 2022-01-20)
binary: rustc
commit-hash: 777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b
commit-date: 2022-01-20
host: x86_64-apple-darwin
release: 1.60.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documentationArea: Adding or improving documentationgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions