Skip to content

Clippy --fix redundant closure breaks with function call through deref #15072

Open
@ealmloff

Description

@ealmloff

Summary

Clippy tries to replace a closure in the form of |x| y(x) with y, but that fails because y can only be called as a function through the deref impl

Lint Name

redundant closure

Reproducer

I tried this code:

use std::ops::Deref;

fn main() {
    #[allow(non_camel_case_types)]
    struct foo;

    impl Deref for foo {
        type Target = fn() -> &'static str;

        fn deref(&self) -> &Self::Target {
            fn hello() -> &'static str {
                "Hello, world!"
            }
            &(hello as fn() -> &'static str)
        }
    }

    fn accepts_fn(f: impl Fn() -> &'static str) {
        println!("{}", f());
    }

    let f = foo;
    accepts_fn(|| f());
}

I saw this happen:

> cargo clippy --fix
    Checking clippy-bug v0.1.0 (/Users/evanalmloff/Desktop/clippy-bug)
warning: failed to automatically apply fixes suggested by rustc to crate `clippy_bug`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0277]: expected a `Fn()` closure, found `main::foo`
  --> src/main.rs:23:16
   |
23 |     accepts_fn(f);
   |     ---------- ^ expected an `Fn()` closure, found `main::foo`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Fn()` is not implemented for `main::foo`
   = note: wrap the `main::foo` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `main::accepts_fn`
  --> src/main.rs:18:27
   |
18 |     fn accepts_fn(f: impl Fn() -> &'static str) {
   |                           ^^^^^^^^^^^^^^^^^^^^ required by this bound in `accepts_fn`
help: consider dereferencing here
   |
23 |     accepts_fn(*f);
   |                +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: redundant closure
  --> src/main.rs:23:16
   |
23 |     accepts_fn(|| f());
   |                ^^^^^^ 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: `#[warn(clippy::redundant_closure)]` on by default

warning: `clippy-bug` (bin "clippy-bug") generated 1 warning (run `cargo clippy --fix --bin "clippy-bug"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s

I expected to see this happen:
Either clippy does not modify the code or fixes the clippy lint itself. If clippy can look for the deref on function call, it could replace the closure with accepts_fn(*f);

Version

rustc 1.89.0-nightly (99e7c15e8 2025-06-01)
binary: rustc
commit-hash: 99e7c15e81385b38a8186b51edc4577d5d7b5bdd
commit-date: 2025-06-01
host: aarch64-apple-darwin
release: 1.89.0-nightly
LLVM version: 20.1.5

Additional Labels

@rustbot label +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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions