Skip to content

Bad diagnostic with Cow and to_owned where into_owned was meant #144792

Description

@edwloef

Code

fnmain(){
_ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().to_owned());}

Current output

error[E0515]: cannot return value referencing functionparameter`x`
--> src/main.rs:2:64
|
2 | _ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x|x.to_string_lossy().to_owned());
| -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
||| returns a value referencing data owned by the current function
|`x` is borrowed here
For more information about this error, try `rustc --explain E0515`.

Desired output

error[E0515]: cannot return value referencing functionparameter`x`
--> src/main.rs:2:64
|
2 | _ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x|x.to_string_lossy().to_owned());
| -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
||| returns a value referencing data owned by the current function
|`x` is borrowed here
|
help: Try using `.into_owned()`if you meant to convert a `Cow<'_, T>` to an owned `T` |2 | _ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().into_owned()); | ++For more information about this error, try `rustc --explain E0515`.

Rationale and extra context

The current error message isn't very helpful, and the two method names are very similar.

Other cases

Rust Version

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

Anything else?

No response

Activity

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions