RS.CLIPPY.UNNECESSARY_TO_OWNED

Unnecessary calls to `to_owned`-like functions

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unnecessary_to_owned. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for unnecessary calls to ToOwned::to_owned and other to_owned-like functions.

Why is this bad?

The unnecessary calls result in useless allocations.

Known problems

unnecessary_to_owned can falsely trigger if IntoIterator::into_iter is applied to an owned copy of a resource and the resource is later used mutably. See #8148.

Example

let path = std::path::Path::new("x");
foo(&path.to_string_lossy().to_string());
fn foo(s: &str) {}

Use instead:

let path = std::path::Path::new("x");
foo(&path.to_string_lossy());
fn foo(s: &str) {}