Skip to content

Incorrect diagnostic suggestion for borrowed macro expression #58815

Closed
@euclio

Description

@euclio

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions