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

chore: multipart suggestions for let_unit_value lint #13754

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

scottgerring
Copy link
Contributor

This should address #13099 for the let_unit test.

changelog: [let_unit]: Updated let_unit to use multipart_suggestions where appropriate

@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2024

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 29, 2024
@scottgerring
Copy link
Contributor Author

Flip1995 has the context of these, so if they have time:

r? flip1995

(But this is still a draft!)

@rustbot rustbot assigned flip1995 and unassigned llogiq Nov 29, 2024
@scottgerring scottgerring reopened this Dec 2, 2024
@scottgerring scottgerring marked this pull request as ready for review December 2, 2024 14:33
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Great work! Just 2 codestyle comments.

Please also rebase your PR on top of the current master. We changed our CI recently and GitHub isn't good at picking this up on older PRs.

clippy_lints/src/unit_types/let_unit_value.rs Outdated Show resolved Hide resolved
clippy_lints/src/unit_types/let_unit_value.rs Outdated Show resolved Hide resolved
@scottgerring scottgerring force-pushed the chore/fix-let-unit-test branch 2 times, most recently from 36b6b3f to 36e6a98 Compare December 3, 2024 13:18
@scottgerring
Copy link
Contributor Author

scottgerring commented Dec 3, 2024

Hey @flip1995 thanks for the review! I rebased and addressed the resulting dogfooding lints and your own comments, so there's a bit of a delta on top of what you reviewed. Should be good to go, though, I reckon.

I am assuming you squash onto master, but if not, I can clean my commit history up in future 🤣

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks! We don't squash on merge. So, yes, please clean up the commit history :)

}

// Emit appropriate diagnostic based on whether there are usages of the let binding
match suggestions.len().cmp(&1) {
Copy link
Member

@flip1995 flip1995 Dec 3, 2024

Choose a reason for hiding this comment

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

That's a bit of overkill 😅 I would suggest to change this to:

if !suggestions.is_empty() {
    let message = if suggestions.len() == 1 {
        "omit the `let` binding"
    } else {
        "omit the `let` binding and replace variable usages with `()`"
    };
    diag.multipart_suggestion(...);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's way less awful. Let me clean it up 👍

@scottgerring scottgerring force-pushed the chore/fix-let-unit-test branch from db9276a to a29525a Compare December 3, 2024 14:14
@scottgerring scottgerring force-pushed the chore/fix-let-unit-test branch from a29525a to ec24388 Compare December 3, 2024 14:15
@scottgerring
Copy link
Contributor Author

@flip1995 that should be it! 🙏

@scottgerring
Copy link
Contributor Author

Hey @flip1995 , do you have a chance to have a quick look at this? We're very close to finishing the whole issue!

@flip1995
Copy link
Member

Sorry for not replying. I won't get to review any Clippy PRs until around christmas... I'll reassign

r? clippy

&& let Some(body_id) = cx.enclosing_body.as_ref()
&& let body = cx.tcx.hir().body(*body_id)
&& is_local_used(cx, body, binding_hir_id)
Copy link
Member

Choose a reason for hiding this comment

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

This line is completely removed, is this intended?

Copy link
Member

Choose a reason for hiding this comment

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

And sorry for the late review, I've opened this PR maybe 5 times and never got around to clicking that "Submit review" button

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @blyxyas no worries!

I think it is unnecessary now; this part here collects usages:

// Collect variable usages
let mut visitor = UnitVariableCollector::new(binding_hir_id);
walk_body(&mut visitor, body);

... and we then emit a lint based on whether or not we had any:

if !suggestions.is_empty() {
let message = if suggestions.len() == 1 {
"omit the `let` binding"
} else {
"omit the `let` binding and replace variable usages with `()`"
};
diag.multipart_suggestion(message, suggestions, Applicability::MachineApplicable);

It could be added back as a sort of "performance optimization guard" to remove the need to call walk_body, assuming that is_local_used is quicker, but it doesn't change the behaviour (I also double checked this!)

Copy link
Member

@blyxyas blyxyas Dec 23, 2024

Choose a reason for hiding this comment

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

I've been all morning trying to measure this but it's in a very awkward spot to get a hold of how many heavy-lifting that function does.

Being that in the case that the body is visited is already in the linting-case, let's just ignore it.

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! ❤️

&& let Some(body_id) = cx.enclosing_body.as_ref()
&& let body = cx.tcx.hir().body(*body_id)
&& is_local_used(cx, body, binding_hir_id)
Copy link
Member

@blyxyas blyxyas Dec 23, 2024

Choose a reason for hiding this comment

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

I've been all morning trying to measure this but it's in a very awkward spot to get a hold of how many heavy-lifting that function does.

Being that in the case that the body is visited is already in the linting-case, let's just ignore it.

@blyxyas blyxyas added this pull request to the merge queue Dec 23, 2024
Merged via the queue into rust-lang:master with commit 988042e Dec 23, 2024
9 checks passed
@scottgerring scottgerring deleted the chore/fix-let-unit-test branch December 23, 2024 15:58
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.

5 participants