-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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() | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit: more common name for variables of |
||||||
| && adtdef.did() == cow | ||||||
| { | ||||||
| if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) { | ||||||
| if let Some(pos) = snippet.rfind(".to_owned") { | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not happy about detecting the use of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
|
|
||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd personally move this test to |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // issue #144792 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use full links and special comment |
||
|
|
||
| fn main() { | ||
| _ = std::env::var_os("RUST_LOG").map_or("warn".into(), |x| x.to_string_lossy().to_owned()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()` | ||
| } | ||
| 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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: likewise, more typical naming convention