Skip to content

Add suggestion to .to_owned() used on Cow when borrowing#154646

Open
m4rch3n1ng wants to merge 2 commits intorust-lang:mainfrom
m4rch3n1ng:144792-cow-diag
Open

Add suggestion to .to_owned() used on Cow when borrowing#154646
m4rch3n1ng wants to merge 2 commits intorust-lang:mainfrom
m4rch3n1ng:144792-cow-diag

Conversation

@m4rch3n1ng
Copy link
Copy Markdown
Contributor

@m4rch3n1ng m4rch3n1ng commented Mar 31, 2026

fixes #144792
supersedes #144925 with the review comments addressed

the tests suggested from @Kivooeo from #144925 (comment) didn't work entirely, because these tests failed due to error [E0308] mismatched types, which actually already provides a suggestion, that actually makes the code compile:

$ cargo check
error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
1 | fn test_cow_suggestion() -> String {
  |                             ------ expected `std::string::String` because of return type
2 |     let os_string = std::ffi::OsString::from("test");
3 |     os_string.to_string_lossy().to_owned()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Cow<'_, str>`
  |
  = note: expected struct `std::string::String`
               found enum `std::borrow::Cow<'_, str>`
help: try using a conversion method
  |
3 |     os_string.to_string_lossy().to_owned().to_string()
  |                                           ++++++++++++

now this suggestion is of course not good or efficient code, but via clippy with -Wclippy::nursery lint group you can actually get to the correct code, so i don't think this is too much of an issue:

the clippy suggestions
$ cargo clippy -- -Wclippy::nursery
warning: this `to_owned` call clones the `Cow<'_, str>` itself and does not cause its contents to become owned
 --> src/main.rs:3:5
  |
3 |     os_string.to_string_lossy().to_owned().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#suspicious_to_owned
  = note: `#[warn(clippy::suspicious_to_owned)]` on by default
help: depending on intent, either make the `Cow` an `Owned` variant
  |
3 |     os_string.to_string_lossy().into_owned().to_string()
  |                                 ++
help: or clone the `Cow` itself
  |
3 -     os_string.to_string_lossy().to_owned().to_string()
3 +     os_string.to_string_lossy().clone().to_string()
  |
$ # apply first suggestion
$ cargo c -- -Wclippy::nursery
warning: redundant clone
 --> src/main.rs:3:45
  |
3 |     os_string.to_string_lossy().into_owned().to_string()
  |                                             ^^^^^^^^^^^^ help: remove this
  |
note: this value is dropped without further use
 --> src/main.rs:3:5
  |
3 |     os_string.to_string_lossy().into_owned().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#redundant_clone
  = note: `-W clippy::redundant-clone` implied by `-W clippy::nursery`
  = help: to override `-W clippy::nursery` add `#[allow(clippy::redundant_clone)]`

the actual error that we are looking for is error [E0515], cannot return value referencing local variable, which was only present in the original issue due to automatic type inference assuming you want to return a cloned Cow<'_, str>, which is of course not possible. this is why i took the original test functions and turned them into closures, where it's less obvious that it's trying to return the wrong type.

r? davidtwco

(because you reviewed the last attempt)
(also, let me know if i should squash this down to one commit and add myself as the co-contributor)

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 31, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 31, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Mar 31, 2026
@ada4a
Copy link
Copy Markdown
Contributor

ada4a commented Apr 1, 2026

(just a drive-by comment) in order for a code block inside Details to work correctly, you need to leave an empty line before and after the code block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad diagnostic with Cow and to_owned where into_owned was meant

6 participants