Skip to content

Commit 41f7c2d

Browse files
committed
apply review suggestions and move Cow sym to rustc
1 parent beee9e7 commit 41f7c2d

7 files changed

Lines changed: 70 additions & 28 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,9 +3531,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
35313531
);
35323532
}
35333533

3534-
if let Some(cow) = tcx.get_diagnostic_item(sym::Cow)
3535-
&& let ty::Adt(adtdef, _) = return_ty.kind()
3536-
&& adtdef.did() == cow
3534+
if let Some(cow_did) = tcx.get_diagnostic_item(sym::Cow)
3535+
&& let ty::Adt(adt_def, _) = return_ty.kind()
3536+
&& adt_def.did() == cow_did
35373537
{
35383538
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) {
35393539
if let Some(pos) = snippet.rfind(".to_owned") {

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ symbols! {
196196
Continue,
197197
ControlFlow,
198198
Copy,
199+
Cow,
199200
Debug,
200201
Default,
201202
Deref,

src/tools/clippy/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,

tests/ui/errors/cow-to-owned.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/ui/errors/cow-to-owned.stderr

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Regression test for: https://github.com/rust-lang/rust/issues/144792
2+
3+
fn main() {
4+
let _ = || {
5+
let os_string = std::ffi::OsString::from("test");
6+
os_string.to_string_lossy().to_owned()
7+
//~^ ERROR: cannot return value referencing local variable `os_string` [E0515]
8+
};
9+
10+
let _ = || {
11+
let s = "hello".to_owned();
12+
let cow = std::borrow::Cow::from(&s);
13+
cow.to_owned()
14+
//~^ ERROR: cannot return value referencing local variable `s` [E0515]
15+
};
16+
17+
let _ = || {
18+
let bytes = b"hello".to_owned();
19+
let cow = std::borrow::Cow::from(&bytes[..]);
20+
cow.to_owned()
21+
//~^ ERROR: cannot return value referencing local variable `bytes` [E0515]
22+
};
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0515]: cannot return value referencing local variable `os_string`
2+
--> $DIR/cow-into-owned-suggestion.rs:6:9
3+
|
4+
LL | os_string.to_string_lossy().to_owned()
5+
| ---------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| returns a value referencing data owned by the current function
8+
| `os_string` is borrowed here
9+
|
10+
help: try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`
11+
|
12+
LL | os_string.to_string_lossy().into_owned()
13+
| ++
14+
15+
error[E0515]: cannot return value referencing local variable `s`
16+
--> $DIR/cow-into-owned-suggestion.rs:13:9
17+
|
18+
LL | let cow = std::borrow::Cow::from(&s);
19+
| -- `s` is borrowed here
20+
LL | cow.to_owned()
21+
| ^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
22+
|
23+
help: try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`
24+
|
25+
LL | cow.into_owned()
26+
| ++
27+
28+
error[E0515]: cannot return value referencing local variable `bytes`
29+
--> $DIR/cow-into-owned-suggestion.rs:20:9
30+
|
31+
LL | let cow = std::borrow::Cow::from(&bytes[..]);
32+
| ----- `bytes` is borrowed here
33+
LL | cow.to_owned()
34+
| ^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
35+
|
36+
help: try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`
37+
|
38+
LL | cow.into_owned()
39+
| ++
40+
41+
error: aborting due to 3 previous errors
42+
43+
For more information about this error, try `rustc --explain E0515`.

0 commit comments

Comments
 (0)