-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make the filter_map
lint more specific
#3188
Comments
Yeah I also thought that the new code wasn't as readable as the original code. This would be a good change! If this is fixed, the changes done in #3186 should also be reverted. |
I'll give this issue a shot in #3461 |
This example doesn't seem to fit in with the other two unless |
I think lints from |
I agree with @camsteffen, in my project, this lint only yielded false positives. I'd even go as far as to say that it should be removed, even from pedantic, until it is specialized. |
The intent of the
filter_map
method is to unwrap element values and skipNone
s when iterating over elements that areOption
s. It's not a universal shorthand forfilter
followed by a call tomap
. Thefilter_map
documentation indicates this.The current implementation of the
fliter_map
lint triggers on every call tofilter
followed by a call tomap
and basically notes that the suggestion can make the code more complicated as a known issue. It encourages changes like this (from #3186):to
I find the final code is less readable and more complicated than the code at the start. Although, there is one less function call, there is now an
if
statement and extra code for constructing an option.Let's change the lint to only trigger in the following conditions. In the table below
option_expr
is an expression with typeOption<_>
andresult_expr
is an expression with typeResult<_, _>
.filter(|x| option_expr.is_some()).map(|x| x.unwrap())
filter_map(|x| option_expr)
filter(|x| option_expr.is_some()).flat_map(|x| x)
filter_map(|x| option_expr)
filter(|x| result_expr.is_ok()).map(|x| x.unwrap())
filter_map(|x| result_expr.ok())
The text was updated successfully, but these errors were encountered: