Skip to content

Commit 0cd12d3

Browse files
authored
Rollup merge of rust-lang#65830 - Quantumplation:master, r=davidtwco
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 0498da3 + 94890d2 commit 0cd12d3

Some content is hidden

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

50 files changed

+128
-112
lines changed

src/librustc_passes/dead.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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
| ^^^^^^^^^

0 commit comments

Comments
 (0)