diff options
| author | Bastian Kauschke <bastian_kauschke@hotmail.de> | 2020-09-22 21:25:32 +0200 |
|---|---|---|
| committer | Bastian Kauschke <bastian_kauschke@hotmail.de> | 2020-09-22 22:13:58 +0200 |
| commit | 438d229b25921e0b1d4b731586cff27a2e79e99f (patch) | |
| tree | 572759421485cc03f45b9cb2fd453d37b2b6a7ad | |
| parent | e0bc267512fc0cb49c86978192857e8187017f0b (diff) | |
| download | rust-438d229b25921e0b1d4b731586cff27a2e79e99f.tar.gz rust-438d229b25921e0b1d4b731586cff27a2e79e99f.zip | |
dead_code: look at trait impls even if they don't contain items
| -rw-r--r-- | compiler/rustc_passes/src/dead.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/const-generics/issues/issue-70225.rs | 21 | ||||
| -rw-r--r-- | src/test/ui/lint/dead-code/trait-impl.rs | 19 |
3 files changed, 44 insertions, 1 deletions
diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index fe6653e98da..98ded4189cf 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -369,7 +369,7 @@ fn has_allow_dead_code_or_lang_attr( // - This is because lang items are always callable from elsewhere. // or // 2) We are not sure to be live or not -// * Implementation of a trait method +// * Implementations of traits and trait methods struct LifeSeeder<'k, 'tcx> { worklist: Vec<hir::HirId>, krate: &'k hir::Crate<'k>, @@ -415,6 +415,9 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> { } } hir::ItemKind::Impl { ref of_trait, items, .. } => { + if of_trait.is_some() { + self.worklist.push(item.hir_id); + } for impl_item_ref in items { let impl_item = self.krate.impl_item(impl_item_ref.id); if of_trait.is_some() diff --git a/src/test/ui/const-generics/issues/issue-70225.rs b/src/test/ui/const-generics/issues/issue-70225.rs new file mode 100644 index 00000000000..8f8d753d0a7 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-70225.rs @@ -0,0 +1,21 @@ +// check-pass +#![feature(const_generics)] +#![allow(incomplete_features)] +#![deny(dead_code)] + +// We previously incorrectly linted `L` as unused here. +const L: usize = 3; + +fn main() { + let p = Printer {}; + p.print(); +} + +trait Print<const N: usize> { + fn print(&self) -> usize { + 3 + } +} + +struct Printer {} +impl Print<L> for Printer {} diff --git a/src/test/ui/lint/dead-code/trait-impl.rs b/src/test/ui/lint/dead-code/trait-impl.rs new file mode 100644 index 00000000000..92e389a938a --- /dev/null +++ b/src/test/ui/lint/dead-code/trait-impl.rs @@ -0,0 +1,19 @@ +// check-pass +#![deny(dead_code)] + +enum Foo { + Bar, +} + +fn main() { + let p = [0; 0]; + p.bar(); +} + +trait Bar { + fn bar(&self) -> usize { + 3 + } +} + +impl Bar for [u32; Foo::Bar as usize] {} |
