Skip to content

Improve suggestion for field initializer on tuple struct #99885

Closed
@ryanavella

Description

@ryanavella

Given

struct Foo(());

fn bar(x: Foo) -> Foo {
    Foo(0: x.0 as _)
}

The output contains this suggestion, among others

error: casts cannot be followed by a field access
 --> src/lib.rs:4:9
  |
4 |     Foo(0: x.0 as _)
  |         ^^^^
  |
help: try surrounding the expression in parentheses
  |
4 |     Foo((0: x).0 as _)
  |         +    +
help: alternatively, remove the type ascription
  |
4 -     Foo(0: x.0 as _)
4 +     Foo(0.0 as _)
  |

Both the initial error message and the recommendation are confusing. Ideally we should recommend removing 0: since tuple structs do not use field initializers.

Metadata

Metadata

Assignees

No one assigned

    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