about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-08-06 18:49:33 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2017-08-06 18:49:33 +0200
commit09420fc2060e08e332efd00098cda6447285290d (patch)
treed159f32adabd2b247caa75f2173faee6c1a8bd07 /src
parent90f54d00d313178a6246566a5f09cd6af1f7a099 (diff)
Fix union unused fields check
Diffstat (limited to 'src')
-rw-r--r--src/librustc/middle/dead.rs25
-rw-r--r--src/test/ui/union-fields.stderr22
2 files changed, 28 insertions, 19 deletions
diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs
index c82cfb34496..6532cde9715 100644
--- a/src/librustc/middle/dead.rs
+++ b/src/librustc/middle/dead.rs
@@ -190,20 +190,18 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
         self.inherited_pub_visibility = had_inherited_pub_visibility;
     }
 
-    fn mark_as_used_if_union(&mut self, did: DefId) {
+    fn mark_as_used_if_union(&mut self, did: DefId, fields: &hir::HirVec<hir::Field>) {
         if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
-            match self.tcx.hir.find(node_id) {
-                Some(hir_map::NodeItem(item)) => match item.node {
-                    Item_::ItemUnion(ref variant, _) => {
-                        if variant.fields().len() > 1 {
-                            for field in variant.fields() {
+            if let Some(hir_map::NodeItem(item)) = self.tcx.hir.find(node_id) {
+                if let Item_::ItemUnion(ref variant, _) = item.node {
+                    if variant.fields().len() > 1 {
+                        for field in variant.fields() {
+                            if fields.iter().find(|x| x.name.node == field.name).is_some() {
                                 self.live_symbols.insert(field.id);
                             }
                         }
                     }
-                    _ => {}
-                },
-                _ => {}
+                }
             }
         }
     }
@@ -239,11 +237,6 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
             hir::ExprPath(ref qpath @ hir::QPath::TypeRelative(..)) => {
                 let def = self.tables.qpath_def(qpath, expr.id);
                 self.handle_definition(def);
-                self.mark_as_used_if_union(def.def_id());
-            }
-            hir::ExprPath(ref qpath @ hir::QPath::Resolved(..)) => {
-                let def = self.tables.qpath_def(qpath, expr.id);
-                self.mark_as_used_if_union(def.def_id());
             }
             hir::ExprMethodCall(..) => {
                 self.lookup_and_handle_method(expr.id);
@@ -254,6 +247,10 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
             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);
+            }
             _ => ()
         }
 
diff --git a/src/test/ui/union-fields.stderr b/src/test/ui/union-fields.stderr
index d0f1a921425..5c47ba388a4 100644
--- a/src/test/ui/union-fields.stderr
+++ b/src/test/ui/union-fields.stderr
@@ -1,8 +1,8 @@
-error: field is never used: `y`
-  --> $DIR/union-fields.rs:20:5
+error: field is never used: `c`
+  --> $DIR/union-fields.rs:16:5
    |
-20 |     y: u32,
-   |     ^^^^^^
+16 |     c: u8, // should be reported
+   |     ^^^^^
    |
 note: lint level defined here
   --> $DIR/union-fields.rs:11:9
@@ -10,5 +10,17 @@ note: lint level defined here
 11 | #![deny(dead_code)]
    |         ^^^^^^^^^
 
-error: aborting due to previous error
+error: field is never used: `a`
+  --> $DIR/union-fields.rs:19:5
+   |
+19 |     a: u8, // should be reported
+   |     ^^^^^
+
+error: field is never used: `a`
+  --> $DIR/union-fields.rs:23:20
+   |
+23 | union NoDropLike { a: u8 } // should be reported as unused
+   |                    ^^^^^
+
+error: aborting due to 3 previous errors