Skip to content

Improve syntax diagnostics or suggestions with enum vs struct #75770

Closed
@rossmacarthur

Description

@rossmacarthur

I changed an enum to a struct but forgot to change the enum keyword to a struct keyword, it took me a while to realize what the problem was because the error is unclear.

enum used where code looks like a struct

pub enum NotEnum {
    field: u8   
}

The error is

error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
 --> src/lib.rs:2:10
  |
2 |     field: u8,    
  |          ^ expected one of `(`, `,`, `=`, `{`, or `}`

error: expected item, found `:`
 --> src/lib.rs:2:10
  |
2 |     field: u8,    
  |          ^ expected item

A suggestion saying help: change `enum` to `struct` would be useful.

struct used where code looks like an enum

pub struct NotStruct {
    Variant
}
error: expected `:`, found `}`
 --> src/lib.rs:3:1
  |
2 |     Variant
  |            - expected `:`
3 | }
  | ^ unexpected token

A suggestion saying help: change `struct` to `enum` would be useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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