Skip to content

Commit b6d5d1f

Browse files
committed
Dead-code pass highlights too much of impl functions
1 parent f8d394e commit b6d5d1f

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

src/librustc_passes/dead.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
589589
// FIXME(66095): Because item.span is annotated with things
590590
// like expansion data, and ident.span isn't, we use the
591591
// def_span method if it's part of a macro invocation
592-
// (and thus has asource_callee set).
592+
// (and thus has a source_callee set).
593593
// We should probably annotate ident.span with the macro
594594
// context, but that's a larger change.
595595
if item.span.source_callee().is_some() {
@@ -653,7 +653,17 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
653653
}
654654
hir::ImplItemKind::Fn(_, body_id) => {
655655
if !self.symbol_is_live(impl_item.hir_id) {
656-
let span = self.tcx.sess.source_map().guess_head_span(impl_item.span);
656+
// FIXME(66095): Because impl_item.span is annotated with things
657+
// like expansion data, and ident.span isn't, we use the
658+
// def_span method if it's part of a macro invocation
659+
// (and thus has a source_callee set).
660+
// We should probably annotate ident.span with the macro
661+
// context, but that's a larger change.
662+
let span = if impl_item.span.source_callee().is_some() {
663+
self.tcx.sess.source_map().guess_head_span(impl_item.span)
664+
} else {
665+
impl_item.ident.span
666+
};
657667
self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used");
658668
}
659669
self.visit_nested_body(body_id)

src/test/ui/lint/dead-code/lint-dead-code-3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

1313
error: associated function is never used: `foo`
14-
--> $DIR/lint-dead-code-3.rs:15:5
14+
--> $DIR/lint-dead-code-3.rs:15:8
1515
|
1616
LL | fn foo(&self) {
17-
| ^^^^^^^^^^^^^
17+
| ^^^
1818

1919
error: function is never used: `bar`
2020
--> $DIR/lint-dead-code-3.rs:20:4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![deny(dead_code)]
2+
3+
struct UnusedStruct; //~ ERROR struct is never constructed: `UnusedStruct`
4+
impl UnusedStruct {
5+
fn unused_impl_fn_1() { //~ ERROR associated function is never used: `unused_impl_fn_1`
6+
println!("blah");
7+
}
8+
9+
fn unused_impl_fn_2(var: i32) { //~ ERROR associated function is never used: `unused_impl_fn_2`
10+
println!("foo {}", var);
11+
}
12+
13+
fn unused_impl_fn_3( //~ ERROR associated function is never used: `unused_impl_fn_3`
14+
var: i32,
15+
) {
16+
println!("bar {}", var);
17+
}
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: struct is never constructed: `UnusedStruct`
2+
--> $DIR/lint-dead-code-6.rs:3:8
3+
|
4+
LL | struct UnusedStruct;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-dead-code-6.rs:1:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: associated function is never used: `unused_impl_fn_1`
14+
--> $DIR/lint-dead-code-6.rs:5:8
15+
|
16+
LL | fn unused_impl_fn_1() {
17+
| ^^^^^^^^^^^^^^^^
18+
19+
error: associated function is never used: `unused_impl_fn_2`
20+
--> $DIR/lint-dead-code-6.rs:9:8
21+
|
22+
LL | fn unused_impl_fn_2(var: i32) {
23+
| ^^^^^^^^^^^^^^^^
24+
25+
error: associated function is never used: `unused_impl_fn_3`
26+
--> $DIR/lint-dead-code-6.rs:13:8
27+
|
28+
LL | fn unused_impl_fn_3(
29+
| ^^^^^^^^^^^^^^^^
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)