Skip to content

Commit 94890d2

Browse files
Use ident instead of def_span in dead-code pass
According to @estebank, def_span scans forward on the line until it finds a {, and if it can't find one, fallse back to the span for the whole item. This was apparently written before the identifier span was explicitly tracked on each node. This means that if an unused function signature spans multiple lines, the entire function (potentially hundreds of lines) gets flagged as dead code. This could, for example, cause IDEs to add error squiggly's to the whole function. By using the span from the ident instead, we narrow the scope of this in most cases. In a wider sense, it's probably safe to use ident.span instead of def_span in most locations throughout the whole code base, but since this is my first contribution, I kept it small. Some interesting points that came up while I was working on this: - I reorganized the tests a bit to bring some of the dead code ones all into the same location - A few tests were for things unrelated to dead code (like the path-lookahead for parens), so I added #![allow(dead_code)] and cleaned up the stderr file to reduce noise in the future - The same fix doesn't apply to const and static declarations. I tried adding these cases to the match expression, but that created a much wider change to tests and error messages, so I left it off until I could get some code review to validate the approach.
1 parent cb5fd4e commit 94890d2

21 files changed

+114
-98
lines changed

src/librustc_passes/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
578578
hir::ItemKind::Struct(..) |
579579
hir::ItemKind::Union(..) |
580580
hir::ItemKind::Trait(..) |
581-
hir::ItemKind::Impl(..) => self.tcx.sess.source_map().def_span(item.span),
581+
hir::ItemKind::Impl(..) => item.ident.span,
582582
_ => item.span,
583583
};
584584
let participle = match item.kind {

src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | #![plugin(lint_plugin_test)]
77
= note: `#[warn(deprecated)]` on by default
88

99
warning: function is never used: `lintme`
10-
--> $DIR/lint-plugin-cmdline-allow.rs:10:1
10+
--> $DIR/lint-plugin-cmdline-allow.rs:10:4
1111
|
1212
LL | fn lintme() { }
13-
| ^^^^^^^^^^^
13+
| ^^^^^^
1414
|
1515
note: lint level defined here
1616
--> $DIR/lint-plugin-cmdline-allow.rs:7:9

src/test/ui-fulldeps/lint-tool-cmdline-allow.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ LL | fn lintme() {}
1919
= note: `#[warn(clippy::test_lint)]` on by default
2020

2121
warning: function is never used: `lintme`
22-
--> $DIR/lint-tool-cmdline-allow.rs:10:1
22+
--> $DIR/lint-tool-cmdline-allow.rs:10:4
2323
|
2424
LL | fn lintme() {}
25-
| ^^^^^^^^^^^
25+
| ^^^^^^
2626
|
2727
note: lint level defined here
2828
--> $DIR/lint-tool-cmdline-allow.rs:7:9

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: function is never used: `foo`
2-
--> $DIR/basic.rs:4:1
2+
--> $DIR/basic.rs:4:4
33
|
44
LL | fn foo() {
5-
| ^^^^^^^^
5+
| ^^^
66
|
77
note: lint level defined here
88
--> $DIR/basic.rs:1:9

src/test/ui/lint/dead-code/empty-unused-enum.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: enum is never used: `E`
2-
--> $DIR/empty-unused-enum.rs:3:1
2+
--> $DIR/empty-unused-enum.rs:3:6
33
|
44
LL | enum E {}
5-
| ^^^^^^
5+
| ^
66
|
77
note: lint level defined here
88
--> $DIR/empty-unused-enum.rs:1:9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: struct is never constructed: `Bar`
2-
--> $DIR/lint-dead-code-1.rs:12:5
2+
--> $DIR/lint-dead-code-1.rs:12:16
33
|
44
LL | pub struct Bar;
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-dead-code-1.rs:5:9
@@ -23,16 +23,16 @@ LL | const priv_const: isize = 0;
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

2525
error: struct is never constructed: `PrivStruct`
26-
--> $DIR/lint-dead-code-1.rs:35:1
26+
--> $DIR/lint-dead-code-1.rs:35:8
2727
|
2828
LL | struct PrivStruct;
29-
| ^^^^^^^^^^^^^^^^^^
29+
| ^^^^^^^^^^
3030

3131
error: enum is never used: `priv_enum`
32-
--> $DIR/lint-dead-code-1.rs:64:1
32+
--> $DIR/lint-dead-code-1.rs:64:6
3333
|
3434
LL | enum priv_enum { foo2, bar2 }
35-
| ^^^^^^^^^^^^^^
35+
| ^^^^^^^^^
3636

3737
error: variant is never constructed: `bar3`
3838
--> $DIR/lint-dead-code-1.rs:67:5
@@ -41,28 +41,28 @@ LL | bar3
4141
| ^^^^
4242

4343
error: function is never used: `priv_fn`
44-
--> $DIR/lint-dead-code-1.rs:88:1
44+
--> $DIR/lint-dead-code-1.rs:88:4
4545
|
4646
LL | fn priv_fn() {
47-
| ^^^^^^^^^^^^
47+
| ^^^^^^^
4848

4949
error: function is never used: `foo`
50-
--> $DIR/lint-dead-code-1.rs:93:1
50+
--> $DIR/lint-dead-code-1.rs:93:4
5151
|
5252
LL | fn foo() {
53-
| ^^^^^^^^
53+
| ^^^
5454

5555
error: function is never used: `bar`
56-
--> $DIR/lint-dead-code-1.rs:98:1
56+
--> $DIR/lint-dead-code-1.rs:98:4
5757
|
5858
LL | fn bar() {
59-
| ^^^^^^^^
59+
| ^^^
6060

6161
error: function is never used: `baz`
62-
--> $DIR/lint-dead-code-1.rs:102:1
62+
--> $DIR/lint-dead-code-1.rs:102:4
6363
|
6464
LL | fn baz() -> impl Copy {
65-
| ^^^^^^^^^^^^^^^^^^^^^
65+
| ^^^
6666

6767
error: aborting due to 10 previous errors
6868

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: function is never used: `dead_fn`
2-
--> $DIR/lint-dead-code-2.rs:22:1
2+
--> $DIR/lint-dead-code-2.rs:22:4
33
|
44
LL | fn dead_fn() {}
5-
| ^^^^^^^^^^^^
5+
| ^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-dead-code-2.rs:2:9
@@ -11,16 +11,16 @@ LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

1313
error: function is never used: `dead_fn2`
14-
--> $DIR/lint-dead-code-2.rs:25:1
14+
--> $DIR/lint-dead-code-2.rs:25:4
1515
|
1616
LL | fn dead_fn2() {}
17-
| ^^^^^^^^^^^^^
17+
| ^^^^^^^^
1818

1919
error: function is never used: `main`
20-
--> $DIR/lint-dead-code-2.rs:38:1
20+
--> $DIR/lint-dead-code-2.rs:38:4
2121
|
2222
LL | fn main() {
23-
| ^^^^^^^^^
23+
| ^^^^
2424

2525
error: aborting due to 3 previous errors
2626

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: struct is never constructed: `Foo`
2-
--> $DIR/lint-dead-code-3.rs:13:1
2+
--> $DIR/lint-dead-code-3.rs:13:8
33
|
44
LL | struct Foo;
5-
| ^^^^^^^^^^^
5+
| ^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-dead-code-3.rs:3:9
@@ -17,16 +17,16 @@ LL | fn foo(&self) {
1717
| ^^^^^^^^^^^^^
1818

1919
error: function is never used: `bar`
20-
--> $DIR/lint-dead-code-3.rs:20:1
20+
--> $DIR/lint-dead-code-3.rs:20:4
2121
|
2222
LL | fn bar() {
23-
| ^^^^^^^^
23+
| ^^^
2424

2525
error: enum is never used: `c_void`
26-
--> $DIR/lint-dead-code-3.rs:59:1
26+
--> $DIR/lint-dead-code-3.rs:59:6
2727
|
2828
LL | enum c_void {}
29-
| ^^^^^^^^^^^
29+
| ^^^^^^
3030

3131
error: foreign function is never used: `free`
3232
--> $DIR/lint-dead-code-3.rs:61:5

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ LL | | },
2727
| |_____^
2828

2929
error: enum is never used: `ABC`
30-
--> $DIR/lint-dead-code-4.rs:24:1
30+
--> $DIR/lint-dead-code-4.rs:24:6
3131
|
3232
LL | enum ABC {
33-
| ^^^^^^^^
33+
| ^^^
3434

3535
error: variant is never constructed: `I`
3636
--> $DIR/lint-dead-code-4.rs:36:5

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ LL | Variant6(isize),
2323
| ^^^^^^^^^^^^^^^
2424

2525
error: enum is never used: `Enum3`
26-
--> $DIR/lint-dead-code-5.rs:18:1
26+
--> $DIR/lint-dead-code-5.rs:18:6
2727
|
2828
LL | enum Enum3 {
29-
| ^^^^^^^^^^
29+
| ^^^^^
3030

3131
error: aborting due to 4 previous errors
3232

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![deny(dead_code)]
2+
3+
fn unused() { //~ error: function is never used:
4+
println!("blah");
5+
}
6+
7+
fn unused2(var: i32) { //~ error: function is never used:
8+
println!("foo {}", var);
9+
}
10+
11+
fn unused3( //~ error: function is never used:
12+
var: i32,
13+
) {
14+
println!("bar {}", var);
15+
}
16+
17+
fn main() {
18+
println!("Hello world!");
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: function is never used: `unused`
2+
--> $DIR/newline-span.rs:3:4
3+
|
4+
LL | fn unused() {
5+
| ^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/newline-span.rs:1:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: function is never used: `unused2`
14+
--> $DIR/newline-span.rs:7:4
15+
|
16+
LL | fn unused2(var: i32) {
17+
| ^^^^^^^
18+
19+
error: function is never used: `unused3`
20+
--> $DIR/newline-span.rs:11:4
21+
|
22+
LL | fn unused3(
23+
| ^^^^^^^
24+
25+
error: aborting due to 3 previous errors
26+
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: struct is never constructed: `F`
2-
--> $DIR/unused-enum.rs:3:1
2+
--> $DIR/unused-enum.rs:3:8
33
|
44
LL | struct F;
5-
| ^^^^^^^^^
5+
| ^
66
|
77
note: lint level defined here
88
--> $DIR/unused-enum.rs:1:9
@@ -12,16 +12,16 @@ LL | #![deny(unused)]
1212
= note: `#[deny(dead_code)]` implied by `#[deny(unused)]`
1313

1414
error: struct is never constructed: `B`
15-
--> $DIR/unused-enum.rs:4:1
15+
--> $DIR/unused-enum.rs:4:8
1616
|
1717
LL | struct B;
18-
| ^^^^^^^^^
18+
| ^
1919

2020
error: enum is never used: `E`
21-
--> $DIR/unused-enum.rs:6:1
21+
--> $DIR/unused-enum.rs:6:6
2222
|
2323
LL | enum E {
24-
| ^^^^^^
24+
| ^
2525

2626
error: aborting due to 3 previous errors
2727

src/test/ui/lint/dead-code/with-core-crate.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: function is never used: `foo`
2-
--> $DIR/with-core-crate.rs:7:1
2+
--> $DIR/with-core-crate.rs:7:4
33
|
44
LL | fn foo() {
5-
| ^^^^^^^^
5+
| ^^^
66
|
77
note: lint level defined here
88
--> $DIR/with-core-crate.rs:1:9

src/test/ui/path-lookahead.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// run-pass
2-
3-
#![warn(unused)]
2+
#![allow(dead_code)]
3+
#![warn(unused_parens)]
44

55
// Parser test for #37765
66

7-
fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
7+
fn with_parens<T: ToString>(arg: T) -> String {
88
return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
99
}
1010

11-
fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
11+
fn no_parens<T: ToString>(arg: T) -> String {
1212
return <T as ToString>::to_string(&arg);
1313
}
1414

src/test/ui/path-lookahead.stderr

+2-22
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@ LL | return (<T as ToString>::to_string(&arg));
77
note: lint level defined here
88
--> $DIR/path-lookahead.rs:3:9
99
|
10-
LL | #![warn(unused)]
11-
| ^^^^^^
12-
= note: `#[warn(unused_parens)]` implied by `#[warn(unused)]`
13-
14-
warning: function is never used: `with_parens`
15-
--> $DIR/path-lookahead.rs:7:1
16-
|
17-
LL | fn with_parens<T: ToString>(arg: T) -> String {
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
|
20-
note: lint level defined here
21-
--> $DIR/path-lookahead.rs:3:9
22-
|
23-
LL | #![warn(unused)]
24-
| ^^^^^^
25-
= note: `#[warn(dead_code)]` implied by `#[warn(unused)]`
26-
27-
warning: function is never used: `no_parens`
28-
--> $DIR/path-lookahead.rs:11:1
29-
|
30-
LL | fn no_parens<T: ToString>(arg: T) -> String {
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | #![warn(unused_parens)]
11+
| ^^^^^^^^^^^^^
3212

src/test/ui/span/macro-span-replacement.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
warning: struct is never constructed: `S`
2-
--> $DIR/macro-span-replacement.rs:7:14
2+
--> $DIR/macro-span-replacement.rs:7:12
33
|
44
LL | $b $a;
5-
| ^
6-
...
7-
LL | m!(S struct);
8-
| ------------- in this macro invocation
5+
| ^^
96
|
107
note: lint level defined here
118
--> $DIR/macro-span-replacement.rs:3:9

src/test/ui/span/unused-warning-point-at-signature.rs renamed to src/test/ui/span/unused-warning-point-at-identifier.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fn func() -> usize { //~ WARN function is never used
2020
3
2121
}
2222

23-
fn //~ WARN function is never used
24-
func_complete_span()
23+
fn
24+
func_complete_span() //~ WARN function is never used
2525
-> usize
2626
{
2727
3

0 commit comments

Comments
 (0)