Skip to content

librustc: dont warn for inside of items when the parent is dead #29682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/lint-dead-code-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
34 changes: 32 additions & 2 deletions src/test/compile-fail/lint-dead-code-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
}