Skip to content

Destructuring structs with unused variable gives wrong warning. #56472

Closed
@orion78fr

Description

@orion78fr
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let points = vec!(Point { x: 1, y: 2 }, Point { x: 3, y: 4 });

    let y_sum: i32 = points.iter()
        .map(|Point { x, y }| y)
        .sum();
    println!("{}", y_sum);
}

gives this warning :

warning: unused variable: `x`
  --> src\main.rs:10:23
   |
10 |         .map(|Point { x, y }| y)
   |                       ^ help: consider using `_x` instead
   |
   = note: #[warn(unused_variables)] on by default

However, applying such changes gives these errors :

error[E0026]: struct `Point` does not have a field named `_x`
  --> src\main.rs:10:23
   |
10 |         .map(|Point { _x, y }| y)
   |                       ^^ struct `Point` does not have this field

error[E0027]: pattern does not mention field `x`
  --> src\main.rs:10:15
   |
10 |         .map(|Point { _x, y }| y)
   |               ^^^^^^^^^^^^^^^ missing field `x`

error: aborting due to 2 previous errors

I think the warning should be replaced with something that works (like x:_) or not issued at all.

$ rustc --version --verbose
rustc 1.30.1 (1433507eb 2018-11-07)
binary: rustc
commit-hash: 1433507eba7d1a114e4c6f27ae0e1a74f60f20de
commit-date: 2018-11-07
host: x86_64-pc-windows-msvc
release: 1.30.1
LLVM version: 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions