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

Suggest replacing filter-for-some-and-unwrap with .flatten #6061

Closed
NoraCodes opened this issue Sep 17, 2020 · 9 comments · Fixed by #6342
Closed

Suggest replacing filter-for-some-and-unwrap with .flatten #6061

NoraCodes opened this issue Sep 17, 2020 · 9 comments · Fixed by #6342
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@NoraCodes
Copy link

NoraCodes commented Sep 17, 2020

What it does

Suggests replacing code that filters with Option::is_some and then maps Option::unwrap with .flatten() which does the same thing due to Option implementing IntoIterator.

Categories (optional)

  • Kind: clippy::style

Advantage

It is one function call fewer and makes it clearer what is happening - one level of indirection is being removed. In addition, many codebases want to eliminate all possible panic sources, of which Option::unwrap is one, although in this case it is statically known not to panic.

Drawbacks

None.

Example

fn odds_out(x: i32) -> Option<i32> { if x % 2 == 0 { Some(x) } else { None } }
let evens: Vec<_> = myints.iter().map(odds_out).filter(Option::is_some).map(Option::unwrap).collect();

Could be written as:

fn odds_out(x: i32) -> Option<i32> { if x % 2 == 0 { Some(x) } else { None } }
let evens: Vec<_> = myints.iter().map(odds_out).flatten().collect();
@NoraCodes NoraCodes added the A-lint Area: New lints label Sep 17, 2020
@flip1995 flip1995 added L-suggestion Lint: Improving, adding or fixing lint suggestions good-first-issue These issues are a good way to get started with Clippy labels Sep 17, 2020
@bbqbaron
Copy link

Heyo, could I take a crack at this? New Rust user, but I certainly understand the intent here so hopefully should be able to suss it out.

@ghost
Copy link

ghost commented Sep 18, 2020

Shouldn't the recommendation itself be linted to use filter_map?

@bbqbaron
Copy link

Shouldn't the recommendation itself be linted to use filter_map?

My (newbie) understanding is that .filter_map would require some closure arg |x| x to return the option x, whereas .flatten just converts the options to to iters, which automatically removes any empty ones. May be a matter of taste, but .flatten seems more direct.

@ghost
Copy link

ghost commented Sep 20, 2020

I meant replacing the map(..).flatten(..) chain with filter_map. In fact we already have a pedantic lint, clippy::map_flatten, which triggers on the recommendation.

Playground example

@bbqbaron
Copy link

I meant replacing the map(..).flatten(..) chain with filter_map. In fact we already have a pedantic lint, clippy::map_flatten, which triggers on the recommendation.

Playground example

Ah, that makes sense. I wonder if maybe this example is a red herring, since you might chain filter into map given any iterable of options, even one you didn't construct. Ie, the map(odds_out) could be seen as just a contrived part of the setup, not the "target code" being checked?

I leave it to maintainers' judgment, since they know better than I what code people tend to write often enough to be worth checking, and how exhaustive we want our list of cases to be.

@flip1995
Copy link
Member

flip1995 commented Oct 5, 2020

So on the one hand @mikerite is right, that map(..).flatten(..) gets linted by Clippy, so the suggested code in the above example would again get linted. But the filter(is_some).map(unwrap) construct doesn't have to follow a .map(..) call in the iterator chain. So suggesting to use flatten(..) in this case is valid, IMO.

But rather than implementing this as a new lint, I just would enhance the filter_map lint with that special case.

@flip1995 flip1995 added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed A-lint Area: New lints labels Oct 5, 2020
@mlegner
Copy link
Contributor

mlegner commented Nov 4, 2020

@flip1995 So you suggest to check for both .map(..).filter(is_some).map(unwrap) and .filter(is_some).map(unwrap) and suggest .filter_map(..) and .flatten(), respectively? Currently, only chains of two methods are considered in the lint. So simply checking for .filter(is_some).map(unwrap) would fit the current structure better but would potentially require the programmer to change their code twice.

@bbqbaron Are you still working on this?

@bbqbaron
Copy link

bbqbaron commented Nov 4, 2020

@mlegner I've been following the convo. I got a hacky implementation working when I first picked it up, just to get the project running, and then was waiting for this desired behavior question to shake out.

@flip1995
Copy link
Member

flip1995 commented Nov 8, 2020

So you suggest to check for both .map(..).filter(is_some).map(unwrap) and .filter(is_some).map(unwrap) and suggest .filter_map(..) and .flatten(), respectively?

Nope, I'm suggesting only linting .filter(is_some).map(unwrap) and let the other lint handle the map() case, if applicable. Linting applied suggestions is totally fine for Clippy.

bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Nov 15, 2020
bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Nov 15, 2020
bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Nov 29, 2020
bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Feb 28, 2021
bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Feb 28, 2021
bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Mar 30, 2021
bbqbaron pushed a commit to bbqbaron/rust-clippy that referenced this issue Mar 30, 2021
@bors bors closed this as completed in 775ef47 Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants