-
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
Allow filter_map_identity
when the closure is typed
#12562
Conversation
This extends the `filter_map_identity` lint to support typed closures. For untyped closures, we know that the program compiles, and therefore we can safely suggest using flatten. For typed closures, they may participate in type resolution. In this case we use `Applicability::MaybeIncorrect`. Details: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60filter_map_identity.60.20lint.20when.20closures.20are.20typed.3F
Is there a reason that we'd only want to do this for filter_map_identity, and not for all callers of But also, are we sure that type annotations can always be "reasonably" specified elsewhere when the closure is removed? I'm having a hard time coming up with a practical example where you can't. I think there's usually always somewhere where the type could be specified to make type inference happy. E.g. fn accepts_iter(_: impl Iterator) {}
accepts_iter(iter::empty().map(|v: i32| v)); Removing the #9122 is a more practical FP where these "untyped" functions were introduced, but even here one can write |
I was thinking we could introduce this change gradually, starting from less error prone cases such as this one, and slowly moving into more common ones while accounting for issues that can occur such as the one you've presented.
I have a hard time coming up with situations where this is exceptionally hard to be honest. Even in the tests that I have added where I'd have expected the identity function to actually help with inference, rustc flatout refused to compile it, though that is certainly different to the For |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me with the nit fixed, @Centri3 does this make sense to you as well?
return Some(Applicability::MachineApplicable); | ||
} | ||
if is_expr_identity_function(cx, expr) { | ||
return Some(Applicability::MaybeIncorrect); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be Unspecified
(or HasPlaceholders
).
MaybeIncorrect
suggestions should still compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've interpreted "valid Rust code" as it parsing correctly, everything else can fail I'm pretty sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustfix somewhere has an issue to rename these variants to make the difference clearer. I think it's mostly stuck on finding better names:
I'm not sure if special casing fn calls as the <std::iter::Empty<Option<_>> as Default>::default().flat_map(|v: Option<i32>| v)
// ^ i32 needs to go here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really think we should pass this as is without some kind of backwards heuristic to type_certainty
. I have no problems with the code itself, but if possible we should extend type_certainty
to either look "inwards" (as it does currently), or "outwards".
I've seen FPs in the past caused by this, MREs will probably look contrived but this definitely can (and will) come up often in practice.
Let's at least run lintcheck on a lot of popular crates before we do merge as is, it'd be nice if it can select a lot of random ones in the top 1000
fwiw, running I think we should first extend |
you can run |
I didn't know about that, though it still would be nice if it could choose them random (otherwise we're checking the same crates everytime) |
What I did:
Result:
((empty)) Thus no errors were introduced from this. Should I increase the number to say top 500? |
Top 200 is probably enough for now. |
Is the new applicability okay? |
Yeah, looks fine to me. I'd say its ready to go, but I'll wait a bit to see if others have something to say |
The applicability also looks good to me(I haven't looked at anything else) |
Lets move this forward, thanks! LGTM @bors r+ |
Allow `filter_map_identity` when the closure is typed This extends the `filter_map_identity` lint to support typed closures. For untyped closures, we know that the program compiles, and therefore we can safely suggest using flatten. For typed closures, they may participate in type resolution. In this case we use `Applicability::MaybeIncorrect`. Details: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60filter_map_identity.60.20lint.20when.20closures.20are.20typed.3F changelog: `filter_map_identity` will now suggest using flatten for typed closures. r? `@y21` && `@Centri3`
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
👀 Test was successful, but fast-forwarding failed: 422 1 review requesting changes and 1 approving review by reviewers with write access. |
@bors retry |
Allow `filter_map_identity` when the closure is typed This extends the `filter_map_identity` lint to support typed closures. For untyped closures, we know that the program compiles, and therefore we can safely suggest using flatten. For typed closures, they may participate in type resolution. In this case we use `Applicability::MaybeIncorrect`. Details: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60filter_map_identity.60.20lint.20when.20closures.20are.20typed.3F changelog: `filter_map_identity` will now suggest using flatten for typed closures. r? `@y21` && `@Centri3`
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
👀 Test was successful, but fast-forwarding failed: 422 1 review requesting changes and 1 approving review by reviewers with write access. |
Third time's the charm? @bors retry |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
This extends the
filter_map_identity
lint to support typed closures.For untyped closures, we know that the program compiles, and therefore we can safely suggest using flatten.
For typed closures, they may participate in type resolution. In this case we use
Applicability::MaybeIncorrect
.Details:
https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60filter_map_identity.60.20lint.20when.20closures.20are.20typed.3F
changelog:
filter_map_identity
will now suggest using flatten for typed closures.r? @y21 && @Centri3