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

Destructuring structs with unused variable gives wrong warning. #56472

Closed
orion78fr opened this issue Dec 3, 2018 · 1 comment
Closed

Destructuring structs with unused variable gives wrong warning. #56472

orion78fr opened this issue Dec 3, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@orion78fr
Copy link

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
@estebank
Copy link
Contributor

estebank commented Dec 3, 2018

CC #50303, @zackmdavis we need to extend #50854 to closure args too 😬.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. labels Dec 3, 2018
@zackmdavis zackmdavis self-assigned this Dec 3, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 8, 2019
Suggest appropriate code for unused field when destructuring pattern

Fix rust-lang#56472.
@zackmdavis zackmdavis removed their assignment Jun 16, 2019
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 C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants