`closure-returning-async-block` suggestion causes `does not live long enough` error
Open
Nobody has claimed this yet.
A-async-await
A-async-closures
A-diagnostics
C-bug
D-invalid-suggestion
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied src/main.rs example with --force-warn closure-returning-async-block and compare the original and suggested forms. Investigate the closure-returning-async-block lint; done means its suggestion no longer produces the reported E0597 error or is withheld when the rewrite changes compilation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100