Skip to content

impl AsyncFn used as argument for function that takes impl Fn passed as closure triggers clippy::redundant_closure #13892

Open
@sigurd4

Description

@sigurd4

Summary

Currently, in the latest rust nightly, it's not possible to pass an impl AsyncFn into a function that takes an impl Fn to call it without awaiting, without using a closure as a workaround.

Still, clippy warns about using a closure to do this, and assumes you can just pass the asynchronous function directly, which would give a compile error.

Lint Name

clippy::redundant_closure

Reproducer

I tried this code (within an async context):

pub async fn visit_async<'a, T, const N: usize, F>(array: &'a [T; N], visitor: F)
where
    F: AsyncFn(&'a T),
    T: 'a
{
    //let futures = array.each_ref().map(visitor); // This is what clippy wants me to do, which, i agree, looks nicer, does not actually compile.

    #[allow(unused)] // Temporarily because of todo!()
    let futures = array.each_ref().map(|x| visitor(x));

    // This is where i'd somehow join all the futures in the array `futures` and await them.
    // futures.join_all().await // For example, if join_all was a thing
    todo!()
}

I saw this happen:

$ cargo clippy
    Checking minimum_reproducable_example v1.0.5 (/home/sigurd/Code/rust/sss/minimum_reproducable_example)
warning: redundant closure
 --> minimum_reproducable_example/src/lib.rs:9:40
  |
9 |     let futures = array.each_ref().map(|x| visitor(x));
  |                                        ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `visitor`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
  = note: `#[warn(clippy::redundant_closure)]` on by default

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

I expected to see this happen:

No warning, because what clippy suggests to do instead is not possible at the moment.

Version

$ rustc -Vv
rustc 1.85.0-nightly (dd84b7d5e 2024-12-27)
binary: rustc
commit-hash: dd84b7d5eec3c20d7fcd13e6450af029d3cece9d
commit-date: 2024-12-27
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

@rustbot -I-suggestion-causes-error

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