rust-lang / rust-lang/rust-clippy

needless_pass_by_ref_mut: false negative with fn and closures

Open
#12,588 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

No clippy warning for needless_pass_by_ref_mut when both wrapping fn and closure have mutable args.

Discovered while working on https://github.com/rust-lang/rust/pull/123188

checked with clippy from playground 0.1.79 (2024-03-27 c9f8f34)
https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=047b73855da451dd617b8d43557b8d0d

Lint Name

needless_pass_by_ref_mut

Reproducer

I tried this code:

#![warn(clippy::needless_pass_by_ref_mut)]

fn main() {}

fn foo(n: &mut u8)->u8 {
    let x = |c: &mut u8| *c;
    x(n)
}

fn foo2(n: &mut u8)->u8 {
    let x = |c: &u8| *c;
    x(n)
}

fn foo3(n: &u8)->u8 {
    let x = |c: &u8| *c;
    x(n)
}

I expected to see this happen:
For foo should warn about unused &mut in fn args and closure. Probably should first warn about closure arg, because if closure fixed, it will successfully warn about fn args (as in foo2)

Instead, this happened:

No warn for foo

Version

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 by locating the needless_pass_by_ref_mut lint entry point and its existing tests, then run the Rust reproducer from the issue. Done means the foo case reports the unnecessary mutable references, with the closure argument diagnosed before the wrapping function argument as described.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.