rust-lang / rust-lang/rust-clippy

`cloned_ref_to_slice_refs` suggestion may cause error

Open
#14,856 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

cloned_ref_to_slice_refs lint may trigger when the clone() prevents borrow checker issues.

Lint Name

cloned_ref_to_slice_refs

Reproducer

I tried this code:

use std::sync::RwLock;

#[derive(Clone)]
struct Foo;
struct Bar;

struct FooBar {
    foo: Foo,
    bar: Bar,
}

fn main() {
    let lock = RwLock::new(FooBar{
        foo: Foo,
        bar: Bar,
    });
    let mut foobar = lock.write().unwrap();
    let f = &foobar.foo;

    do_something(&[f.clone()], &mut foobar.bar);
    //do_something(std::slice::from_ref(f), &mut foobar_ref.bar);
}

fn do_something(_foos: &[Foo], _bar: &mut Bar) {
    // ...
}

I saw this happen:

warning: this call to `clone` can be replaced with `std::slice::from_ref`
  --> src\foobar.rs:20:18
   |
20 |     do_something(&[f.clone()], &mut foobar.bar);
   |                  ^^^^^^^^^^^^ help: try: `std::slice::from_ref(f)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cloned_ref_to_slice_refs

I expected to see this happen:
Nothing.
Following the suggestion leads to the following error:

error[E0502]: cannot borrow `foobar` as mutable because it is also borrowed as immutable
  --> src\foobar.rs:21:48
   |
18 |     let f= &foobar.foo;
   |             ------ immutable borrow occurs here
...
21 |     do_something(std::slice::from_ref(f), &mut foobar.bar);
   |     ------------                               ^^^^^^ mutable borrow occurs here
   |     |
   |     immutable borrow later used by call
Version
rustc 1.89.0-nightly (60dabef95 2025-05-19)
binary: rustc
commit-hash: 60dabef95a3de3ec974dcb50926e4bfe743f078f
commit-date: 2025-05-19
host: x86_64-pc-windows-msvc
release: 1.89.0-nightly
LLVM version: 20.1.5
Additional Labels

@rustbot label +I-suggestion-causes-error

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 cloned_ref_to_slice_refs lint and run the supplied Rust reproducer to observe the warning and suggested replacement. Trace how the lint determines that from_ref is safe, then ensure this case does not receive an invalid suggestion and verify the reproducer no longer reports the problematic recommendation.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.