Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Wilfred opened this issue Mar 25, 2025 · 1 comment
Open

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

Wilfred opened this issue Mar 25, 2025 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Mar 25, 2025

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

@Wilfred Wilfred added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 25, 2025
@xizheyin
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants