Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sigurd4 opened this issue Dec 28, 2024 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@sigurd4
Copy link

sigurd4 commented Dec 28, 2024

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

@sigurd4 sigurd4 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 28, 2024
@sigurd4
Copy link
Author

sigurd4 commented Dec 28, 2024

@rustbot

  • "-I-suggestion-causes-error"

@sigurd4
Copy link
Author

sigurd4 commented Dec 28, 2024

@rustbot +I-suggestion-causes-error

I hope this works

@sigurd4
Copy link
Author

sigurd4 commented Dec 28, 2024

@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants