From b3011dd711ea8bf4bcb04d96aff814863eff3973 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 28 Mar 2019 01:45:50 +0000 Subject: [PATCH 1/3] Visit ItemKind::Impl for dead code lint --- src/librustc/middle/dead.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index f12367a89bb..cd1fa8763c3 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -172,6 +172,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { | hir::ItemKind::Ty(..) | hir::ItemKind::Static(..) | hir::ItemKind::Existential(..) + | hir::ItemKind::Impl(..) | hir::ItemKind::Const(..) => { intravisit::walk_item(self, &item); } From 40db5756b435d2e67a834475833f4752486672e0 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 28 Mar 2019 01:47:39 +0000 Subject: [PATCH 2/3] Add a regression test for #47131 --- src/test/ui/dead-code-impl.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/ui/dead-code-impl.rs diff --git a/src/test/ui/dead-code-impl.rs b/src/test/ui/dead-code-impl.rs new file mode 100644 index 00000000000..84829c98e57 --- /dev/null +++ b/src/test/ui/dead-code-impl.rs @@ -0,0 +1,17 @@ +// run-pass + +#![deny(dead_code)] + +pub struct GenericFoo(T); + +type Foo = GenericFoo; + +impl Foo { + fn bar(self) -> u8 { + 0 + } +} + +fn main() { + println!("{}", GenericFoo(0).bar()); +} From 8cdfad91539dea8c9ec7d22c08f40e42e25e6ff0 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 28 Mar 2019 23:33:17 +0000 Subject: [PATCH 3/3] Walk all items for dead_code lint by default --- src/librustc/middle/dead.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index cd1fa8763c3..dd080d9ee26 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -73,7 +73,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { Def::Const(_) | Def::AssociatedConst(..) | Def::TyAlias(_) => { self.check_def_id(def.def_id()); } - _ if self.in_pat => (), + _ if self.in_pat => {}, Def::PrimTy(..) | Def::SelfTy(..) | Def::SelfCtor(..) | Def::Local(..) | Def::Upvar(..) => {} Def::Ctor(ctor_def_id, CtorOf::Variant, ..) => { @@ -91,6 +91,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { self.check_def_id(variant_id); } } + Def::ToolMod | Def::NonMacroAttr(..) | Def::Err => {} _ => { self.check_def_id(def.def_id()); } @@ -166,17 +167,13 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { } hir::ItemKind::Enum(..) => { self.inherited_pub_visibility = item.vis.node.is_pub(); + intravisit::walk_item(self, &item); } - hir::ItemKind::Fn(..) - | hir::ItemKind::Ty(..) - | hir::ItemKind::Static(..) - | hir::ItemKind::Existential(..) - | hir::ItemKind::Impl(..) - | hir::ItemKind::Const(..) => { + hir::ItemKind::ForeignMod(..) => {} + _ => { intravisit::walk_item(self, &item); } - _ => () } } Node::TraitItem(trait_item) => { @@ -188,7 +185,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { Node::ForeignItem(foreign_item) => { intravisit::walk_foreign_item(self, &foreign_item); } - _ => () + _ => {} } self.repr_has_repr_c = had_repr_c; self.inherited_pub_visibility = had_inherited_pub_visibility;