-
Notifications
You must be signed in to change notification settings - Fork 13.6k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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()); | ||
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()
} |
||
//~^ 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.
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!