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

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
Contributor

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

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

// issue #144792

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
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
4 participants