]> git.lizzy.rs Git - rust.git/commitdiff
Handle type aliases as well
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 6 Aug 2017 18:46:32 +0000 (20:46 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 6 Aug 2017 18:46:32 +0000 (20:46 +0200)
src/librustc/middle/dead.rs
src/test/ui/union-fields.rs
src/test/ui/union-fields.stderr

index 6532cde9715b0fdffbc7d182e3597914422a8989..a525b4e13b78dd2b1cda110cc18da2d678f02e3d 100644 (file)
@@ -247,9 +247,12 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
             hir::ExprTupField(ref lhs, idx) => {
                 self.handle_tup_field_access(&lhs, idx.node);
             }
-            hir::ExprStruct(ref qpath, ref fields, _) => {
-                let def = self.tables.qpath_def(qpath, expr.id);
-                self.mark_as_used_if_union(def.def_id(), fields);
+            hir::ExprStruct(_, ref fields, _) => {
+                if let ty::TypeVariants::TyAdt(ref def, _) = self.tables.expr_ty(expr).sty {
+                    if def.is_union() {
+                        self.mark_as_used_if_union(def.did, fields);
+                    }
+                }
             }
             _ => ()
         }
index 7b39a548fe9d265c066985fc87c45ee7219f72ce..021f57e3eee0aa690218bdc4fa636a580e4a5d62 100644 (file)
@@ -22,6 +22,13 @@ union U2 {
 }
 union NoDropLike { a: u8 } // should be reported as unused
 
+union U {
+    a: u8, // should not be reported
+    b: u8, // should not be reported
+    c: u8, // should be reported
+}
+type A = U;
+
 fn main() {
     let u = U1 { a: 0 };
     let _a = unsafe { u.b };
@@ -30,4 +37,6 @@ fn main() {
     let _b = unsafe { u.b };
 
     let _u = NoDropLike { a: 10 };
+    let u = A { a: 0 };
+    let _b = unsafe { u.b };
 }
index 5c47ba388a45289e029040dc64489dd307d004b4..f3a2702d5aefa2266d5333e5069687b1578defe0 100644 (file)
@@ -22,5 +22,11 @@ error: field is never used: `a`
 23 | union NoDropLike { a: u8 } // should be reported as unused
    |                    ^^^^^
 
-error: aborting due to 3 previous errors
+error: field is never used: `c`
+  --> $DIR/union-fields.rs:28:5
+   |
+28 |     c: u8, // should be reported
+   |     ^^^^^
+
+error: aborting due to 4 previous errors