Skip to content

Fix redundant closure call async false positive#17107

Open
lkirkwood wants to merge 2 commits into
rust-lang:masterfrom
lkirkwood:16232
Open

Fix redundant closure call async false positive#17107
lkirkwood wants to merge 2 commits into
rust-lang:masterfrom
lkirkwood:16232

Conversation

@lkirkwood

@lkirkwood lkirkwood commented May 28, 2026

Copy link
Copy Markdown

Previously, async closures containing early returns which were immediately called triggered the redundant_closure_call lint. Regular non-async closures with these early return statements did not trigger these lints.

Async closures of both kinds (async || {} and || async {}) trigger some desugaring which needs to be appropriately handled when looking for early return statements. This commit implements that logic.

fixes: #16232

changelog: [redundant_closure_call]: Fix false positive on async closures with early returns

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 28, 2026
@rustbot

rustbot commented May 28, 2026

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, ada4a, dswij, llogiq, samueltardieu

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add exemple where "use" appears in the suggestion? I think that only "move" does.

View changes since this review

Comment thread tests/ui/redundant_closure_call_early.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 4, 2026
@rustbot

rustbot commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@lkirkwood

lkirkwood commented Jun 5, 2026

Copy link
Copy Markdown
Author

Thanks for the review!

Could you add exemple where "use" appears in the suggestion? I think that only "move" does.

Yeah happy to remove it. I added it because its one of the possible "capture clauses" in the ast. I don't know what the policy is on supporting unstable features. The following code compiles on nightly:

#![allow(unused)]
#![feature(ergonomic_clones)]

fn maybe() -> Option<()> {
    Some(())
}

async fn test() {
    let async_result = (async use || {
        if maybe().is_none() {
            Some(0)
        } else {
            Some(42)
        }
    })()
    .await;
}

fn main() {}

But obviously the feature may never be stabilised so you could argue its dead code. Up to you.

@lkirkwood

Copy link
Copy Markdown
Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 5, 2026
@samueltardieu

Copy link
Copy Markdown
Member

But obviously the feature may never be stabilised so you could argue its dead code. Up to you.

It can be used right now even when unstable, so I'd prefer we keep it and add tests. If this is removed from the compiler, Clippy won't compile anymore unless the relevant code is removed as well, so we won't accumulate technical debt there.

@lkirkwood lkirkwood force-pushed the 16232 branch 3 times, most recently from 5020155 to ef50c1d Compare June 7, 2026 02:17
@lkirkwood

Copy link
Copy Markdown
Author

Ok, done! I elected for #![allow(incomplete_features)] instead of expect so it wont need changing if the feature becomes complete, but happy to change it if that's the wrong approach. Thanks for your patience on this.

@rustbot ready

@samueltardieu

Copy link
Copy Markdown
Member

Ok, done! I elected for #![allow(incomplete_features)] instead of expect so it wont need changing if the feature becomes complete, but happy to change it if that's the wrong approach. Thanks for your patience on this.

On the contrary, I would prefer that we are forced to change it when the feature becomes complete to not keep a useless #![allow] in our source code.

lkirkwood added 2 commits June 8, 2026 17:13
Previously, async closures containing early returns which were
immediately called triggered the `redundant_closure_call` lint.
Regular non-async closures with these early return statements did not
trigger these lints.

Async closures of both kinds (`async || {}` and `|| async {}`) trigger
some desugaring which needs to be appropriately handled when looking for
early return statements. This commit implements that logic.
Also remove some redundant allows and make expects more granular instead
of applying to the whole module.
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.

False positive clippy::redundant_closure_call when using async closures as async try block

3 participants