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 · Fixed by #139014
Open

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

Wilfred opened this issue Mar 25, 2025 · 1 comment · Fixed by #139014
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

jhpratt added a commit to jhpratt/rust that referenced this issue Mar 27, 2025
Improve suggest construct with literal syntax instead of calling

Closing rust-lang#138931

When constructing a structure through a format similar to calling a constructor, we can use verbose suggestions to hint at using literal syntax for clearer advice. The case of multiple fields is also considered here, provided that the field has the same number of arguments as CallExpr.

r? compiler
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 27, 2025
Rollup merge of rust-lang#139014 - xizheyin:issue-138931, r=oli-obk

Improve suggest construct with literal syntax instead of calling

Closing rust-lang#138931

When constructing a structure through a format similar to calling a constructor, we can use verbose suggestions to hint at using literal syntax for clearer advice. The case of multiple fields is also considered here, provided that the field has the same number of arguments as CallExpr.

r? compiler
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

Successfully merging a pull request may close this issue.

2 participants