Skip to content
Closed
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if let Some(cow) = tcx.get_diagnostic_item(sym::Cow)
if let Some(cow_did) = tcx.get_diagnostic_item(sym::Cow)

nit: likewise, more typical naming convention

&& let ty::Adt(adtdef, _) = return_ty.kind()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
&& let ty::Adt(adtdef, _) = return_ty.kind()
&& let ty::Adt(adt_def, _) = return_ty.kind()

nit: more common name for variables of AdtDef

&& 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
Copy Markdown
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!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are other diagnostics code here that does similar things, so happy to call it pre-existing until there's a better solution for this part of the compiler

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
Copy Markdown
Member

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
Copy Markdown
Member

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
Copy Markdown
Member

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()
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd adopt this as the test

//~^ 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