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 suggests wrong fix when the function being called is not Copy #1608

Open
utkarshkukreti opened this issue Mar 6, 2017 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@utkarshkukreti
Copy link

utkarshkukreti commented Mar 6, 2017

Code reduced from my actual project:

fn main() {
    let f = |_| true;
    while Some(1).map_or(false, |x| f(x)) {}
}

Clippy:

warning: redundant closure found
 --> src/main.rs:3:33
  |
3 |     while Some(1).map_or(false, |x| f(x)) {}
  |                                 ^^^^^^^^
  |
  = note: #[warn(redundant_closure)] on by default
help: remove closure as shown:
  |     while Some(1).map_or(false, f) {}
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#redundant_closure

Clippy's suggestion doesn't compile:

error[E0382]: use of moved value: `f`
 --> src/main.rs:3:33
  |
3 |     while Some(1).map_or(false, f) {}
  |                                 ^ value moved here in previous iteration of loop
  |
  = note: move occurs because `f` has type `[closure@src/main.rs:2:13: 2:21]`, which does not implement the `Copy` trait

error: aborting due to previous error
$ rustc -V
rustc 1.17.0-nightly (b1e31766d 2017-03-03)
$ cargo clippy -V
0.0.118
@oli-obk
Copy link
Contributor

oli-obk commented Mar 6, 2017

This should be fixed by suggesting an & before the closure name in case it is not Copy

@utkarshkukreti
Copy link
Author

That works for the code I posted above but doesn't work in my actual code. Here's a slightly bigger test case which doesn't work with &.

This compiles but Clippy gives the same warning:

fn take_while<F>(str: &str, mut f: F)
    where F: FnMut(char) -> bool
{
    let mut chars = str.chars();
    while chars.clone().next().map_or(false, |ch| f(ch)) {
        chars.next();
    }
}
fn main() {
    take_while("...!", |ch| ch == '.');
}

With &f:

error[E0277]: the trait bound `F: std::ops::Fn<(char,)>` is not satisfied
 --> src/main.rs:5:32
  |
5 |     while chars.clone().next().map_or(false, &f) {
  |                                ^^^^^^ the trait `std::ops::Fn<(char,)>` is not implemented for `F`
  |
  = help: consider adding a `where F: std::ops::Fn<(char,)>` bound
  = note: required because of the requirements on the impl of `std::ops::FnOnce<(char,)>` for `&F`

With f:

error[E0382]: use of moved value: `f`
 --> src/main.rs:5:46
  |
5 |     while chars.clone().next().map_or(false, f) {
  |                                              ^ value moved here in previous iteration of loop
  |
  = note: move occurs because `f` has type `F`, which does not implement the `Copy` trait

@oli-obk
Copy link
Contributor

oli-obk commented Mar 6, 2017

Right, in that case we need &mut

@utkarshkukreti
Copy link
Author

Ahh, thanks! I tried a lot of things to make &f compile but I forgot that FnMut would be implemented for &mut f and not &f.

@utkarshkukreti utkarshkukreti changed the title redundant_closure false positive when the expression is in the condition of a while redundant_closure suggests wrong fix when the function being called is not Copy Mar 28, 2017
@cipriancraciun
Copy link

I also got the same issue with an non-Copy Fn used in a loop. (However in my case an & before the function did solve the issue...) (clippy version is 0.0.211)

(If needed I can provide the source code.)

@camsteffen camsteffen added C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed L-suggestion Lint: Improving, adding or fixing lint suggestions labels Nov 14, 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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. 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

4 participants