RS.CLIPPY.SUSPICIOUS_TO_OWNED
Calls to `to_owned` on a `Cow<'_, _>` might not do what they are expected
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: suspicious_to_owned. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for the usage of _.to_owned(), on a Cow<\'_, _>.
Why is this bad?
Calling to_owned() on a Cow creates a clone of the Cow
itself, without taking ownership of the Cow contents (i.e.
it's equivalent to calling Cow::clone).
The similarly named into_owned method, on the other hand,
clones the Cow contents, effectively turning any Cow::Borrowed
into a Cow::Owned.
Given the potential ambiguity, consider replacing to_owned
with clone for better readability or, if getting a Cow::Owned
was the original intent, using into_owned instead.
Example
let s = "Hello world!";
let cow = Cow::Borrowed(s);
let data = cow.to_owned();
assert!(matches!(data, Cow::Borrowed(_)))
Use instead:
let s = "Hello world!";
let cow = Cow::Borrowed(s);
let data = cow.clone();
assert!(matches!(data, Cow::Borrowed(_)))
or
let s = "Hello world!";
let cow = Cow::Borrowed(s);
let _data: String = cow.into_owned();