Skip to content

Commit f94157e

Browse files
Handle type aliases as well
1 parent 09420fc commit f94157e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/librustc/middle/dead.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,12 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
247247
hir::ExprTupField(ref lhs, idx) => {
248248
self.handle_tup_field_access(&lhs, idx.node);
249249
}
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);
250+
hir::ExprStruct(_, ref fields, _) => {
251+
if let ty::TypeVariants::TyAdt(ref def, _) = self.tables.expr_ty(expr).sty {
252+
if def.is_union() {
253+
self.mark_as_used_if_union(def.did, fields);
254+
}
255+
}
253256
}
254257
_ => ()
255258
}

src/test/ui/union-fields.rs

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ union U2 {
2222
}
2323
union NoDropLike { a: u8 } // should be reported as unused
2424

25+
union U {
26+
a: u8, // should not be reported
27+
b: u8, // should not be reported
28+
c: u8, // should be reported
29+
}
30+
type A = U;
31+
2532
fn main() {
2633
let u = U1 { a: 0 };
2734
let _a = unsafe { u.b };
@@ -30,4 +37,6 @@ fn main() {
3037
let _b = unsafe { u.b };
3138

3239
let _u = NoDropLike { a: 10 };
40+
let u = A { a: 0 };
41+
let _b = unsafe { u.b };
3342
}

src/test/ui/union-fields.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@ error: field is never used: `a`
2222
23 | union NoDropLike { a: u8 } // should be reported as unused
2323
| ^^^^^
2424

25-
error: aborting due to 3 previous errors
25+
error: field is never used: `c`
26+
--> $DIR/union-fields.rs:28:5
27+
|
28+
28 | c: u8, // should be reported
29+
| ^^^^^
30+
31+
error: aborting due to 4 previous errors
2632

0 commit comments

Comments
 (0)