Open
Description
Summary
Clippy suggests writing a function as async fn
that cannot be written that way.
Lint Name
manual_async_fn
Reproducer
I tried this code:
trait Trait {
fn async_function<'long>(_: &(), _: &'long ()) -> impl Future<Output = ()> + 'long;
}
impl Trait for () {
fn async_function<'long>(_: &(), _: &'long ()) -> impl Future<Output = ()> + 'long {
async {}
}
}
I saw this happen:
warning: this function can be simplified using the `async fn` syntax
--> src/main.rs:6:5
|
6 | fn async_function<'long>(_: &(), _: &'long ()) -> impl Future<Output = ()> + 'long {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_async_fn
= note: `#[warn(clippy::manual_async_fn)]` on by default
help: make the function `async` and return the output of the future directly
|
6 - fn async_function<'long>(_: &(), _: &'long ()) -> impl Future<Output = ()> + 'long {
6 + async fn async_function<'long>(_: &(), _: &'long ()) {}
I expected this to happen:
The lint not triggering, as async fn
can not be used here as it describes the wrong lifetimes.
Compile error when suggestion is applied
``` error[E0477]: the type `impl std::future::Future` does not fulfill the required lifetime --> src/main.rs:6:5 | 6 | async fn async_function<'long>(_: &(), _: &'long ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: type must outlive the lifetime `'long` as defined here as required by this binding --> src/main.rs:6:29 | 6 | async fn async_function<'long>(_: &(), _: &'long ()) {} | ```Version
rustc 1.87.0-nightly (f280acf4c 2025-02-19)
binary: rustc
commit-hash: f280acf4c743806abbbbcfe65050ac52ec4bdec0
commit-date: 2025-02-19
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0
Additional Labels
No response