Skip to content

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

Open
@Kampfkarren

Description

@Kampfkarren
Contributor

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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 21, 2022
chenyukang

chenyukang commented on Aug 21, 2022

@chenyukang
Member

@rustbot claim

chenyukang

chenyukang commented on Aug 31, 2022

@chenyukang
Member

I spent some time to debug this issue. If the span is from a macro, it's difficult to get the right
diagonstic message, this message is removed by PR: #85937

Note there a special note: this error originates in the macro format

If we remove this check at:

if in_external_macro(sess, sp) {

The result is also not right:

error[E0308]: mismatched types
   --> /home/cat/rust/library/alloc/src/macros.rs:117:9
    |
114 | macro_rules! format {
    | ------------------- in this expansion of `format!`
...
117 |         res
    |         ^^^
    |         |
    |         expected `&str`, found struct `String`
    |         help: consider borrowing here: `&res`
    |
   ::: a.rs:5:15
    |
5   |     takes_str(format!("e"));
    |               ------------ in this macro invocation

error: aborting due to previous error

The res is from format!:

macro_rules! format {
    ($($arg:tt)*) => {{
        let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
        res
    }}
}
removed their assignment
on Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @chenyukang@Kampfkarren

        Issue actions

          "Consider borrowing here" message does not show up when argument is a format call · Issue #100830 · rust-lang/rust