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

[map_unwrap_or]: don't lint when referenced variable is moved #10919

Merged
merged 1 commit into from Jun 14, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Jun 9, 2023

Fixes #10579.

The previous way of checking if changing map(f).unwrap_or(a) to map_or(a, f) is safe had a flaw when the argument to unwrap_or moves a binding and the map closure references that binding in some way.

It used to simply check if any of the identifiers in the unwrap_or argument are referenced in the map closure, but it didn't consider the case where the moved binding is referred to through references, for example:

let x = vec![1, 2];
let x_ref = &x;
Some(()).map(|_| x_ref.clone()).unwrap_or(x);

This compiles as is, but we cannot change it to map_or. This lint however did suggest changing it, because the simple way of checking if x is referenced anywhere in the map closure fails here. The safest thing to do here imo (what this PR does) is check if the moved value x is referenced anywhere in the body (before the unwrap_or call). One can always create a reference to the value and smuggle them into the closure, without actually referring to x. The original, linked issue shows another one such example:

    let x = vec![1,2,3,0];
    let y = x.strip_suffix(&[0]).map(|s| s.to_vec()).unwrap_or(x);

x.strip_suffix(&[0]) creates a reference to x that is available through s inside of the map closure, so we can't change it to map_or.

changelog: [map_unwrap_or]: don't lint when referenced variable is moved

@rustbot
Copy link
Collaborator

rustbot commented Jun 9, 2023

r? @xFrednet

(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 Jun 9, 2023
@xFrednet
Copy link
Member

Hey @blyxyas, this looks like another good PR if you want to review it. If you're busy rn, I'm also happy to do the entire review :)

@blyxyas
Copy link
Member

blyxyas commented Jun 10, 2023

If you're busy rn

You understimate me, I may be just out of the hospital, but gimme that bad boi! Clippy 👏 comes 👏 first
(Don't worry, I'm good)

@xFrednet
Copy link
Member

xFrednet commented Jun 10, 2023

Clippy 👏 comes 👏 first

Just making sure, I basically no-lifed Clippy at the start, and only later learned that you need a break every once in a while. That being said, I've no-lifing another project rn again, so it's all back to normal xD

@blyxyas
Copy link
Member

blyxyas commented Jun 10, 2023

@xFrednet I'm sorry but I don't think I can do the full review.
I actually went to the hospital for overdosing on anxiety meds, I've been out for like 4 hours and seeing code rn feels like reading heroglyphics.

If this is urgent, could you review it? If not, I think we'll have to wait a day or two until I can come back to seeing letters and colors in my screen.

@xFrednet
Copy link
Member

@blyxyas No worries at all, rest 👏 comes 👏 first! Generally speaking, it's totally fine, to let PRs wait on a review for a bit. I usually try to get to every one within 5 days, but even a week is not a problem. If you know that it'll take time, it's always good to let the author know, but there are no real time-critical issues in Clippy. The changes would need to wait until the next sync anyways.

I've already reviewed some PRs today, and will look at this over the next few days. If you want to pick up the review, when you feel better, you're very welcome. I appreciate you being honest! You can delete that part from your message if you feel it's too personal. Anxiety can suck so much..., if you have any questions, need reassurance or anything, you can always reach out on Zulip as well!


@y21 Sorry, for hijacking the PR comments for this discussion. You'll get a review in the next couple of days :D

@y21
Copy link
Member Author

y21 commented Jun 10, 2023

Sorry, for hijacking the PR comments for this discussion. You'll get a review in the next couple of days :D

it's all good, i won't be home this weekend anyways, so take your time :)

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.

LGTM! Thanks, pretty hard issue to solve ❤️

cc @xFrednet

@xFrednet
Copy link
Member

Looks good to me! Thank you for the PR :D

And the review @blyxyas

@bors r=blyxyas,xFrednet

@bors
Copy link
Collaborator

bors commented Jun 14, 2023

📌 Commit 9d58a42 has been approved by blyxyas,xFrednet

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 14, 2023

⌛ Testing commit 9d58a42 with merge a59236f...

@bors
Copy link
Collaborator

bors commented Jun 14, 2023

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

@bors bors merged commit a59236f into rust-lang:master Jun 14, 2023
5 checks passed
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.

map_unwrap_or incorrectly suggested due to ownership issues
5 participants