rust-lang / rust-lang/rust-clippy

Doesn't trigger for slice.to_owned/to_vec calls which then passed as a slice

Open
#16,779 1 comment 0 reactions 1 assignee View on GitHub

@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(&copy)
}

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(&copy)
}

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

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.