Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
matthiaskrgr opened this issue Nov 28, 2022 · 1 comment · Fixed by #105174
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Nov 28, 2022

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.
@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Nov 28, 2022
@chenyukang
Copy link
Member

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2022
…d, r=eholk

Suggest removing struct field from destructive binding only in shorthand scenario

Fixes rust-lang#105028
@bors bors closed this as completed in 0e24cee Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants