rust-lang / rust-lang/rust-clippy
False positive in `unnecessary_to_owned` when a mutable borrow would cause a compiler error
Open
@saberoueslati is already working on this.
Since Sep 19, 2026.
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
Minimal reproduction:
pub fn one_borrow(data: &mut [u8]) {
multiple_borrows(
// warning: unnecessary use of `to_vec`
// but if we remove `to_vec()`, we get
// error[E0502]: cannot borrow `*data` as mutable because it is also borrowed as immutable
&data[0..8].to_vec(),
&mut data[8..16]
);
}
fn multiple_borrows(src:&[u8], dest: &mut [u8]){
// Doesn't really matter what's in this function
if src.len() == dest.len() {
dest.copy_from_slice(src);
}
}
(I know in this contrived use case you can use split_at_mut, but this can occur in cases where that isn't possible)
Lint Name
unnecessary_to_owned
Reproducer
I tried this code:
pub fn one_borrow(data: &mut [u8]) {
multiple_borrows(
// warning: unnecessary use of `to_vec`
// but if we remove `to_vec()`, we get
// error[E0502]: cannot borrow `*data` as mutable because it is also borrowed as immutable
&data[0..8].to_vec(),
&mut data[8..16]
);
}
fn multiple_borrows(src:&[u8], dest: &mut [u8]){
// Doesn't really matter what's in this function
if src.len() == dest.len() {
dest.copy_from_slice(src);
}
}
I saw this happen:
warning: unnecessary use of `to_vec`
I expected to see this happen:
No warning.
Version
occurs on both rustc 1.87.0 (17067e9ac 2025-05-09) and rustc 1.89.0-nightly (283db70ac 2025-05-25)
rustc 1.89.0-nightly (283db70ac 2025-05-25)
binary: rustc
commit-hash: 283db70ace62a0ae704a624e43b68c2ee44b87a6
commit-date: 2025-05-25
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
Additional Labels
@rustbot label +I-suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.