Skip to content

unused_variable suggets removing struct field from destructive binding which obv. does not work like this #105028

Closed
@matthiaskrgr

Description

@matthiaskrgr
Member

Given the following code:

struct Struct {
    x: isize,
    y: isize
}

fn main() {
    match (Struct { x: 239, y: 240 }) {
        // ignored field
        Struct { x: unused, .. } => {}
    }
}

The current output is:

warning: unused variable: `unused`
 --> src/main.rs:9:21
  |
9 |         Struct { x: unused, .. } => {}
  |                     ^^^^^^-
  |                     |
  |                     help: try removing the field
  |
  = note: `#[warn(unused_variables)]` on by default

warning: field `y` is never read
 --> src/main.rs:3:5
  |
1 | struct Struct {
  |        ------ field in this struct
2 |     x: isize,
3 |     y: isize
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

The suggestion is questionable because Struct { x: .. } is not valid rust code, we should probably suggest removing the x: as well..?

Errors of the code rustc suggested:

The following errors were reported:
error: `..` patterns are not allowed here
 --> src/main.rs:9:22
  |
9 |         Struct { x:  .. } => {}
  |                      ^^
  |
  = note: only allowed in tuple, tuple struct, and slice patterns

error[E0027]: pattern does not mention field `y`
 --> src/main.rs:9:9
  |
9 |         Struct { x:  .. } => {}
  |         ^^^^^^^^^^^^^^^^^ missing field `y`
  |
help: include the missing field in the pattern
  |
9 |         Struct { x:  .., y } => {}
  |                        ~~~~~
help: if you don't care about this missing field, you can explicitly ignore it
  |
9 |         Struct { x:  .., .. } => {}
  |                        ~~~~~~

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0027`.
Original diagnostics will follow.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Nov 28, 2022
chenyukang

chenyukang commented on Dec 2, 2022

@chenyukang
Member

@rustbot claim

added a commit that references this issue on Dec 2, 2022

fix rust-lang#105028, Only suggest removing struct field from destruc…

0e24cee
added a commit that references this issue on Dec 6, 2022

Rollup merge of rust-lang#105174 - chenyukang:yukang/fix-105028-unuse…

90d84ce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @chenyukang@matthiaskrgr

    Issue actions

      unused_variable suggets removing struct field from destructive binding which obv. does not work like this · Issue #105028 · rust-lang/rust