Skip to content

[useless_asref]: check that the clone receiver is the parameter #12136

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

Merged
merged 2 commits into from
Jan 15, 2024

Conversation

y21
Copy link
Member

@y21 y21 commented Jan 12, 2024

Fixes #12135

There was no check for the receiver of the clone call in the map closure. This makes sure that it's a path to the parameter.

changelog: [useless_asref]: check that the clone receiver is the closure parameter

@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2024

r? @Jarcho

(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 Jan 12, 2024
@alex
Copy link
Member

alex commented Jan 12, 2024

Thanks for jumping on this so quickly.

@GuillaumeGomez
Copy link
Member

Thanks for fixing it!

hir::Param {
pat:
hir::Pat {
kind: hir::PatKind::Binding(_, local_id, ..),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to check for Ref.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean |&x| patterns (PatKind::Ref)? Do you mean that we should also lint on .as_ref().map(|&x| x.clone())? Or do you mean that we shouldn't lint when ref is on the binding?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure exactly what this Ref pattern is checking exactly. I looked at map_clone (which lint for something similar) to check how they did and they handle both so I think you should as well here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looking more into this, (correct me if I'm wrong) it looks like map_clone used to lint on .as_ref().map(|&x| x) because it's the same as .copied(), but as of recently doesn't anymore, because of a should_run_lint check that lets useless_asref check it.
But useless_asref only seems to handle .clone() calls, so there's nothing to catch that case anymore.

I suppose we could extend the logic in should_run_lint to only return false when clone is called in the closure, so that map_clone can continue linting on .map(|x| *x) and .map(|&x| x). We won't need a Ref check here, then (because it's already handled elsewhere).

This is getting pretty close to duplicating the logic from useless_asref inside should_run_lint though. I'm wondering if we could just move the clone checking that's currently in useless_asref to map_clone so they don't need this sort of coordination?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh but you still can trick the lint:

let x = Some(String::new());
let x = x.as_ref();
x.map(|&x| x.clone()); // or whatever

If this is duplicating the logic from map_clone, then might be worth having some code in common instead. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in be5707c . Is that what you had in mind? (This also moved that let else from before to an if let guard because I think it looks nicer and less indentation, shouldn't change anything functionally)

Copy link
Member Author

@y21 y21 Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened a new issue for the other one: #12142, so this can't be forgotten :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I missed something, you still only handle Binding and still not Ref (Ref is used here in map_clone).

Copy link
Member Author

@y21 y21 Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&& let hir::PatKind::Binding(_, local_id, ..) = strip_pat_refs(param.pat).kind =>

The call to strip_pat_refs removes any ref patterns, so |&&x|, |&x|, |x| will all be handled the same way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. Nicely done!

@Jarcho
Copy link
Contributor

Jarcho commented Jan 15, 2024

Thank you. @bors r+

@bors
Copy link
Contributor

bors commented Jan 15, 2024

📌 Commit be5707c has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 15, 2024

⌛ Testing commit be5707c with merge ec4d027...

bors added a commit that referenced this pull request Jan 15, 2024
[`useless_asref`]: check that the clone receiver is the parameter

Fixes #12135

There was no check for the receiver of the `clone` call in the map closure. This makes sure that it's a path to the parameter.

changelog: [`useless_asref`]: check that the clone receiver is the closure parameter
@bors
Copy link
Contributor

bors commented Jan 15, 2024

💔 Test failed - checks-action_test

@Jarcho
Copy link
Contributor

Jarcho commented Jan 15, 2024

@bors retry

bors added a commit that referenced this pull request Jan 15, 2024
[`useless_asref`]: check that the clone receiver is the parameter

Fixes #12135

There was no check for the receiver of the `clone` call in the map closure. This makes sure that it's a path to the parameter.

changelog: [`useless_asref`]: check that the clone receiver is the closure parameter
@bors
Copy link
Contributor

bors commented Jan 15, 2024

⌛ Testing commit be5707c with merge 96b67bc...

@bors
Copy link
Contributor

bors commented Jan 15, 2024

💔 Test failed - checks-action_test

@Jarcho
Copy link
Contributor

Jarcho commented Jan 15, 2024

@bors retry

Come on macos, you can do it.

@bors
Copy link
Contributor

bors commented Jan 15, 2024

⌛ Testing commit be5707c with merge 692f53f...

@bors
Copy link
Contributor

bors commented Jan 15, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho
Pushing 692f53f to master...

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.

clippy::useless_asref warns in cases where map callback performs a method call before a clone
6 participants