Skip to content

mut_mut complains when borrowing from a slice of mutable references #13021

@cmrschwarz

Description

@cmrschwarz

Summary

There are many valid reasons to work on a mutable slice of mutable references &mut [&mut Foo].

For example, if the Foos are large and you temporarily want to work on them in a sorted order,
you might reasonably want to sort mutable references to them instead.
In other cases the Foos might not be movable at all, but you want to process a shrinking subset of them.

When you now want to borrow a single element from this slice,
you end up with the type &mut &mut Foo,
because you can't move / clone the mutable reference out of the slice.

Clippy complains about this, which it shouldn't imo.

Lint Name

mut_mut

Reproducer

I tried this code:

#![warn(clippy::pedantic)]
fn process_foos(array_of_foos: &mut [&mut Foo]) {
    let entry = &mut array_of_foos[42];
    // modify entry...
}

I saw this happen:

warning: this expression mutably borrows a mutable reference. Consider reborrowing
  --> src/main.rs:13:17
   |
13 |     let entry = &mut array_of_foos[42];
   |                 ^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut
note: the lint level is defined here
  --> src/main.rs:9:8
   |
9  | #[warn(clippy::pedantic)]
   |        ^^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::mut_mut)]` implied by `#[warn(clippy::pedantic)]`

I expected to see this happen:
No warnings should be generated.

Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

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