Skip to content

redundant_closure doesn't handle Fn #12853

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

Closed
mladedav opened this issue May 25, 2024 · 1 comment · Fixed by #12865
Closed

redundant_closure doesn't handle Fn #12853

mladedav opened this issue May 25, 2024 · 1 comment · Fixed by #12865
Assignees
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

@mladedav
Copy link
Contributor

mladedav commented May 25, 2024

Summary

When a closure |val| fun(val) is used where fun is Fn, the suggestion is to use it directly as fun. However, that may not compile and the correct suggestion is &fun.

Lint Name

redundat_closure

Reproducer

I tried this code:

#![warn(clippy::redundant_closure)]
#![allow(dead_code)]

fn foo<F: Fn(u32) -> u32 + 'static>(f: F) -> Option<u32> {
    let x = Box::new(move || None.map(|x| f(x)));

    x()
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: redundant closure
 --> src/lib.rs:5:39
  |
5 |     let x = Box::new(move || None.map(|x| f(x)));
  |                                       ^^^^^^^^ help: replace the closure with the function itself: `f`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::redundant_closure)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^

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

and if I apply that fix, I get:

error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
 --> src/lib.rs:5:22
  |
5 |     let x = Box::new(move || None.map(f));
  |                      ^^^^^^^          - closure is `FnOnce` because it moves the variable `f` out of its environment
  |                      |
  |                      this closure implements `FnOnce`, not `Fn`
6 |
7 |     x()
  |     --- the requirement to implement `Fn` derives from here
  |
  = note: required for `Box<{closure@src/lib.rs:5:22: 5:29}>` to implement `Fn<()>`

For more information about this error, try `rustc --explain E0525`.
error: could not compile `playground` (lib) due to 1 previous error

I expected to see this happen (note the ampersand, that's what's missing):

...

5 |     let x = Box::new(move || None.map(|x| f(x)));
  |                                       ^^^^^^^^ help: replace the closure with the function itself: `&f`
...

Version

1.78.0 (playground)

Additional Labels

@rustbot label +I-suggestion-causes-error

@mladedav mladedav 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 May 25, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label May 25, 2024
@J-ZhengLi
Copy link
Member

J-ZhengLi commented May 27, 2024

looks like another adjustment problem?

@rustbot claim

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

Successfully merging a pull request may close this issue.

3 participants