Skip to content

Offer a suggested fix for structs with a single field #138931

@Wilfred

Description

@Wilfred

Code

struct Person {
    name: String,
}

fn main() {
    Person("Wilfred".to_owned());
}

Current output

error[E0423]: expected function, tuple struct or tuple variant, found struct `Person`
 --> src/main.rs:6:5
  |
1 | / struct Person {
2 | |     name: String,
3 | | }
  | |_- `Person` defined here
...
6 |       Person("Wilfred".to_owned());
  |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `Person { name: val }`

Desired output

Ideally rustc would offer a fix here of Person { name: "Wilfred".to_owned() }.

Rationale and extra context

When there's only one field in a struct, this is probably what the user meant to write.

Rust Version

$ rustc --version --verbose 
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

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