-
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
Suggest replacing filter-for-some-and-unwrap with .flatten #6061
Comments
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. |
Shouldn't the recommendation itself be linted to use |
My (newbie) understanding is that |
I meant replacing the |
Ah, that makes sense. I wonder if maybe this example is a red herring, since you might chain 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. |
So on the one hand @mikerite is right, that But rather than implementing this as a new lint, I just would enhance the |
@flip1995 So you suggest to check for both @bbqbaron Are you still working on this? |
@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. |
Nope, I'm suggesting only linting |
What it does
Suggests replacing code that filters with
Option::is_some
and then mapsOption::unwrap
with.flatten()
which does the same thing due toOption
implementingIntoIterator
.Categories (optional)
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
Could be written as:
The text was updated successfully, but these errors were encountered: