Skip to content

Diagnostic assumes that braced unresolved identifiers are formatting arguments #141350

@fmease

Description

@fmease

Uplifted from #141213 (comment). Given

struct Type { field: i32 }

impl Type {
    fn method(&self) { {field} }
}

the compiler outputs

error[E0425]: cannot find value `field` in this scope
 --> file.rs:4:25
  |
4 |     fn method(&self) { {field} }
  |                         ^^^^^
  |
  = help: you might have meant to use the available field in a format string: `"{}", self.field`

warning: unnecessary braces around block return value
 --> file.rs:4:24
  |
4 |     fn method(&self) { {field} }
  |                        ^     ^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
4 -     fn method(&self) { {field} }
4 +     fn method(&self) { field }
  |

error: aborting due to 1 previous error; 1 warning emitted

Notice the (unstructured) suggestion you might have meant to use the available field in a format string: `"{}", self.field` which is obviously incorrect, there's no format string in sight and fn method(&self) { "{}", self.field; } is butchered.

For context, this regressed in PR #141213.

Version Information

rustc 1.89.0-nightly (c43786c9b 2025-05-21)
binary: rustc
commit-hash: c43786c9b7b8d8dcc3f9c604e0e3074c16ed69d3
commit-date: 2025-05-21
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-papercutDiagnostics: An error or lint that needs small tweaks.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