Skip to content

Commit 61a551b

Browse files
committed
Auto merge of rust-lang#65830 - Quantumplation:master, r=davidtwco,estebank
Use ident.span instead of def_span in dead-code pass Hello! First time contributor! :) This should fix rust-lang#58729. According to @estebank in the duplicate rust-lang#63064, def_span scans forward on the line until it finds a {, and if it can't find one, falls 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.
2 parents e8b190a + 6186ede commit 61a551b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+139
-109
lines changed

src/librustc_passes/dead.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,27 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
569569

570570
fn visit_item(&mut self, item: &'tcx hir::Item) {
571571
if self.should_warn_about_item(item) {
572-
// For items that have a definition with a signature followed by a
573-
// block, point only at the signature.
572+
// For most items, we want to highlight its identifier
574573
let span = match item.kind {
575574
hir::ItemKind::Fn(..) |
576575
hir::ItemKind::Mod(..) |
577576
hir::ItemKind::Enum(..) |
578577
hir::ItemKind::Struct(..) |
579578
hir::ItemKind::Union(..) |
580579
hir::ItemKind::Trait(..) |
581-
hir::ItemKind::Impl(..) => self.tcx.sess.source_map().def_span(item.span),
580+
hir::ItemKind::Impl(..) => {
581+
// FIXME(66095): Because item.span is annotated with things
582+
// like expansion data, and ident.span isn't, we use the
583+
// def_span method if it's part of a macro invocation
584+
// (and thus has asource_callee set).
585+
// We should probably annotate ident.span with the macro
586+
// context, but that's a larger change.
587+
if item.span.source_callee().is_some() {
588+
self.tcx.sess.source_map().def_span(item.span)
589+
} else {
590+
item.ident.span
591+
}
592+
},
582593
_ => item.span,
583594
};
584595
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/fail-no-dead-code.stderr renamed to src/test/ui/lint/dead-code/basic.stderr

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: enum is never used: `E`
2-
--> $DIR/lint-dead-code-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
8-
--> $DIR/lint-dead-code-empty-unused-enum.rs:1:9
8+
--> $DIR/empty-unused-enum.rs:1:9
99
|
1010
LL | #![deny(unused)]
1111
| ^^^^^^

src/test/ui/lint/lint-dead-code-impl-trait.stderr renamed to src/test/ui/lint/dead-code/impl-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: type alias is never used: `Unused`
2-
--> $DIR/lint-dead-code-impl-trait.rs:12:1
2+
--> $DIR/impl-trait.rs:12:1
33
|
44
LL | type Unused = ();
55
| ^^^^^^^^^^^^^^^^^
66
|
77
note: lint level defined here
8-
--> $DIR/lint-dead-code-impl-trait.rs:1:9
8+
--> $DIR/impl-trait.rs:1:9
99
|
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
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/lint-dead-code-3.stderr renamed to 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/lint-dead-code-4.stderr renamed to 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/lint-dead-code-5.stderr renamed to 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+

src/test/ui/lint/lint-dead-code-type-alias.stderr renamed to src/test/ui/lint/dead-code/type-alias.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: type alias is never used: `Unused`
2-
--> $DIR/lint-dead-code-type-alias.rs:4:1
2+
--> $DIR/type-alias.rs:4:1
33
|
44
LL | type Unused = u8;
55
| ^^^^^^^^^^^^^^^^^
66
|
77
note: lint level defined here
8-
--> $DIR/lint-dead-code-type-alias.rs:1:9
8+
--> $DIR/type-alias.rs:1:9
99
|
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
error: struct is never constructed: `F`
2-
--> $DIR/lint-dead-code-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
8-
--> $DIR/lint-dead-code-unused-enum.rs:1:9
8+
--> $DIR/unused-enum.rs:1:9
99
|
1010
LL | #![deny(unused)]
1111
| ^^^^^^
1212
= note: `#[deny(dead_code)]` implied by `#[deny(unused)]`
1313

1414
error: struct is never constructed: `B`
15-
--> $DIR/lint-dead-code-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/lint-dead-code-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/lint-dead-code-unused-variant.stderr renamed to src/test/ui/lint/dead-code/unused-struct-variant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: variant is never constructed: `Bar`
2-
--> $DIR/lint-dead-code-unused-variant.rs:8:5
2+
--> $DIR/unused-struct-variant.rs:8:5
33
|
44
LL | Bar(B),
55
| ^^^^^^
66
|
77
note: lint level defined here
8-
--> $DIR/lint-dead-code-unused-variant.rs:1:9
8+
--> $DIR/unused-struct-variant.rs:1:9
99
|
1010
LL | #![deny(unused)]
1111
| ^^^^^^

src/test/ui/lint/lint-dead-code-variant.stderr renamed to src/test/ui/lint/dead-code/unused-variant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: variant is never constructed: `Variant1`
2-
--> $DIR/lint-dead-code-variant.rs:5:5
2+
--> $DIR/unused-variant.rs:5:5
33
|
44
LL | Variant1,
55
| ^^^^^^^^
66
|
77
note: lint level defined here
8-
--> $DIR/lint-dead-code-variant.rs:1:9
8+
--> $DIR/unused-variant.rs:1:9
99
|
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^

src/test/ui/fail-no-dead-code-core.stderr renamed to src/test/ui/lint/dead-code/with-core-crate.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: function is never used: `foo`
2-
--> $DIR/fail-no-dead-code-core.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
8-
--> $DIR/fail-no-dead-code-core.rs:1:9
8+
--> $DIR/with-core-crate.rs:1:9
99
|
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^

0 commit comments

Comments
 (0)