Skip to content

suggestion for unnecessary_mut_passed doesn't add surrounding parens #15722

@ada4a

Description

@ada4a

Summary

When the mutable reference passed is surrounded by parens -- for example to have a method called on -- the suggestion forgets to reinstate those parens. This can break the types, since the & now might get seen as being placed on the entire expression.

Reproducer

I tried this code:

#![allow(unused, clippy::needless_borrow)]
#![warn(clippy::unnecessary_mut_passed)]

fn main() {
    let _: usize = (&mut [0]).len();
}

I expected to see this happen:

warning: the method `len` doesn't need a mutable reference
 --> src/lib.rs:5:20
  |
5 |     let _: usize = (&mut [0]).len();
  |                    ^^^^^^^^^^ help: remove this `mut`: `(&[0])`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed

Instead, this happened:

warning: the method `len` doesn't need a mutable reference
 --> src/lib.rs:5:20
  |
5 |     let _: usize = (&mut [0]).len();
  |                    ^^^^^^^^^^ help: remove this `mut`: `&[0]`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed

Version

rustc 1.92.0-nightly (7c275d09e 2025-09-18)
binary: rustc
commit-hash: 7c275d09ea6b953d2cca169667184a7214bd14c7
commit-date: 2025-09-18
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.1

Additional Labels

@rustbot label I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions