Skip to content

Additional suggestion for returning async closure Future is wrong #150701

@ghost

Description

async fn f(c: impl Future<Output = ()>) {
    c.await
}
#[tokio::main]
async fn main() {
    f(async || {}()).await
}

Cargo clippy said:

error[E0618]: expected function, found `()`
 --> src/main.rs:6:16
  |
6 |     f(async || {}()).await
  |                ^^--                                   |                |
  |                call expression requires function
  |                                                     help: if you meant to create this closure and immediately call it, surround the closure with parentheses          |
6 |     f((async || {})()).await
  |       +           +

error[E0277]: `{async closure@src/main.rs:6:7: 6:15}` is not a future
 --> src/main.rs:6:7
  |
6 |     f(async || {}()).await
  |     - ^^^^^^^^^^^^^ `{async closure@src/main.rs:6:7: 6:15}` is not a future
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `std::future::Future` is not implemented for `{async closure@src/main.rs:6:7: 6:15}`
note: required by a bound in `f`
 --> src/main.rs:1:20
  |
1 | async fn f(c: impl Future<Output = ()>) {
  |                    ^^^^^^^^^^^^^^^^^^^ required by this bound in `f`
help: use parentheses to call this closure
  |
6 |     f(async || {}()()).await
  |                    ++

error[E0277]: `{async closure@src/main.rs:6:7: 6:15}` is not a future
 --> src/main.rs:6:5
  |
6 |     f(async || {}()).await
  |     ^^--------^^^^^^
  |     | |
  |     | consider calling this closure
  |     `{async closure@src/main.rs:6:7: 6:15}` is not a future
  |
  = help: the trait `std::future::Future` is not implemented for `{async closure@src/main.rs:6:7: 6:15}`
note: required by a bound in `f`
 --> src/main.rs:1:20
  |
1 | async fn f(c: impl Future<Output = ()>) {
  |                    ^^^^^^^^^^^^^^^^^^^ required by this bound in `f`

error[E0277]: `{async closure@src/main.rs:6:7: 6:15}` is not a future
 --> src/main.rs:6:22
  |
6 |     f(async || {}()).await
  |       --------       ^^^^^ `{async closure@src/main.rs:6:7: 6:15}` is not a future
  |       |
  |       consider calling this closure
  |
  = help: the trait `std::future::Future` is not implemented for `{async closure@src/main.rs:6:7: 6:15}`
note: required by a bound in `f`
 --> src/main.rs:1:20
  |
1 | async fn f(c: impl Future<Output = ()>) {
  |                    ^^^^^^^^^^^^^^^^^^^ required by this bound in `f`

The 2nd suggestion is wrong, according to the 2nd suggestion to add parentheses after async closure can't fix the error. And the 1st suggestion will change to

error[E0618]: expected function, found `()`
 --> src/main.rs:6:16
  |
6 |     f(async || {}()()).await                          |                ^^--
  |                |
  |                call expression requires function

Only surrounding async closure

async fn f(c: impl Future<Output = ()>) {
    c.await
}
#[tokio::main]
async fn main() {
    f((async || {})()).await
}

fixes the error. But after fixing the error, clippy suggests

warning: try not to call a closure in the expression where it is declared                                        --> src/main.rs:6:7
  |                                                     6 |     f((async || {})()).await
  |       ^^^^^^^^^^^^^^^ help: try doing something like: `async {}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-closures`async || {}`A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions