Skip to content

redundant_closure false positive with Rc<F>/Arc<F> #8073

@jamesbornholt

Description

@jamesbornholt

Summary

It seems like something changed in 1.57 that causes Clippy to suggest removing a redundant closure involving an Arc<F> or Rc<F>, but the suggestion doesn't compile, and rustc explicitly suggests undoing the suggestion.

Lint Name

redundant_closure

Reproducer

I tried this code (playground):

fn run<F: Fn() -> u64>(f: F) -> u64 {
    f()
}

fn main() {
    let f = std::sync::Arc::new(|| 5);
    let f2 = f.clone();
    
    run(move || f2());
}

The redundant_closure lint suggested rewriting the run call to run(f2):

warning: redundant closure
 --> src/main.rs:9:9
  |
9 |     run(move || f2());
  |         ^^^^^^^^^^^^ help: replace the closure with the function itself: `f2`
  |
  = note: `#[warn(clippy::redundant_closure)]` on by default

But this suggestion doesn't compile, because Arc<F> is not a function. In fact, the compiler tells me to undo what Clippy suggested:

error[E0277]: expected a `Fn<()>` closure, found `Arc<[closure@src/main.rs:6:33: 6:37]>`
 --> src/main.rs:9:9
  |
9 |     run(f2);
  |     --- ^^ expected an `Fn<()>` closure, found `Arc<[closure@src/main.rs:6:33: 6:37]>`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Fn<()>` is not implemented for `Arc<[closure@src/main.rs:6:33: 6:37]>`
  = note: wrap the `Arc<[closure@src/main.rs:6:33: 6:37]>` in a closure with no arguments: `|| { /* code */ }`

This seems to be new behavior in 1.57; the code that triggered this has been around for a while and did not trigger this lint on 1.56. The example code above also does not trigger the lint on 1.56.

Version

rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: aarch64-apple-darwin
release: 1.57.0
LLVM version: 13.0.0

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 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