Skip to content

Conversation

Periodic1911
Copy link
Contributor

@Periodic1911 Periodic1911 commented Aug 4, 2025

Fixes #144792

Adds a suggestion when using .to_borrow() on Cow where .into_borrow() should be used, and makes a test that explicitly checks if it is shown.

@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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 Aug 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2025

This PR modifies tests/ui/issues/. If this PR is adding new tests to tests/ui/issues/,
please refrain from doing so, and instead add it to more descriptive subdirectories.

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

Copy link
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

Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

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

Some test adjustments

We have a bit of a gap in our test coverage, so I'm trying to keep a close eye on new tests

Copy link
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

@@ -0,0 +1,7 @@
// issue #144792
Copy link
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

// issue #144792

fn main() {
_ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().to_owned());
Copy link
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
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

}

if let Some(cow) = tcx.get_diagnostic_item(sym::Cow)
&& let ty::Adt(adtdef, _) = return_ty.kind()
Copy link
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

);
}

if let Some(cow) = tcx.get_diagnostic_item(sym::Cow)
Copy link
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

&& 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
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

// issue #144792

fn main() {
_ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().to_owned());
Copy link
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

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 13, 2025
@Dylan-DPC
Copy link
Member

@Periodic1911 any updates on this? thanks

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

5 participants