Skip to content

[redundant_closure_call]: handle nested closures #10930

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 3 commits into from
Jun 19, 2023
Merged

Conversation

y21
Copy link
Member

@y21 y21 commented Jun 11, 2023

Fixes #9956.

This ended up being a much larger change than I'd thought, and I ended up having to pretty much rewrite it as a late lint pass, because it needs access to certain things that I don't think are available in early lint passes (e.g. getting the parent expr). I think this'll be required to fi-x #10922 anyway, so this is probably fine.
(edit: had to write "fi-x" because "fix" makes github think that this PR fixes it, which it doesn't 😅 )

Previously, it would suggest changing (|| || 42)()() to || 42(), which is a type error (it needs parens: (|| 42)()). In my opinion, though, the suggested fix should have really been 42, so that's what this PR changes.

changelog: [redundant_closure_call]: handle nested closures and rewrite as a late lint pass

@rustbot
Copy link
Collaborator

rustbot commented Jun 11, 2023

r? @Manishearth

(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 11, 2023
@Manishearth
Copy link
Member

r? @blyxyas and @xFrednet

(this seems like an interesting PR to review)

@rustbot
Copy link
Collaborator

rustbot commented Jun 12, 2023

Failed to set assignee to blyxyas: invalid assignee

Note: Only org members, users with write permissions, or people who have commented on the PR may be assigned.

@blyxyas
Copy link
Member

blyxyas commented Jun 12, 2023

Yep, will review it in a few hours 👍

@blyxyas
Copy link
Member

blyxyas commented Jun 12, 2023

I haven't reviewed this yet. But I find that... maybe... Instead of reworking the lint to be a late one. We could just add the parenthesis and run the lint logic again after the fix? I'm not sure if that's possible. Just a thought.

If changing it to late will fix another issue, I guess it's still a worth it conversion.

@xFrednet
Copy link
Member

I haven't reviewed this yet. But I find that... maybe... Instead of reworking the lint to be a late one. We could just add the parenthesis and run the lint logic again after the fix?

We might be able to do that. It's okay, if the suggestion of a lint, triggers a different lint (Even though it should be avoided, if that's reasonable). Though, late lints are generally speaking preferable. Clippy has more utilities for them, they provide important type information etc. It's not worth going back and rewriting all early lints, but if we have a late lint implementation, that passes all tests, I'd say that's preferable.

This can, of course, depend on the lint, but if the logic is similar, I think it would be better to do this refactoring :)

@y21
Copy link
Member Author

y21 commented Jun 13, 2023

I originally had it suggest just wrapping it in parens, but I found it sad that it suggest something that is going to be linted again by itself (e.g. rewrite (|| || 42)() as (|| 42)(), which still has unnecessary closures), so I thought it'd be nice if it could do this in one go. Now looking back, I'm not sure if this is really an issue..
And removing all nested closures requires (at least, AFAIK) a late lint pass because there's a few things involved, but most importantly, for an expression like (|| || || 42)()()(), it should only consider the innermost call (and ignore the others, so as to not lint multiple times with the same suggestion), and then walk up the parent chain to figure out how many calls there are, so we can get the whole span and also to know how many closures we are allowed to remove. Getting the parent expression is not possible in an early lint pass, right? The TyCtxt and almost all the utils seem to be based entirely on HIR.

But, if this logic is unnecessary, I suppose I could also change it back to just suggesting parens.

@xFrednet
Copy link
Member

Getting the parent expression is not possible in an early lint pass, right?

Not really, early lint passes can only go down the AST and not up.

But, if this logic is unnecessary, I suppose I could also change it back to just suggesting parens.

With that comment, I was more referencing a potential case, where the reimplementation is 2x the original size with no benefit. This is not the case here, so don't worry. I think ranges are the few things which are easier to handle on the AST level. Almost everything else is better during a late lint pass.

@blyxyas
Copy link
Member

blyxyas commented Jun 13, 2023

I find this use case very funny. If an user writes (|||| 42)() they should attain to the consequences.

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.

In general, this is pretty good (just some very minor nits). Thanks! ❤️

cc @xFrednet

@xFrednet
Copy link
Member

LGTM to me as well. Thank you for the PR.

Dear bors, I humbly ask you to merge this pull request into master, in the name of princess @blyxyas and me as her shadow reviewer ✨

@bors
Copy link
Contributor

bors commented Jun 19, 2023

📌 Commit 3fe2478 has been approved by blyxyas,xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jun 19, 2023

⌛ Testing commit 3fe2478 with merge 4487570...

bors added a commit that referenced this pull request Jun 19, 2023
[`redundant_closure_call`]: handle nested closures

Fixes #9956.

This ended up being a much larger change than I'd thought, and I ended up having to pretty much rewrite it as a late lint pass, because it needs access to certain things that I don't think are available in early lint passes (e.g. getting the parent expr). I think this'll be required to fi-x #10922 anyway, so this is probably fine.
(edit: had to write "fi-x" because "fix" makes github think that this PR fixes it, which it doesn't 😅 )

Previously, it would suggest changing `(|| || 42)()()` to `|| 42()`, which is a type error (it needs parens: `(|| 42)()`). In my opinion, though, the suggested fix should have really been `42`, so that's what this PR changes.

changelog: [`redundant_closure_call`]: handle nested closures and rewrite as a late lint pass
@bors
Copy link
Contributor

bors commented Jun 19, 2023

💔 Test failed - checks-action_test

@y21
Copy link
Member Author

y21 commented Jun 19, 2023

Downloading artifact 'target' to: '/home/runner/work/rust-clippy/rust-clippy/target'
Error: The HTTP request timed out after 00:01:40.
Error: Exit code 1 returned from process: file name '/home/runner/runners/2.305.0/bin/Runner.PluginHost', arguments 'action "GitHub.Runner.Plugins.Artifact.DownloadArtifact, Runner.Plugins"'.

what happened there?

@xFrednet
Copy link
Member

xFrednet commented Jun 19, 2023

I'm guessing just some CI funkiness?

Bors, please, we even asked nicely in the last comment

@xFrednet xFrednet closed this Jun 19, 2023
@xFrednet xFrednet reopened this Jun 19, 2023
@xFrednet
Copy link
Member

Woops, wrong button 😅

Now let's try this properly again:

@bors retry

@bors
Copy link
Contributor

bors commented Jun 19, 2023

⌛ Testing commit 3fe2478 with merge 5b60388...

@bors
Copy link
Contributor

bors commented Jun 19, 2023

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

@bors bors merged commit 5b60388 into rust-lang:master Jun 19, 2023
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.

redundant_closure_call breaks code
6 participants