Skip to content

invalid suggestion: self.field cannot be used with format args capture #98241

Closed
@matthiaskrgr

Description

@matthiaskrgr

Given the following code:

struct A {
    foo: u32
}

impl A {
    fn func(&self) -> String {
        format!("{foo}")
    }
}

The current output is:

error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `foo` in this scope
 --> src/lib.rs:7:19
  |
7 |         format!("{foo}")
  |                   ^^^ help: you might have meant to use the available field: `self.foo`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

Note that {self.foo} is not accepted (yet..?) by the compiler, so we should suggest something like format!("{}", self.foo) I guess?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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