Skip to content

Incorrect suggestion for extra & in pattern #106182

Closed
@RalfJung

Description

@RalfJung

Given the following code:

struct S(u32, Vec<i32>);

fn foo(x: &S) {
    match x {
        S(& (mut y), v) => {
        }
    }
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/lib.rs:5:11
  |
4 |     match x {
  |           - this expression has type `&S`
5 |         S(& (mut y), v) => {
  |           ^^^^^^^^^ expected `u32`, found reference
  |
  = note:   expected type `u32`
          found reference `&_`
help: consider removing `&` from the pattern
  |
5 -         S(& (mut y), v) => {
5 +         S(mut y), v) => {
  |

Note that the suggestion is wrong, it removes the ( but left in the matching ). Applying that suggestion leads to invalid code:

struct S(u32, Vec<i32>);

fn foo(x: &S) {
    match x {
        S(mut y), v) => {
        }
    }
}

It should instead suggest

5 -         S(& (mut y), v) => {
5 +         S(mut y, v) => {

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in 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