rust-lang / rust-lang/rust-clippy

manual_async_fn suggests code with incompatible lifetimes

Open
#14,372 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the manual_async_fn lint and reproduce the diagnostic using the example in src/main.rs. Compare the suggested async fn signature with the original lifetime requirements; done means the lint no longer triggers for this case and a regression test covers the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.