From b6d5d1fb7901b4c0fe4c9ffa2854f616fd60e541 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Wed, 6 May 2020 10:01:20 +0200 Subject: [PATCH] Dead-code pass highlights too much of impl functions --- src/librustc_passes/dead.rs | 14 ++++++-- .../ui/lint/dead-code/lint-dead-code-3.stderr | 4 +-- .../ui/lint/dead-code/lint-dead-code-6.rs | 20 ++++++++++++ .../ui/lint/dead-code/lint-dead-code-6.stderr | 32 +++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/lint/dead-code/lint-dead-code-6.rs create mode 100644 src/test/ui/lint/dead-code/lint-dead-code-6.stderr diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index 1b8c053b16e0b..22ff87efd2ede 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -589,7 +589,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { // FIXME(66095): Because item.span is annotated with things // like expansion data, and ident.span isn't, we use the // def_span method if it's part of a macro invocation - // (and thus has asource_callee set). + // (and thus has a source_callee set). // We should probably annotate ident.span with the macro // context, but that's a larger change. if item.span.source_callee().is_some() { @@ -653,7 +653,17 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { } hir::ImplItemKind::Fn(_, body_id) => { if !self.symbol_is_live(impl_item.hir_id) { - let span = self.tcx.sess.source_map().guess_head_span(impl_item.span); + // FIXME(66095): Because impl_item.span is annotated with things + // like expansion data, and ident.span isn't, we use the + // def_span method if it's part of a macro invocation + // (and thus has a source_callee set). + // We should probably annotate ident.span with the macro + // context, but that's a larger change. + let span = if impl_item.span.source_callee().is_some() { + self.tcx.sess.source_map().guess_head_span(impl_item.span) + } else { + impl_item.ident.span + }; self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used"); } self.visit_nested_body(body_id) diff --git a/src/test/ui/lint/dead-code/lint-dead-code-3.stderr b/src/test/ui/lint/dead-code/lint-dead-code-3.stderr index a2614a0bf74b3..6d174e8d9bc38 100644 --- a/src/test/ui/lint/dead-code/lint-dead-code-3.stderr +++ b/src/test/ui/lint/dead-code/lint-dead-code-3.stderr @@ -11,10 +11,10 @@ LL | #![deny(dead_code)] | ^^^^^^^^^ error: associated function is never used: `foo` - --> $DIR/lint-dead-code-3.rs:15:5 + --> $DIR/lint-dead-code-3.rs:15:8 | LL | fn foo(&self) { - | ^^^^^^^^^^^^^ + | ^^^ error: function is never used: `bar` --> $DIR/lint-dead-code-3.rs:20:4 diff --git a/src/test/ui/lint/dead-code/lint-dead-code-6.rs b/src/test/ui/lint/dead-code/lint-dead-code-6.rs new file mode 100644 index 0000000000000..0a543d5c6228d --- /dev/null +++ b/src/test/ui/lint/dead-code/lint-dead-code-6.rs @@ -0,0 +1,20 @@ +#![deny(dead_code)] + +struct UnusedStruct; //~ ERROR struct is never constructed: `UnusedStruct` +impl UnusedStruct { + fn unused_impl_fn_1() { //~ ERROR associated function is never used: `unused_impl_fn_1` + println!("blah"); + } + + fn unused_impl_fn_2(var: i32) { //~ ERROR associated function is never used: `unused_impl_fn_2` + println!("foo {}", var); + } + + fn unused_impl_fn_3( //~ ERROR associated function is never used: `unused_impl_fn_3` + var: i32, + ) { + println!("bar {}", var); + } +} + +fn main() {} diff --git a/src/test/ui/lint/dead-code/lint-dead-code-6.stderr b/src/test/ui/lint/dead-code/lint-dead-code-6.stderr new file mode 100644 index 0000000000000..7dc60730d6aad --- /dev/null +++ b/src/test/ui/lint/dead-code/lint-dead-code-6.stderr @@ -0,0 +1,32 @@ +error: struct is never constructed: `UnusedStruct` + --> $DIR/lint-dead-code-6.rs:3:8 + | +LL | struct UnusedStruct; + | ^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/lint-dead-code-6.rs:1:9 + | +LL | #![deny(dead_code)] + | ^^^^^^^^^ + +error: associated function is never used: `unused_impl_fn_1` + --> $DIR/lint-dead-code-6.rs:5:8 + | +LL | fn unused_impl_fn_1() { + | ^^^^^^^^^^^^^^^^ + +error: associated function is never used: `unused_impl_fn_2` + --> $DIR/lint-dead-code-6.rs:9:8 + | +LL | fn unused_impl_fn_2(var: i32) { + | ^^^^^^^^^^^^^^^^ + +error: associated function is never used: `unused_impl_fn_3` + --> $DIR/lint-dead-code-6.rs:13:8 + | +LL | fn unused_impl_fn_3( + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors +