Skip to content

When pattern doesn't match the type of if let expression, see if a field would be of the correct type and provide an apprpriate structured suggestion #81222

Closed
@estebank

Description

@estebank

Just encountered the following output:

error[E0308]: mismatched types
   --> compiler/rustc_passes/src/loops.rs:152:40
    |
152 |   ...                   if let hir::ExprKind::Path(hir::QPath::Resolved(
    |  ______________________________^
153 | | ...                       None,
154 | | ...                       Path { ident, res: hir::def::Res::Err, .. },
155 | | ...                   )) = break_expr
    | |                        ^   ---------- this expression has type `&&rustc_hir::Expr<'_>`
    | |________________________|
    |                          expected struct `rustc_hir::Expr`, found enum `rustc_hir::ExprKind`

It would be nice if we could peek at the fields of break_expr and provide an appropriate structured suggestion:

error[E0308]: mismatched types
   --> compiler/rustc_passes/src/loops.rs:152:40
    |
152 |   ...                   if let hir::ExprKind::Path(hir::QPath::Resolved(
    |  ______________________________^
153 | | ...                       None,
154 | | ...                       Path { ident, res: hir::def::Res::Err, .. },
155 | | ...                   )) = break_expr
    | |                        ^   ---------- this expression has type `&&rustc_hir::Expr<'_>`
    | |________________________|
    |                          expected struct `rustc_hir::Expr`, found enum `rustc_hir::ExprKind`
    |
help: you might have meant to use field `kind` of type `rustc_hir::ExprKind`
    |
155 | ...                   )) = break_expr.kind
    |                                      ^^^^^

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-type-systemArea: Type systemD-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