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 moved FnMut #6903

Closed
glittershark opened this issue Mar 14, 2021 · 3 comments · Fixed by #7437
Closed

Redundant_closure false positive with moved FnMut #6903

glittershark opened this issue Mar 14, 2021 · 3 comments · Fixed by #7437
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

@glittershark
Copy link

Lint name: redundant_closure

I tried this code (reduced to a minimal example):

fn fp(x: Vec<i32>, y: Vec<i32>) -> Vec<i32> {
    let mut res = Vec::new();
    let mut add_to_res = |n| res.push(n);
    x.into_iter().for_each(|x| add_to_res(x));
    y.into_iter().for_each(add_to_res);
    res
}

If I apply the suggestion (redundant_closure on the fourth line), resulting in the following code:

fn fp(x: Vec<i32>, y: Vec<i32>) -> Vec<i32> {
    let mut res = Vec::new();
    let mut add_to_res = |n| res.push(n);
    x.into_iter().for_each(add_to_res);
    y.into_iter().for_each(add_to_res);
    res
}

I get the following error:

error[E0382]: use of moved value: `add_to_res`
 --> src/lib.rs:7:28
  |
6 |     x.into_iter().for_each(add_to_res);
  |                            ---------- value moved here
7 |     y.into_iter().for_each(add_to_res);
  |                            ^^^^^^^^^^ value used here after move
  |
note: closure cannot be moved more than once as it is not `Copy` due to moving the variable `res` out of its environment
 --> src/lib.rs:5:30
  |
5 |     let mut add_to_res = |n| res.push(n);
  |                              ^^^

Meta

  • cargo clippy -V: e.g. clippy 0.0.212 (98d6634 2020-11-14)
  • rustc -Vv:
    rustc 1.50.0-nightly (98d66340d 2020-11-14)
    binary: rustc
    commmit-hash: 98d66340d6e63eda115afc8b0da1d87965881936
    commit-date: 2020-11-14
    host: x86_64-unknown-linux-gnu
    release: 1.50.0-nightly
    
@glittershark glittershark 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 Mar 14, 2021
@camsteffen camsteffen added 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 and removed I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 29, 2021
@camsteffen
Copy link
Contributor

It should suggest for_each(&mut add_to_res).

@ebobrow
Copy link
Contributor

ebobrow commented Jun 30, 2021

I'd like to try this. Should the lint always suggest &mut for FnMut closures, or only if the closure is later reused? I believe that || some_mut_func() is identical to &mut some_mut_func, so there's no harm in adding &mut except that it's potentially unnecessary code.

@camsteffen
Copy link
Contributor

Should the lint always suggest &mut for FnMut closures, or only if the closure is later reused?

Yes. clippy_utils::path_to_local and LocalUsedVisitor should come in handy.

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

Successfully merging a pull request may close this issue.

3 participants