Open
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