Skip to content
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

Merged
merged 1 commit into from
Aug 19, 2023

Conversation

lengyijun
Copy link
Contributor

@lengyijun lengyijun commented Aug 11, 2023

changelog: [iter_overeager_cloned]

key idea:
for f in .map(f) and .for_each(f):

  1. f must be closure
  2. don't lint if mutable paramter in clsure f: |mut x| ...
  3. don't lint if parameter is moved
  4. maybe incorrect

@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2023

r? @llogiq

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 11, 2023
@lengyijun lengyijun marked this pull request as draft August 11, 2023 01:22
@lengyijun lengyijun force-pushed the map_foreach branch 2 times, most recently from e277f63 to dd9d4fa Compare August 11, 2023 01:31
@lengyijun lengyijun force-pushed the map_foreach branch 3 times, most recently from 3029f37 to 87da7b3 Compare August 11, 2023 12:56
clippy_lints/src/methods/iter_overeager_cloned.rs Outdated Show resolved Hide resolved
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), _, _, _) => {
Copy link
Member

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 Bindings. 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?

@lengyijun lengyijun force-pushed the map_foreach branch 2 times, most recently from 4d755c6 to 4802375 Compare August 12, 2023 11:25
@bors
Copy link
Collaborator

bors commented Aug 14, 2023

☔ The latest upstream changes (presumably #11289) made this pull request unmergeable. Please resolve the merge conflicts.

@lengyijun lengyijun force-pushed the map_foreach branch 5 times, most recently from e1ebe56 to 52c50a5 Compare August 15, 2023 03:50
@lengyijun lengyijun changed the title [Draft] [iter_overeager_cloned]: detect .cloned().map() and .cloned().for_each() [iter_overeager_cloned]: detect .cloned().map() and .cloned().for_each() Aug 15, 2023
@lengyijun lengyijun marked this pull request as ready for review August 15, 2023 03:53
@lengyijun
Copy link
Contributor Author

r? @blyxyas

@rustbot rustbot assigned blyxyas and unassigned llogiq Aug 15, 2023
clippy_lints/src/methods/iter_overeager_cloned.rs Outdated Show resolved Hide resolved
tests/ui/iter_overeager_cloned.stderr Show resolved Hide resolved
@@ -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 };
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

@blyxyas blyxyas left a 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
@blyxyas
Copy link
Member

blyxyas commented Aug 19, 2023

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 19, 2023

📌 Commit e440065 has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Aug 19, 2023

⌛ Testing commit e440065 with merge 7408f1d...

@bors
Copy link
Collaborator

bors commented Aug 19, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas
Pushing 7408f1d to master...

@bors bors merged commit 7408f1d into rust-lang:master Aug 19, 2023
5 checks passed
@lengyijun lengyijun deleted the map_foreach branch October 27, 2023 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants