Skip to content
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

Incorrect diagnostic suggestion for borrowed macro expression #58815

Closed
euclio opened this issue Feb 28, 2019 · 0 comments
Closed

Incorrect diagnostic suggestion for borrowed macro expression #58815

euclio opened this issue Feb 28, 2019 · 0 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`.

Comments

@euclio
Copy link
Contributor

euclio commented Feb 28, 2019

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d0c29d2300d97268198951d9ea5252d7

For the following code:

fn main() {
    let s: String = &String::from("hello");
    let r: String = &format!("hello");
}

We get the following diagnostics:

error[E0308]: mismatched types
 --> src/main.rs:2:21
  |
2 |     let s: String = &String::from("hello");
  |                     ^^^^^^^^^^^^^^^^^^^^^^
  |                     |
  |                     expected struct `std::string::String`, found reference
  |                     help: consider removing the borrow: `String::from("hello")`
  |
  = note: expected type `std::string::String`
             found type `&std::string::String`

error[E0308]: mismatched types
 --> src/main.rs:3:21
  |
3 |     let r: String = &format!("hello");
  |                     ^^^^^^^^^^^^^^^^^
  |                     |
  |                     expected struct `std::string::String`, found reference
  |                     help: try using a conversion method: `&format!("hello").to_string()`
  |
  = note: expected type `std::string::String`
             found type `&std::string::String`

error: aborting due to 2 previous errors

Notice that the String expression correctly suggests removing the borrow, but the format! expression incorrectly suggests adding to_string(), which does not fix the error.

@euclio euclio changed the title Incorrect diagnostic suggestion for borrowed macro result Incorrect diagnostic suggestion for borrowed macro expression Feb 28, 2019
@estebank estebank added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. label Feb 28, 2019
estebank added a commit to estebank/rust that referenced this issue Mar 2, 2019
pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 8, 2019
Suggest removal of `&` when borrowing macro and appropriate

Fix rust-lang#58815.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`.
Projects
None yet
Development

No branches or pull requests

2 participants