Skip to content

Commit 32213c6

Browse files
Rollup merge of rust-lang#154646 - m4rch3n1ng:144792-cow-diag, r=davidtwco
Add suggestion to `.to_owned()` used on `Cow` when borrowing fixes rust-lang#144792 supersedes rust-lang#144925 with the review comments addressed the tests suggested from @Kivooeo from rust-lang#144925 (comment) didn't work entirely, because these tests failed due to error `[E0308]` mismatched types, which actually already provides a suggestion, that actually makes the code compile: ``` $ cargo check error[E0308]: mismatched types --> src/main.rs:3:5 | 1 | fn test_cow_suggestion() -> String { | ------ expected `std::string::String` because of return type 2 | let os_string = std::ffi::OsString::from("test"); 3 | os_string.to_string_lossy().to_owned() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Cow<'_, str>` | = note: expected struct `std::string::String` found enum `std::borrow::Cow<'_, str>` help: try using a conversion method | 3 | os_string.to_string_lossy().to_owned().to_string() | ++++++++++++ ``` now this suggestion is of course not good or efficient code, but via clippy with `-Wclippy::nursery` lint group you can actually get to the correct code, so i don't think this is too much of an issue: <details> <summary>the clippy suggestions</summary> ``` $ cargo clippy -- -Wclippy::nursery warning: this `to_owned` call clones the `Cow<'_, str>` itself and does not cause its contents to become owned --> src/main.rs:3:5 | 3 | os_string.to_string_lossy().to_owned().to_string() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#suspicious_to_owned = note: `#[warn(clippy::suspicious_to_owned)]` on by default help: depending on intent, either make the `Cow` an `Owned` variant | 3 | os_string.to_string_lossy().into_owned().to_string() | ++ help: or clone the `Cow` itself | 3 - os_string.to_string_lossy().to_owned().to_string() 3 + os_string.to_string_lossy().clone().to_string() | $ # apply first suggestion $ cargo c -- -Wclippy::nursery warning: redundant clone --> src/main.rs:3:45 | 3 | os_string.to_string_lossy().into_owned().to_string() | ^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use --> src/main.rs:3:5 | 3 | os_string.to_string_lossy().into_owned().to_string() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#redundant_clone = note: `-W clippy::redundant-clone` implied by `-W clippy::nursery` = help: to override `-W clippy::nursery` add `#[allow(clippy::redundant_clone)]` ``` </details> the actual error that we are looking for is error `[E0515]`, cannot return value referencing local variable, which was only present in the original issue due to automatic type inference assuming you want to return a cloned `Cow<'_, str>`, which is of course not possible. this is why i took the original test functions and turned them into closures, where it's less obvious that it's trying to return the wrong type. r? davidtwco (because you reviewed the last attempt) (also, let me know if i should squash this down to one commit and add myself as the co-contributor)
2 parents 1ddb9f2 + 697ae4a commit 32213c6

1 file changed

Lines changed: 0 additions & 1 deletion

File tree

clippy_utils/src/sym.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ generate! {
5050
Cargo_toml: "Cargo.toml",
5151
Child,
5252
Command,
53-
Cow,
5453
Current,
5554
DOUBLE_QUOTE: "\"",
5655
DebugStruct,

0 commit comments

Comments
 (0)