Skip to content

bad diagnostic suggestion for self access in a format string #105520

Open
@ehuss

Description

@ehuss

Given the following code:

struct Foo {
    value: i32
}

impl Foo {
    fn f(&self) -> String {
        format!("{value}")
    }
}

The current output is:

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

Applying the suggestion doesn't work since format strings don't allow non-identifier expressions. Fortunately applying the suggestion and compiling again will lead to another suggestion with the correct syntax. Ideally the original diagnostic should provide the correct syntax of format!("{}", self.value)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-fmtArea: `core::fmt`A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-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