Skip to content

Commit fe278a8

Browse files
authored
Auto merge of #36252 - joshtriplett:union-field-never-used, r=sanxiyn
Fix "field is never used" warning to take unions into account When compiling code containing a union with an unused field, rustc says "struct field is never used". Rather than saying "struct or union", or adding logic to determine the type of the item, just change the message to "field is never used", dropping the "struct". Update tests accordingly.
2 parents 3b272bf + 046c7f2 commit fe278a8

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
548548
fn visit_struct_field(&mut self, field: &hir::StructField) {
549549
if self.should_warn_about_field(&field) {
550550
self.warn_dead_code(field.id, field.span,
551-
field.name, "struct field");
551+
field.name, "field");
552552
}
553553

554554
intravisit::walk_struct_field(self, field);

src/test/compile-fail/lint-dead-code-4.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
struct Foo {
1616
x: usize,
17-
b: bool, //~ ERROR: struct field is never used
17+
b: bool, //~ ERROR: field is never used
1818
}
1919

2020
fn field_read(f: Foo) -> usize {
@@ -46,8 +46,8 @@ enum IJK {
4646
I, //~ ERROR variant is never used
4747
J {
4848
a: String,
49-
b: i32, //~ ERROR struct field is never used
50-
c: i32, //~ ERROR struct field is never used
49+
b: i32, //~ ERROR field is never used
50+
c: i32, //~ ERROR field is never used
5151
},
5252
K //~ ERROR variant is never used
5353

@@ -68,9 +68,9 @@ fn field_match_in_patterns(b: XYZ) -> String {
6868
}
6969

7070
struct Bar {
71-
x: usize, //~ ERROR: struct field is never used
71+
x: usize, //~ ERROR: field is never used
7272
b: bool,
73-
c: bool, //~ ERROR: struct field is never used
73+
c: bool, //~ ERROR: field is never used
7474
_guard: ()
7575
}
7676

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(untagged_unions)]
12+
#![deny(dead_code)]
13+
14+
union Foo {
15+
x: usize,
16+
b: bool, //~ ERROR: field is never used
17+
_unused: u16,
18+
}
19+
20+
fn field_read(f: Foo) -> usize {
21+
unsafe { f.x }
22+
}
23+
24+
fn main() {
25+
let _ = field_read(Foo { x: 2 });
26+
}

0 commit comments

Comments
 (0)