diff options
| author | Kevin Butler <haqkrs@gmail.com> | 2015-11-07 22:07:13 +0000 |
|---|---|---|
| committer | Kevin Butler <haqkrs@gmail.com> | 2015-11-08 02:04:34 +0000 |
| commit | c0a3538315158696a8d128d8ada466b9c278090b (patch) | |
| tree | 6397de06db8989ba2b0fb3ab20fa92163253fb30 | |
| parent | 43b5d5e0de1dbbe655160915cc9a7bb56a68d86c (diff) | |
| download | rust-c0a3538315158696a8d128d8ada466b9c278090b.tar.gz rust-c0a3538315158696a8d128d8ada466b9c278090b.zip | |
librustc: dont warn for inside of items when the parent is dead
| -rw-r--r-- | src/librustc/middle/dead.rs | 23 | ||||
| -rw-r--r-- | src/test/compile-fail/lint-dead-code-3.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/lint-dead-code-4.rs | 34 |
3 files changed, 44 insertions, 15 deletions
diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index b4280f86c7d..fff080a70e6 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -539,19 +539,18 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { item.node.descriptive_variant() ); } else { - match item.node { - hir::ItemEnum(ref enum_def, _) => { - for variant in &enum_def.variants { - if self.should_warn_about_variant(&variant.node) { - self.warn_dead_code(variant.node.data.id(), variant.span, - variant.node.name, "variant"); - } - } - }, - _ => () - } + // Only continue if we didn't warn + visit::walk_item(self, item); + } + } + + fn visit_variant(&mut self, variant: &hir::Variant, g: &hir::Generics, id: ast::NodeId) { + if self.should_warn_about_variant(&variant.node) { + self.warn_dead_code(variant.node.data.id(), variant.span, + variant.node.name, "variant"); + } else { + visit::walk_variant(self, variant, g, id); } - visit::walk_item(self, item); } fn visit_foreign_item(&mut self, fi: &hir::ForeignItem) { diff --git a/src/test/compile-fail/lint-dead-code-3.rs b/src/test/compile-fail/lint-dead-code-3.rs index ba1b7f03b0f..28475b1ef8a 100644 --- a/src/test/compile-fail/lint-dead-code-3.rs +++ b/src/test/compile-fail/lint-dead-code-3.rs @@ -30,7 +30,7 @@ impl Foo { } fn bar() { //~ ERROR: function is never used - fn baz() {} //~ ERROR: function is never used + fn baz() {} Foo.foo(); baz(); diff --git a/src/test/compile-fail/lint-dead-code-4.rs b/src/test/compile-fail/lint-dead-code-4.rs index 8e37dd805d5..20cd13c1875 100644 --- a/src/test/compile-fail/lint-dead-code-4.rs +++ b/src/test/compile-fail/lint-dead-code-4.rs @@ -25,12 +25,41 @@ enum XYZ { X, //~ ERROR variant is never used Y { //~ ERROR variant is never used a: String, - b: i32, //~ ERROR: struct field is never used - c: i32, //~ ERROR: struct field is never used + b: i32, + c: i32, }, Z } +enum ABC { //~ ERROR enum is never used + A, + B { + a: String, + b: i32, + c: i32, + }, + C +} + +// ensure struct variants get warning for their fields +enum IJK { + I, //~ ERROR variant is never used + J { + a: String, + b: i32, //~ ERROR struct field is never used + c: i32, //~ ERROR struct field is never used + }, + K //~ ERROR variant is never used + +} + +fn struct_variant_partial_use(b: IJK) -> String { + match b { + IJK::J { a, b: _, .. } => a, + _ => "".to_string() + } +} + fn field_match_in_patterns(b: XYZ) -> String { match b { XYZ::Y { a, b: _, .. } => a, @@ -58,6 +87,7 @@ fn field_match_in_let(f: Bar) -> bool { fn main() { field_read(Foo { x: 1, b: false }); field_match_in_patterns(XYZ::Z); + struct_variant_partial_use(IJK::J { a: "".into(), b: 1, c: -1 }); field_match_in_let(Bar { x: 42, b: true, c: false, _guard: () }); let _ = Baz { x: 0 }; } |
