Skip to content
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

Dead-code pass highlights too much of impl functions #71947

Merged
merged 1 commit into from
May 9, 2020
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
14 changes: 12 additions & 2 deletions src/librustc_passes/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/dead-code/lint-dead-code-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/lint/dead-code/lint-dead-code-6.rs
Original file line number Diff line number Diff line change
@@ -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() {}
32 changes: 32 additions & 0 deletions src/test/ui/lint/dead-code/lint-dead-code-6.stderr
Original file line number Diff line number Diff line change
@@ -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