rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

automatic fix tries to remove #[async_recursion] annotation

Open pacak opened this issue 2 months ago • 5 comments

Summary

~~You were the Chosen One! It was said that you would destroy the Sith, not join them!~~

Clippy tries to break perfectly working code then fails.

Reproducer

Code:

[dependencies]
async-recursion = "=1.1.1"
#[async_recursion::async_recursion]
pub async fn f() {
    f().await
}

Current output:

error: errors present after applying fixes to crate `clippy_repro_async_recursion`
 --> src/lib.rs
  = cause: error[E0733]: recursion in an async fn requires boxing
            --> src/lib.rs:1:1
             |
           1 | pub async fn f() {
             | ^^^^^^^^^^^^^^^^
           2 |     f().await
             |     --------- recursive call here
             |
             = note: a recursive `async fn` call must introduce indirection such as `Box::pin` to avoid an infinitely sized future


           error: aborting due to 1 previous error


           For more information about this error, try `rustc --explain E0733`.
help: to report this as a bug, open an issue at https://github.com/rust-lang/rust-clippy/issues, quoting the full output of this command
help: to possibly apply more fixes, pass in the `--broken-code` flag
note: original diagnostics will follow:
warning: this function has a `#[must_use]` attribute with no message, but returns a type already considered as `#[must_use]`
 --> src/lib.rs:1:36
  |
1 |   #[async_recursion::async_recursion]
  |  _-----------------------------------^
  | | |
  | | help: remove the attribute
2 | | pub async fn f() {
  | |_^
  |
note: the return type is pinned boxed `std::future::Future` trait object
 --> src/lib.rs:1:1
  |
1 | #[async_recursion::async_recursion]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: alternatively, you may add an explicit reason to the `must_use` attribute
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.99.0/index.html#double_must_use
  = note: `#[warn(clippy::double_must_use)]` on by default
  = note: this warning originates in the attribute macro `async_recursion::async_recursion` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `clippy-repro-async-recursion` (lib) generated 1 warning (run `cargo clippy --fix --lib -p clippy-repro-async-recursion -- --no-deps` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s

Desired output:

Nothing?

Version

rustc 1.99.0 (b940084d7 2026-09-28)
binary: rustc
commit-hash: b940084d7eb6a299eb4bfeb8e34901bc051e7ac4
commit-date: 2026-09-28
host: x86_64-unknown-linux-gnu
release: 1.99.0
LLVM version: 23.1.1

Additional Labels

No response

pacak avatar Oct 01 '26 18:10 pacak

Now, if code does not actually need the recursion

#[async_recursion::async_recursion]
pub async fn f() {
}

it prints this

    Checking clippy-repro-async-recursion v0.1.0 (/tmp/clippy-repro/async-recursion)
warning: this function has a `#[must_use]` attribute with no message, but returns a type already considered as `#[must_use]`
 --> src/lib.rs:1:36
  |
1 |   #[async_recursion::async_recursion]
  |  _-----------------------------------^
  | | |
  | | help: remove the attribute
2 | | pub async fn f() {}
  | |_^
  |
note: the return type is pinned boxed `std::future::Future` trait object
 --> src/lib.rs:1:1
  |
1 | #[async_recursion::async_recursion]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: alternatively, you may add an explicit reason to the `must_use` attribute
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.99.0/index.html#double_must_use
  = note: `#[warn(clippy::double_must_use)]` on by default
  = note: this warning originates in the attribute macro `async_recursion::async_recursion` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `clippy-repro-async-recursion` (lib) generated 1 warning (run `cargo clippy --fix --lib -p clippy-repro-async-recursion -- ` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s

And silently removes it.

Still wrong - I don't see a #[must_use] and removing it might break some code where recursion is used conditionally.

pacak avatar Oct 01 '26 18:10 pacak

#[async_recursion.. has an #[must_use] inside it. The double_must_use lint should not be triggered inside macros.

That this points to the outer attribute is a bit unfortunate/funny.

@rustbot label "good first issue"

Our docs about macros are here ^^ https://doc.rust-lang.org/nightly/clippy/development/macro_expansions.html

CommanderStorm avatar Oct 01 '26 18:10 CommanderStorm

@rustbot claim

Mitshah2406 avatar Oct 01 '26 18:10 Mitshah2406

#[async_recursion.. has an #[must_use] inside it. The double_must_use lint should not be triggered inside macros.

This sounds like a good improvement to me.

djc avatar Oct 02 '26 12:10 djc

BTW for anyone coming here for async_recursion in particular, they fixed (worked around? seems to me they removed must_use altogether..) the warning in https://github.com/dcchut/async-recursion/pull/50 (and available in 1.2.0 release)

martinetd avatar Oct 23 '26 02:10 martinetd