Skip to content

needless_borrows_for_generic_arg fires incorrectly on &mut variable inside closure #13170

Open
@zancas

Description

@zancas

Summary

clippy incorrectly warns that a borrow is needless here:

cargo clippy
   Compiling build_utils v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/build_utils)
    Checking zingoconfig v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingoconfig)
    Checking zingo-netutils v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-netutils)
    Checking zingo-memo v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-memo)
    Checking zingo-status v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-status)
   Compiling darkside-tests v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/darkside-tests)
    Checking zingo-sync v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-sync)
warning: the borrowed expression implements the required traits
  --> zingo-memo/src/lib.rs:83:28
   |
83 |         CompactSize::write(&mut w, typecode as usize)?;
   |                            ^^^^^^ help: change this to: `w`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
   = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default

When I rerun with --fix, I see (the expected) borrow of moved value



This seems superficially similar to #13162 but it causes a compile time error, not a runtime error.

Lint Name

cargo clippy --fix

Reproducer

I tried this code:

cargo clippy --fix

I saw this happen:

cargo clippy --fix
   Compiling build_utils v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/build_utils)
    Checking zingoconfig v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingoconfig)
    Checking zingo-netutils v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-netutils)
    Checking zingo-memo v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-memo)
    Checking zingo-status v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-status)
   Compiling darkside-tests v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/darkside-tests)
    Checking zingo-sync v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-sync)
    Checking zingo-testvectors v0.1.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingo-testvectors)
   Compiling zingolib v0.2.0 (/home/nattyb/src/zingolabs/zingolibs/spent_in/zingolib)
warning: failed to automatically apply fixes suggested by rustc to crate `zingo_memo`

after fixes were automatically applied the compiler reported errors within these files:

  * zingo-memo/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
warning: variable does not need to be mutable
  --> zingo-memo/src/lib.rs:75:40
   |
75 |     Vector::write(writer, &receivers, |mut w, receiver| {
   |                                        ----^
   |                                        |
   |                                        help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

error[E0382]: use of moved value: `w`
  --> zingo-memo/src/lib.rs:84:28
   |
75 |     Vector::write(writer, &receivers, |mut w, receiver| {
   |                                        ----- move occurs because `w` has type `&mut W`, which does not implement the `Copy` trait
...
83 |         CompactSize::write(w, typecode as usize)?;
   |                            - value moved here
84 |         CompactSize::write(w, data.len())?;
   |                            ^ value used here after move
   |
help: if `W` implemented `Clone`, you could clone the value
  --> zingo-memo/src/lib.rs:68:46
   |
68 | pub fn write_unified_address_to_raw_encoding<W: Write>(
   |                                              ^ consider constraining this type parameter with `Clone`
...
83 |         CompactSize::write(w, typecode as usize)?;
   |                            - you could clone this value

Version

rustc --version -v
rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions