-
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
[iter_overeager_cloned]: detect .cloned().map() and .cloned().for_each() #11319
Conversation
r? @llogiq (rustbot has picked a reviewer for you, use r? to override) |
e277f63
to
dd9d4fa
Compare
3029f37
to
87da7b3
Compare
if let rustc_hir::ExprKind::Closure(closure) = expr.kind | ||
&& let body@ Body {params: [p], .. } = cx.tcx.hir().body(closure.body) { | ||
match p.pat.kind { | ||
rustc_hir::PatKind::Binding(BindingAnnotation(_, Mutability::Not), _, _, _) => { |
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.
We should probably expand this to other parameters as well, by using pat.walk
and checking for any Binding
s. If they're all used, we could lint; though I'm not sure how to handle this, maybe only if one is used?
The reason why we can't use each_binding
is that it gives us the HirId
of the Pat
, not the binding, so we technically can but it's not pretty
Is there any reason we should lint only Binding
that I'm not aware of?
4d755c6
to
4802375
Compare
☔ The latest upstream changes (presumably #11289) made this pull request unmergeable. Please resolve the merge conflicts. |
e1ebe56
to
52c50a5
Compare
r? @blyxyas |
@@ -51,8 +59,46 @@ pub(super) fn check<'tcx>( | |||
return; | |||
} | |||
|
|||
if let Op::NeedlessMove(_, expr) = op { | |||
let rustc_hir::ExprKind::Closure(closure) = expr.kind else { return } ; | |||
let body@ Body {params: [p], .. } = cx.tcx.hir().body(closure.body) else { return }; |
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 think this destructuring could mean this only lints when the body has only one param.
Is this intended? If so, some optimization could be done in that walk method
.
Maybe I'm wrong, am not with an IDE at the moment to corroborate this pattern destructuring.
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.
map
and for_each
require a closure of only one param
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.
Just that mini-nit of changing the variable name and I think this is ready, thanks ❤️!
…ach() key idea: for `f` in `.map(f)` and `.for_each(f)`: 1. `f` must be a closure with one parameter 2. don't lint if mutable paramter in clsure `f`: `|mut x| ...` 3. don't lint if parameter is moved
@bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
changelog: [
iter_overeager_cloned
]key idea:
for
f
in.map(f)
and.for_each(f)
:f
must be closuref
:|mut x| ...