rust-lang / rust-lang/rust-clippy
Doesn't trigger for slice.to_owned/to_vec calls which then passed as a slice
Open
@1mpossible-code is already working on this.
Since Apr 3, 2026.
C-bug
I-false-negative
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
If you have a slice, and need a slice, I've seen code where someone calls .to_vec() or .to_owned() and then uses a & to convert that back to a slice. This is an unnecessary performance penalty, and fits in line with the existing unnecessary_to_owned/redundant_clone lints.
Lint Name
unnecessary_to_owned/redundant_clone
Reproducer
I tried this code:
#![deny(clippy::redundant_clone, clippy::unnecessary_to_owned)]
fn main() {
let values = vec!["Hello".to_owned()];
println!("{}", does_unnecessary_copy_with_to_vec(&values));
println!("{}", does_unnecessary_copy_with_to_owned(&values));
}
fn does_unnecessary_copy_with_to_vec(values: &[String]) -> usize {
let copy = values.to_vec(); // Unnecessary - just pass `values` directly to `takes_slice`.
takes_slice(©)
}
fn does_unnecessary_copy_with_to_owned(values: &[String]) -> usize {
let copy = values.to_owned(); // Unnecessary - just pass `values` directly to `takes_slice`.
takes_slice(©)
}
fn takes_slice(values: &[String]) -> usize {
values.len()
}
I expected to see this happen:
Error on lines 10 and 15 for unnecessary copies.
Instead, this happened:
No errors.
Version
rustc 1.94.0 (4a4ef493e 2026-03-02)
binary: rustc
commit-hash: 4a4ef493e3a1488c6e321570238084b38948f6db
commit-date: 2026-03-02
host: aarch64-apple-darwin
release: 1.94.0
LLVM version: 21.1.8
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.