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

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

Closed
jamesbornholt opened this issue Dec 3, 2021 · 0 comments · Fixed by #8193
Closed

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

jamesbornholt opened this issue Dec 3, 2021 · 0 comments · Fixed by #8193
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

@jamesbornholt
Copy link

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

@jamesbornholt jamesbornholt 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 3, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 3, 2021
bors added a commit that referenced this issue Dec 29, 2021
fix [`redundant_closure`] fp with `Rc<F>`/`Arc<F>`

fixes #8073

changelog: don't trigger [`redundant_closure`] on `Arc<F>` or `Rc<F>`
@bors bors closed this as completed in c736a63 Dec 31, 2021
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.

2 participants