]> git.lizzy.rs Git - rust.git/commitdiff
Fix union unused fields check
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 6 Aug 2017 16:49:33 +0000 (18:49 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 6 Aug 2017 16:49:33 +0000 (18:49 +0200)
src/librustc/middle/dead.rs
src/test/ui/union-fields.stderr

index c82cfb344967110feccdd6816f08d6e8eec40df1..6532cde9715b0fdffbc7d182e3597914422a8989 100644 (file)
@@ -190,20 +190,18 @@ fn visit_node(&mut self, node: &hir_map::Node<'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 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
             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 @@ 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);
+            }
             _ => ()
         }
 
index d0f1a9214255b4df42bc0a11145d3d4162b9c956..5c47ba388a45289e029040dc64489dd307d004b4 100644 (file)
@@ -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