Skip to content

closure-returning-async-block suggestion causes does not live long enough error #142145

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn closure-returning-async-block

this code:

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(|| async move { copyable })
}

caused the following diagnostics:

    Checking _snippet_3 v0.1.0 (/tmp/icemaker_global_tempdir.VQhomPU3g4RT/icemaker_clippyfix_tempdir.e9LHZQ3h7hng/_snippet_3)
warning: closure returning async block can be made into an async closure
  --> src/main.rs:10:14
   |
10 |     needs_fn(|| async move { copyable })
   |              ^^ ---------- this async block can be removed, and the closure can be turned into an async closure
   |
   = note: requested on the command line with `--force-warn closure-returning-async-block`
help: turn this into an async closure
   |
10 -     needs_fn(|| async move { copyable })
10 +     needs_fn(async || { copyable })
   |

warning: `_snippet_3` (bin "_snippet_3") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s

However after applying these diagnostics, the resulting code:

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(async || { copyable })
}

no longer compiled:

    Checking _snippet_3 v0.1.0 (/tmp/icemaker_global_tempdir.VQhomPU3g4RT/icemaker_clippyfix_tempdir.e9LHZQ3h7hng/_snippet_3)
error[E0597]: `copyable` does not live long enough
  --> src/main.rs:10:14
   |
10 |     needs_fn(async || { copyable })
   |     ---------^^^^^^^^^^^^^^^^^^^^^-
   |     |        |
   |     |        borrowed value does not live long enough
   |     opaque type requires that `copyable` is borrowed for `'static`
11 | }
   | - `copyable` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `_snippet_3` (bin "_snippet_3") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_3` (bin "_snippet_3" test) due to 1 previous error

Version:

rustc 1.89.0-nightly (44f415c1d 2025-06-06)
binary: rustc
commit-hash: 44f415c1d617ebc7b931a243b7b321ef8a6ca47c
commit-date: 2025-06-06
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-async-closures`async || {}`A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions