-
Notifications
You must be signed in to change notification settings - Fork 2k
Doesn't trigger for slice.to_owned/to_vec calls which then passed as a slice #16779
Copy link
Copy link
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't
Type
Fields
Give feedbackNo fields configured for issues without a type.