Closed
Description
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