Skip to content

Commit 09420fc

Browse files
Fix union unused fields check
1 parent 90f54d0 commit 09420fc

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/librustc/middle/dead.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,18 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
190190
self.inherited_pub_visibility = had_inherited_pub_visibility;
191191
}
192192

193-
fn mark_as_used_if_union(&mut self, did: DefId) {
193+
fn mark_as_used_if_union(&mut self, did: DefId, fields: &hir::HirVec<hir::Field>) {
194194
if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
195-
match self.tcx.hir.find(node_id) {
196-
Some(hir_map::NodeItem(item)) => match item.node {
197-
Item_::ItemUnion(ref variant, _) => {
198-
if variant.fields().len() > 1 {
199-
for field in variant.fields() {
195+
if let Some(hir_map::NodeItem(item)) = self.tcx.hir.find(node_id) {
196+
if let Item_::ItemUnion(ref variant, _) = item.node {
197+
if variant.fields().len() > 1 {
198+
for field in variant.fields() {
199+
if fields.iter().find(|x| x.name.node == field.name).is_some() {
200200
self.live_symbols.insert(field.id);
201201
}
202202
}
203203
}
204-
_ => {}
205-
},
206-
_ => {}
204+
}
207205
}
208206
}
209207
}
@@ -239,11 +237,6 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
239237
hir::ExprPath(ref qpath @ hir::QPath::TypeRelative(..)) => {
240238
let def = self.tables.qpath_def(qpath, expr.id);
241239
self.handle_definition(def);
242-
self.mark_as_used_if_union(def.def_id());
243-
}
244-
hir::ExprPath(ref qpath @ hir::QPath::Resolved(..)) => {
245-
let def = self.tables.qpath_def(qpath, expr.id);
246-
self.mark_as_used_if_union(def.def_id());
247240
}
248241
hir::ExprMethodCall(..) => {
249242
self.lookup_and_handle_method(expr.id);
@@ -254,6 +247,10 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
254247
hir::ExprTupField(ref lhs, idx) => {
255248
self.handle_tup_field_access(&lhs, idx.node);
256249
}
250+
hir::ExprStruct(ref qpath, ref fields, _) => {
251+
let def = self.tables.qpath_def(qpath, expr.id);
252+
self.mark_as_used_if_union(def.def_id(), fields);
253+
}
257254
_ => ()
258255
}
259256

src/test/ui/union-fields.stderr

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
error: field is never used: `y`
2-
--> $DIR/union-fields.rs:20:5
1+
error: field is never used: `c`
2+
--> $DIR/union-fields.rs:16:5
33
|
4-
20 | y: u32,
5-
| ^^^^^^
4+
16 | c: u8, // should be reported
5+
| ^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/union-fields.rs:11:9
99
|
1010
11 | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: field is never used: `a`
14+
--> $DIR/union-fields.rs:19:5
15+
|
16+
19 | a: u8, // should be reported
17+
| ^^^^^
18+
19+
error: field is never used: `a`
20+
--> $DIR/union-fields.rs:23:20
21+
|
22+
23 | union NoDropLike { a: u8 } // should be reported as unused
23+
| ^^^^^
24+
25+
error: aborting due to 3 previous errors
1426

0 commit comments

Comments
 (0)