Skip to content

Suggest the correct name if _ is used in a struct literal #98282

Open
@scottmcm

Description

@scottmcm

Given the following code:

mod blah {
    pub struct Stuff { x: i32 }
    pub fn do_stuff(_: Stuff) {}
}

fn main() {
    blah::do_stuff(_ { x: 10 });
}

The current output is:

error: struct literal body without path
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                      ^^^^^^^^^
  |
help: you might have forgotten to add the struct literal inside the block
  |
7 |     blah::do_stuff(_ { SomeStruct { x: 10 } });
  |                      ++++++++++++           +

error: expected one of `)`, `,`, `.`, `?`, or an operator, found `{`
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                     -^ expected one of `)`, `,`, `.`, `?`, or an operator
  |                     |
  |                     help: missing `,`

warning: unnecessary braces around function argument
 --> src/main.rs:7:22
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                      ^        ^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
7 -     blah::do_stuff(_ { x: 10 });
7 +     blah::do_stuff(_ { x: 10 });
  |

error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/main.rs:7:20
  |
7 |     blah::do_stuff(_ { x: 10 });
  |                    ^ `_` not allowed here

error[[E0061]](https://doc.rust-lang.org/nightly/error-index.html#E0061): this function takes 1 argument but 2 arguments were supplied
 --> src/main.rs:7:5
  |
7 |     blah::do_stuff(_ { x: 10 });
  |     ^^^^^^^^^^^^^^   --------- argument unexpected
  |
note: function defined here
 --> src/main.rs:3:12
  |
3 |     pub fn do_stuff(_: Stuff) {}
  |            ^^^^^^^^ --------
help: remove the extra argument
  |
7 |     blah::do_stuff(_);
  |     ~~~~~~~~~~~~~~~~~

Ideally the output should look like:

error[E0121]: the placeholder `_` is not allowed in struct literals
 --> src/main.rs:5:22
  |
9 |     blah::do_stuff(_ { x: 10 });
  |                    ^
  |                    |
  |                    not allowed in struct literals
  |                    help: replace with the correct struct type: `blah::Stuff`

Like how rustc will suggest the correct type if you use -> _ in a function definition.

(Opened after this thread: https://internals.rust-lang.org/t/enum-path-inference-with-variant-syntax/16851/4?u=scottmcm )

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-parserArea: The lexing & parsing of Rust source code to an ASTD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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