Skip to content

False-positive "field is never used" warning with unions #43393

Closed
@kennytm

Description

@kennytm

(This has already been reported in #32836 (comment) but not as a proper issue, and I still encountered this today, so filed a separated issue here to ensure it is still visible.)

Test case:

union U {
    x: u32,
    y: f32,
}
fn main() {
    let u = U { x: 0x3f800000 };
    let _f = unsafe { u.y };
}

This produces an unused field warning, which is incorrect:

warning: field is never used: `x`
 --> src/main.rs:2:5
  |
2 |     x: u32,
  |     ^^^^^^
  |
  = note: #[warn(dead_code)] on by default

The initialization of field x should also be considered a use.


Repro on playground, all of stable (1.19.0), beta (1.20.0-beta.1) and nightly (1.20.0-nightly ae98ebf 2017-07-20).

Metadata

Metadata

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-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