Skip to content

manual_async_fn suggests code with incompatible lifetimes #14372

Open
@SpecificProtagonist

Description

@SpecificProtagonist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions