-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #65830 - Quantumplation:master, r=davidtwco,estebank
Use ident.span instead of def_span in dead-code pass Hello! First time contributor! :) This should fix #58729. According to @estebank in the duplicate #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.
- Loading branch information
Showing
49 changed files
with
139 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
src/test/ui/fail-no-dead-code.stderr → src/test/ui/lint/dead-code/basic.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
...t/lint-dead-code-empty-unused-enum.stderr → ...i/lint/dead-code/empty-unused-enum.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
.../ui/lint/lint-dead-code-impl-trait.stderr → src/test/ui/lint/dead-code/impl-trait.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![deny(dead_code)] | ||
|
||
fn unused() { //~ error: function is never used: | ||
println!("blah"); | ||
} | ||
|
||
fn unused2(var: i32) { //~ error: function is never used: | ||
println!("foo {}", var); | ||
} | ||
|
||
fn unused3( //~ error: function is never used: | ||
var: i32, | ||
) { | ||
println!("bar {}", var); | ||
} | ||
|
||
fn main() { | ||
println!("Hello world!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error: function is never used: `unused` | ||
--> $DIR/newline-span.rs:3:4 | ||
| | ||
LL | fn unused() { | ||
| ^^^^^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/newline-span.rs:1:9 | ||
| | ||
LL | #![deny(dead_code)] | ||
| ^^^^^^^^^ | ||
|
||
error: function is never used: `unused2` | ||
--> $DIR/newline-span.rs:7:4 | ||
| | ||
LL | fn unused2(var: i32) { | ||
| ^^^^^^^ | ||
|
||
error: function is never used: `unused3` | ||
--> $DIR/newline-span.rs:11:4 | ||
| | ||
LL | fn unused3( | ||
| ^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
.../ui/lint/lint-dead-code-type-alias.stderr → src/test/ui/lint/dead-code/type-alias.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
14 changes: 7 additions & 7 deletions
14
...ui/lint/lint-dead-code-unused-enum.stderr → ...test/ui/lint/dead-code/unused-enum.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
error: struct is never constructed: `F` | ||
--> $DIR/lint-dead-code-unused-enum.rs:3:1 | ||
--> $DIR/unused-enum.rs:3:8 | ||
| | ||
LL | struct F; | ||
| ^^^^^^^^^ | ||
| ^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/lint-dead-code-unused-enum.rs:1:9 | ||
--> $DIR/unused-enum.rs:1:9 | ||
| | ||
LL | #![deny(unused)] | ||
| ^^^^^^ | ||
= note: `#[deny(dead_code)]` implied by `#[deny(unused)]` | ||
|
||
error: struct is never constructed: `B` | ||
--> $DIR/lint-dead-code-unused-enum.rs:4:1 | ||
--> $DIR/unused-enum.rs:4:8 | ||
| | ||
LL | struct B; | ||
| ^^^^^^^^^ | ||
| ^ | ||
|
||
error: enum is never used: `E` | ||
--> $DIR/lint-dead-code-unused-enum.rs:6:1 | ||
--> $DIR/unused-enum.rs:6:6 | ||
| | ||
LL | enum E { | ||
| ^^^^^^ | ||
| ^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...lint/lint-dead-code-unused-variant.stderr → ...nt/dead-code/unused-struct-variant.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...est/ui/lint/lint-dead-code-variant.stderr → ...t/ui/lint/dead-code/unused-variant.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
src/test/ui/fail-no-dead-code-core.stderr → .../ui/lint/dead-code/with-core-crate.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.