Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
Applicability::MaybeIncorrect,
);
}

if let Some(cow) = tcx.get_diagnostic_item(sym::Cow)
&& let ty::Adt(adtdef, _) = return_ty.kind()
&& adtdef.did() == cow
{
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) {
if let Some(pos) = snippet.rfind(".to_owned") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy about detecting the use of .to_owned by way of string comparison, but since this check is in the MIR I don't know how to do it better. Constructive criticism would be greatly appreciated!

let byte_pos = BytePos(pos as u32 + 1u32);
let to_owned_span = return_span.with_hi(return_span.lo() + byte_pos);
err.span_suggestion_short(
to_owned_span.shrink_to_hi(),
"try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`",
"in",
Applicability::MaybeIncorrect,
);
}
}
}
}

Err(err)
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/errors/cow-to-owned.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally move this test to suggestions/ or borrowck/ and about name... I think name is fine but we can do better right? So i'd suggest something like cow-into-owned-suggestion.rs

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// issue #144792
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use full links and special comment //! Regression test for: https://github.com/rust-lang/rust/issues/144792 just because it will be easy to follow with just copy past link


fn main() {
_ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().to_owned());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be fine but I don't like it's env dependable, can we have more interesting test like below and with more coverage

// More robust - doesn't depend on environment
fn test_cow_suggestion() -> String {
    let os_string = std::ffi::OsString::from("test");
    os_string.to_string_lossy().to_owned()
}

// Test multiple Cow scenarios
fn test_cow_from_str() -> String {
    let s = "hello";
    let cow = std::borrow::Cow::from(s);
    cow.to_owned() // Should suggest into_owned()
}

// Test with different Cow types
fn test_cow_bytes() -> Vec<u8> {
    let bytes = b"hello";
    let cow = std::borrow::Cow::from(&bytes[..]);
    cow.to_owned() // Should suggest into_owned()
}

//~^ ERROR cannot return value referencing function parameter
//~| HELP try using `.into_owned()`
}
17 changes: 17 additions & 0 deletions tests/ui/errors/cow-to-owned.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0515]: cannot return value referencing function parameter `x`
--> $DIR/cow-to-owned.rs:4:64
|
LL | _ = 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`
|
LL | _ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().into_owned());
| ++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0515`.
Loading