Skip to content

Add self. on format!() macro failed #141136

Closed
@A4-Tacks

Description

@A4-Tacks

Code

struct Foo {
    x: i32
}
impl Foo {
    fn foo(&self) {
        let _ = format!("{x}");
    }
}

Current output

Checking test_ v0.1.0 (/home/lrne/Project/Rust/test_)
error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:6:27
  |
6 |         let _ = format!("{x}");
  |                           ^
  |
help: you might have meant to use the available field
  |
6 |         let _ = format!("{self.x}");
  |                           +++++

For more information about this error, try `rustc --explain E0425`.
error: could not compile `test_` (bin "test_") due to 1 previous error

Desired output

Checking test_ v0.1.0 (/home/lrne/Project/Rust/test_)
error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:6:27
  |
6 |         let _ = format!("{x}");
  |                           ^
  |
help: you might have meant to use the available field
  |
6 |         let _ = format!("{}", self.x);
  |                               ++++++

For more information about this error, try `rustc --explain E0425`.
error: could not compile `test_` (bin "test_") due to 1 previous error

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0-nightly (a15cce269 2025-04-17)
binary: rustc
commit-hash: a15cce2690e8fab72422515c9dc02c6fbc506733
commit-date: 2025-04-17
host: aarch64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

No response

Metadata

Metadata

Assignees

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