Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map_flatten lint docs should include Option cases #8345

Closed
ParkMyCar opened this issue Jan 24, 2022 · 2 comments · Fixed by #8354
Closed

map_flatten lint docs should include Option cases #8345

ParkMyCar opened this issue Jan 24, 2022 · 2 comments · Fixed by #8354
Labels
A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy

Comments

@ParkMyCar
Copy link
Contributor

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

@camsteffen camsteffen added A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy labels Jan 24, 2022
@camsteffen
Copy link
Contributor

It looks like the lint is correct, but the documentation should be updated to mention that and_then is suggested for Option?

@ParkMyCar
Copy link
Contributor Author

@camsteffen exactly 🙂

@camsteffen camsteffen changed the title map_flatten lint suggests method that isn't available map_flatten lint docs should include Option cases Jan 25, 2022
@bors bors closed this as completed in fb94992 Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants