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

lint flat_map into filter_map #2241

Closed
KamilaBorowska opened this issue Nov 21, 2017 · 10 comments · Fixed by #7101
Closed

lint flat_map into filter_map #2241

KamilaBorowska opened this issue Nov 21, 2017 · 10 comments · Fixed by #7101
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-unnecessary Lint: Warn about unnecessary code T-middle Type: Probably requires verifiying types

Comments

@KamilaBorowska
Copy link
Contributor

filter_map is a more specific version of flat_map. It would be neat if Clippy recommended using filter_map instead of flat_map when the function in flat_map returns Option.

This is an example from documentation with filter_map replaced by flat_map.

fn main() {
    let a = ["1", "2", "lol"];

    let mut iter = a.iter().flat_map(|s| s.parse().ok());

    assert_eq!(iter.next(), Some(1));
    assert_eq!(iter.next(), Some(2));
    assert_eq!(iter.next(), None);
}

it would be more readable with filter_map instead of flat_map.

@oli-obk oli-obk added L-unnecessary Lint: Warn about unnecessary code good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints T-middle Type: Probably requires verifiying types labels Nov 21, 2017
@killercup
Copy link
Member

killercup commented Nov 23, 2017

Eh, quick question: Why not just use a.iter().flat_map(|s| s.parse());? Result::iter only yield the Ok value, just like Option::iter only yields a value when it's Some.

(Yes, I like to abuse flat_map and the Iterator impls on these types.)

(And here comes the usual E-hard suggestion: Write a lint that detects unnecessary method calls, like the .ok() here.)

@clarfonthey
Copy link
Contributor

IMO now that Try is stable, upstream should replace filter_map with a version that uses Try instead. That would allow use of Result as well as option.

@Manishearth
Copy link
Member

Manishearth commented Dec 2, 2017 via email

@clarfonthey
Copy link
Contributor

...right, that's correct. :/

@oli-obk
Copy link
Contributor

oli-obk commented Dec 2, 2017

You can't change the signatures of trait methods upstream

you can if it's generic and strictly allows more code

@Manishearth
Copy link
Member

Manishearth commented Dec 2, 2017 via email

@clarfonthey
Copy link
Contributor

I mean, they could always do a crater run to check. Although at the moment, isn't FilterMap literally coded as a façade over FlatMap, or am I wrong?

@Manishearth
Copy link
Member

Manishearth commented Dec 2, 2017 via email

@KamilaBorowska
Copy link
Contributor Author

KamilaBorowska commented Dec 3, 2017

FilterMap has a better implementation of size_hint than FlatMap as it knows that next can only provide 0 or 1 element and doesn't store currently processed iterator in it as it doesn't need to do that with Option - after getting a single element, Option iterator always keeps returning None.

@llogiq
Copy link
Contributor

llogiq commented Nov 5, 2018

One thing we could lint is manually constructing one-element-vec![..]s in flat_map, where using filter_map will do. Example: rust-lang/rust#55476

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-unnecessary Lint: Warn about unnecessary code T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants