Skip to content

Commit 03cf0e9

Browse files
committed
refactor: emit "unused assoc fn" in lexical order
with repect to other dead code lints
1 parent a29dada commit 03cf0e9

File tree

5 files changed

+87
-30
lines changed

5 files changed

+87
-30
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,13 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
836836
let module_items = tcx.hir_module_items(module);
837837

838838
for item in module_items.items() {
839+
if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind {
840+
for item in impl_item.items {
841+
visitor.check_definition(item.id.owner_id.def_id);
842+
}
843+
continue;
844+
}
845+
839846
if !live_symbols.contains(&item.owner_id.def_id) {
840847
let parent = tcx.local_parent(item.owner_id.def_id);
841848
if parent != module && !live_symbols.contains(&parent) {
@@ -900,10 +907,6 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
900907
}
901908
}
902909

903-
for impl_item in module_items.impl_items() {
904-
visitor.check_definition(impl_item.owner_id.def_id);
905-
}
906-
907910
for foreign_item in module_items.foreign_items() {
908911
visitor.check_definition(foreign_item.owner_id.def_id);
909912
}

tests/ui/lint/dead-code/issue-85255.stderr

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@ note: the lint level is defined here
1414
LL | #![warn(dead_code)]
1515
| ^^^^^^^^^
1616

17-
warning: fields `a` and `b` are never read
18-
--> $DIR/issue-85255.rs:19:5
19-
|
20-
LL | pub(crate) struct Foo1 {
21-
| ---- fields in this struct
22-
LL | a: i32,
23-
| ^
24-
LL | pub b: i32,
25-
| ^
26-
27-
warning: fields `a` and `b` are never read
28-
--> $DIR/issue-85255.rs:31:5
29-
|
30-
LL | pub(crate) struct Foo2 {
31-
| ---- fields in this struct
32-
LL | a: i32,
33-
| ^
34-
LL | pub b: i32,
35-
| ^
36-
3717
warning: method `a` is never used
3818
--> $DIR/issue-85255.rs:14:8
3919
|
@@ -46,6 +26,16 @@ warning: method `b` is never used
4626
LL | pub fn b(&self) -> i32 { 6 }
4727
| ^
4828

29+
warning: fields `a` and `b` are never read
30+
--> $DIR/issue-85255.rs:19:5
31+
|
32+
LL | pub(crate) struct Foo1 {
33+
| ---- fields in this struct
34+
LL | a: i32,
35+
| ^
36+
LL | pub b: i32,
37+
| ^
38+
4939
warning: method `a` is never used
5040
--> $DIR/issue-85255.rs:26:8
5141
|
@@ -58,6 +48,16 @@ warning: method `b` is never used
5848
LL | pub fn b(&self) -> i32 { 6 }
5949
| ^
6050

51+
warning: fields `a` and `b` are never read
52+
--> $DIR/issue-85255.rs:31:5
53+
|
54+
LL | pub(crate) struct Foo2 {
55+
| ---- fields in this struct
56+
LL | a: i32,
57+
| ^
58+
LL | pub b: i32,
59+
| ^
60+
6161
warning: method `a` is never used
6262
--> $DIR/issue-85255.rs:38:8
6363
|

tests/ui/lint/dead-code/lint-dead-code-3.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ note: the lint level is defined here
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

13+
error: method `foo` is never used
14+
--> $DIR/lint-dead-code-3.rs:16:8
15+
|
16+
LL | fn foo(&self) {
17+
| ^^^
18+
1319
error: function `bar` is never used
1420
--> $DIR/lint-dead-code-3.rs:21:4
1521
|
@@ -34,12 +40,6 @@ error: function `blah` is never used
3440
LL | fn blah() {}
3541
| ^^^^
3642

37-
error: method `foo` is never used
38-
--> $DIR/lint-dead-code-3.rs:16:8
39-
|
40-
LL | fn foo(&self) {
41-
| ^^^
42-
4343
error: function `free` is never used
4444
--> $DIR/lint-dead-code-3.rs:62:8
4545
|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![deny(unused)]
2+
3+
struct Foo;
4+
5+
impl Foo {
6+
fn one() {}
7+
//~^ ERROR associated function `one` is never used [dead_code]
8+
9+
fn two(&self) {}
10+
//~^ ERROR method `two` is never used [dead_code]
11+
12+
// seperation between functions
13+
// ...
14+
// ...
15+
16+
fn used() {}
17+
18+
fn three(&self) {
19+
//~^ ERROR method `three` is never used [dead_code]
20+
Foo::one();
21+
// ...
22+
}
23+
}
24+
25+
fn main() {
26+
Foo::used();
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: associated function `one` is never used
2+
--> $DIR/unused-assoc-fns.rs:6:8
3+
|
4+
LL | fn one() {}
5+
| ^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-assoc-fns.rs:1:9
9+
|
10+
LL | #![deny(unused)]
11+
| ^^^^^^
12+
= note: `#[deny(dead_code)]` implied by `#[deny(unused)]`
13+
14+
error: method `two` is never used
15+
--> $DIR/unused-assoc-fns.rs:9:8
16+
|
17+
LL | fn two(&self) {}
18+
| ^^^
19+
20+
error: method `three` is never used
21+
--> $DIR/unused-assoc-fns.rs:18:8
22+
|
23+
LL | fn three(&self) {
24+
| ^^^^^
25+
26+
error: aborting due to 3 previous errors
27+

0 commit comments

Comments
 (0)