Skip to content

compiler suggests invalid code with ref pattern #54180

Closed
@ehuss

Description

@ehuss

The following example:

struct S {
    f1: i32,
}

fn main() {
    let s = S { f1: 123 };
    let S { ref f1 } = s;
}

Gives the following suggestion:

warning: unused variable: `f1`
 --> src/main.rs:9:15
  |
9 |     let S{ref f1} = s;
  |               ^^ help: try ignoring the field: `f1: _`
  |
  = note: #[warn(unused_variables)] on by default

When the suggestion is applied, the code does not compile:

error: expected `,`
 --> src/main.rs:9:17
  |
9 |     let S { ref f1: _ } = s;
  |                 ^^

error[E0027]: pattern does not mention field `f1`
 --> src/main.rs:9:9
  |
9 |     let S { ref f1: _ } = s;
  |         ^^^^^^^^^^^^^^^ missing field `f1`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0027`.

I'm not sure what the best suggestion would be. Removing the ref might be an option (I'm not sure if using a wildcard is equivalent in this case?). Suggesting .. is another option.

This suggestion was changed in 1.28 (previous versions gave a different, wrong suggestion). Tested up to rustc 1.30.0-nightly (2d4e34c 2018-09-09).

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