Dead-code pass highlights too much of impl functions

This commit is contained in:
mibac138 2020-05-06 10:01:20 +02:00
parent f8d394e518
commit b6d5d1fb79
4 changed files with 66 additions and 4 deletions

View file

@ -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)

View file

@ -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

View file

@ -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() {}

View file

@ -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