-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
fn foo(_s: Option<&str>) {
}
fn main() {
let s = Some("".to_string());
foo(s);
}
// AND:
fn foo(_s: Option<&str>) {
}
fn main() {
let s = Some("".to_string());
foo(s.map(|x| &*x));
}
Current output
error[E0308]: mismatched types
--> foo.rs:6:9
|
6 | foo(s);
| --- ^ expected `&str`, found struct `String`
| |
| arguments to this function are incorrect
|
= note: expected enum `Option<&str>`
found enum `Option<String>`
note: function defined here
--> foo.rs:1:4
|
1 | fn foo(_s: Option<&str>) {
| ^^^ ----------------
help: try converting the passed type into a `&str`
|
6 | foo(s.map(|x| &*x));
| +++++++++++++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
// AND:
error[E0515]: cannot return value referencing function parameter `x`
--> foo.rs:6:19
|
6 | foo(s.map(|x| &*x));
| ^^-
| | |
| | `x` is borrowed here
| returns a value referencing data owned by the current function
error: aborting due to previous error
For more information about this error, try `rustc --explain E0515`.
Desired output
I believe the help suggestion should be valid code. I was able to solve this error with:
fn foo(s: Option<&str>) {
}
fn main() {
let s = Some("".to_string());
foo(s.as_ref().map(AsRef::as_ref));
}
Rationale and extra context
rustc's help suggestions are often extremely helpful, but in this case the suggested change simply does not work and also doesn't provide any further help or explanation about why it doesn't work.
Other cases
No response
Anything else?
No response
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.