Skip to content

"Consider borrowing here" message does not show up when argument is a format call #100830

Open
@Kampfkarren

Description

@Kampfkarren

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ee83cc45a558eccd6b784e17065e71a5

fn takes_str(_: &str) {}

fn main() {
    takes_str(String::from("e"));
    takes_str(format!("e"));
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:4:15
  |
4 |     takes_str(String::from("e"));
  |     --------- ^^^^^^^^^^^^^^^^^
  |     |         |
  |     |         expected `&str`, found struct `String`
  |     |         help: consider borrowing here: `&String::from("e")`
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn takes_str(_: &str) {}
  |    ^^^^^^^^^ -------

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:5:15
  |
5 |     takes_str(format!("e"));
  |               ^^^^^^^^^^^^ expected `&str`, found struct `String`
  |
  = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

Using String::from directly tells us we can borrow it with &. Using format! does not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions