-
Couldn't load subscription status.
- Fork 1.8k
Description
What it does
Checks for usage of filter(Option::is_some) or filter(Result::is_ok) on an Iterator.
This is more succinctly written with just flatten().
Careful: The suggestion must be MaybeIncorrect, as this changes the type of the iterator from Item = Option<T> to Item = T.
good-first-issue instructions: This lint should be quite easy to add as another clippy_lints/src/methods/ lint. All of the parts required to check for this should already be there and can be reused.
Advantage
- Shorter code
- Potentially more readable code in following methods in the iterator chain:
.filter(Option::is_some).map(|x| /*some code*/ x.unwrap() /*some code*/) // becomes .flatten().map(|x| /*some code*/ x /*some code*/)
Drawbacks
Changes the type of the iterator chain. This will lead to more refactorings required down the chain. But I think in most cases this will benefit the code complexity overall.
Example
v.into_iter().filter(Option::is_some);
v.into_iter().filter(Result::is_ok);Could be written as:
v.into_iter().flatten();