rust-lang / rust-lang/rust-clippy

needless_borrows_for_generic_arg fires incorrectly on &mut variable inside closure

Open
#13,170 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the needless_borrows_for_generic_args lint and reproduce the warning using the closure in zingo-memo/src/lib.rs:75-84. Run cargo clippy --fix and verify that the lint no longer suggests removing the borrow when doing so causes w to move, while valid needless-borrow cases remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.