Skip to content

redundant_closure suggests wrong fix when the function being called is not Copy #1608

Open
@utkarshkukreti

Description

@utkarshkukreti

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-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