Skip to content

needless_pass_by_ref_mut false positive in closure #11561

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

Closed
cuviper opened this issue Sep 24, 2023 · 2 comments · Fixed by #11621
Closed

needless_pass_by_ref_mut false positive in closure #11561

cuviper opened this issue Sep 24, 2023 · 2 comments · Fixed by #11621
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

@cuviper
Copy link
Member

cuviper commented Sep 24, 2023

Summary

I have a closure that captures &mut impl FnMut and calls it, and this lint thinks it doesn't need to be mutable.

I can reproduce this on 1.73.0-beta.7 and 1.74.0-nightly (13e6f24b9 2023-09-23), as well as my own compilation of clippy master (aa137a7e5705 2023-09-24).

Lint Name

needless_pass_by_ref_mut

Reproducer

I tried this code:

pub fn filter_copy<T: Copy>(predicate: &mut impl FnMut(T) -> bool) -> impl FnMut(&T) -> bool + '_ {
    move |&item| predicate(item)
}

I saw this happen:

warning: this argument is a mutable reference, but not used mutably
 --> src/lib.rs:1:40
  |
1 | pub fn filter_copy<T: Copy>(predicate: &mut impl FnMut(T) -> bool) -> impl FnMut(&T) -> bool + '_ {
  |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&impl FnMut(T) -> bool`
  |
  = warning: changing this function will impact semver compatibility
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
  = note: `#[warn(clippy::needless_pass_by_ref_mut)]` on by default

I expected to see this happen:
No lint, as it must be &mut to be callable in the closure.

The suggested change to &impl FnMut is an error:

error[E0596]: cannot borrow `*predicate` as mutable, as it is behind a `&` reference
 --> src/lib.rs:2:18
  |
2 |     move |&item| predicate(item)
  |                  ^^^^^^^^^ cannot borrow as mutable

Version

rustc 1.74.0-nightly (13e6f24b9 2023-09-23)
binary: rustc
commit-hash: 13e6f24b9adda67852fb86538541adaa68aff6e8
commit-date: 2023-09-23
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0

Additional Labels

No response

@cuviper cuviper 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 Sep 24, 2023
@cuviper
Copy link
Member Author

cuviper commented Sep 25, 2023

@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Sep 25, 2023
@GuillaumeGomez
Copy link
Member

Fixed in #11621. I'm adding a regression test in it just in case.

GuillaumeGomez added a commit to GuillaumeGomez/rust-clippy that referenced this issue Oct 6, 2023
GuillaumeGomez added a commit to GuillaumeGomez/rust-clippy that referenced this issue Oct 6, 2023
GuillaumeGomez added a commit to GuillaumeGomez/rust-clippy that referenced this issue Oct 17, 2023
GuillaumeGomez added a commit to GuillaumeGomez/rust-clippy that referenced this issue Oct 18, 2023
@bors bors closed this as completed in cd477d4 Oct 19, 2023
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.

3 participants