Skip to content

Commit c41dcac

Browse files
committed
dead-code-lint: de-dup multiple unused assoc fns
1 parent 03cf0e9 commit c41dcac

File tree

8 files changed

+77
-72
lines changed

8 files changed

+77
-72
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,12 @@ impl<'tcx> DeadVisitor<'tcx> {
712712

713713
let parent_info = if let Some(parent_item) = parent_item {
714714
let parent_descr = tcx.def_descr(parent_item.to_def_id());
715-
Some(ParentInfo {
716-
num,
717-
descr,
718-
parent_descr,
719-
span: tcx.def_ident_span(parent_item).unwrap(),
720-
})
715+
let span = if let DefKind::Impl { .. } = tcx.def_kind(parent_item) {
716+
tcx.def_span(parent_item)
717+
} else {
718+
tcx.def_ident_span(parent_item).unwrap()
719+
};
720+
Some(ParentInfo { num, descr, parent_descr, span })
721721
} else {
722722
None
723723
};
@@ -800,16 +800,7 @@ impl<'tcx> DeadVisitor<'tcx> {
800800
}
801801

802802
fn check_definition(&mut self, def_id: LocalDefId) {
803-
if self.live_symbols.contains(&def_id) {
804-
return;
805-
}
806-
if has_allow_dead_code_or_lang_attr(self.tcx, def_id) {
807-
return;
808-
}
809-
let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else {
810-
return
811-
};
812-
if name.as_str().starts_with('_') {
803+
if self.is_live_code(def_id) {
813804
return;
814805
}
815806
match self.tcx.def_kind(def_id) {
@@ -827,6 +818,18 @@ impl<'tcx> DeadVisitor<'tcx> {
827818
_ => {}
828819
}
829820
}
821+
822+
fn is_live_code(&self, def_id: LocalDefId) -> bool {
823+
// if we cannot get a name for the item, then we just assume that it is
824+
// live. I mean, we can't really emit a lint.
825+
let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else {
826+
return true;
827+
};
828+
829+
self.live_symbols.contains(&def_id)
830+
|| has_allow_dead_code_or_lang_attr(self.tcx, def_id)
831+
|| name.as_str().starts_with('_')
832+
}
830833
}
831834

832835
fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
@@ -837,9 +840,26 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
837840

838841
for item in module_items.items() {
839842
if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind {
843+
let mut dead_items = Vec::new();
840844
for item in impl_item.items {
841-
visitor.check_definition(item.id.owner_id.def_id);
845+
match item.kind {
846+
hir::AssocItemKind::Const | hir::AssocItemKind::Type => {
847+
visitor.check_definition(item.id.owner_id.def_id)
848+
}
849+
hir::AssocItemKind::Fn { .. } => {
850+
let did = item.id.owner_id.def_id;
851+
if !visitor.is_live_code(did) {
852+
dead_items.push(did)
853+
}
854+
}
855+
}
842856
}
857+
visitor.warn_multiple_dead_codes(
858+
&dead_items,
859+
"used",
860+
Some(item.owner_id.def_id),
861+
false,
862+
);
843863
continue;
844864
}
845865

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ struct Foo {
1111
struct Bar;
1212

1313
impl Bar {
14-
fn a(&self) -> i32 { 5 } //~ WARNING: method `a` is never used
15-
pub fn b(&self) -> i32 { 6 } //~ WARNING: method `b` is never used
14+
fn a(&self) -> i32 { 5 } //~ WARNING: methods `a` and `b` are never used [dead_code]
15+
pub fn b(&self) -> i32 { 6 }
1616
}
1717

1818
pub(crate) struct Foo1 {
@@ -23,8 +23,8 @@ pub(crate) struct Foo1 {
2323
pub(crate) struct Bar1;
2424

2525
impl Bar1 {
26-
fn a(&self) -> i32 { 5 } //~ WARNING: method `a` is never used
27-
pub fn b(&self) -> i32 { 6 } //~ WARNING: method `b` is never used
26+
fn a(&self) -> i32 { 5 } //~ WARNING: methods `a` and `b` are never used [dead_code]
27+
pub fn b(&self) -> i32 { 6 }
2828
}
2929

3030
pub(crate) struct Foo2 {
@@ -35,8 +35,8 @@ pub(crate) struct Foo2 {
3535
pub(crate) struct Bar2;
3636

3737
impl Bar2 {
38-
fn a(&self) -> i32 { 5 } //~ WARNING: method `a` is never used
39-
pub fn b(&self) -> i32 { 6 } //~ WARNING: method `b` is never used
38+
fn a(&self) -> i32 { 5 } //~ WARNING: methods `a` and `b` are never used [dead_code]
39+
pub fn b(&self) -> i32 { 6 }
4040
}
4141

4242

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

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

17-
warning: method `a` is never used
17+
warning: methods `a` and `b` are never used
1818
--> $DIR/issue-85255.rs:14:8
1919
|
20+
LL | impl Bar {
21+
| -------- methods in this implementation
2022
LL | fn a(&self) -> i32 { 5 }
2123
| ^
22-
23-
warning: method `b` is never used
24-
--> $DIR/issue-85255.rs:15:12
25-
|
2624
LL | pub fn b(&self) -> i32 { 6 }
2725
| ^
2826

@@ -36,15 +34,13 @@ LL | a: i32,
3634
LL | pub b: i32,
3735
| ^
3836

39-
warning: method `a` is never used
37+
warning: methods `a` and `b` are never used
4038
--> $DIR/issue-85255.rs:26:8
4139
|
40+
LL | impl Bar1 {
41+
| --------- methods in this implementation
4242
LL | fn a(&self) -> i32 { 5 }
4343
| ^
44-
45-
warning: method `b` is never used
46-
--> $DIR/issue-85255.rs:27:12
47-
|
4844
LL | pub fn b(&self) -> i32 { 6 }
4945
| ^
5046

@@ -58,17 +54,15 @@ LL | a: i32,
5854
LL | pub b: i32,
5955
| ^
6056

61-
warning: method `a` is never used
57+
warning: methods `a` and `b` are never used
6258
--> $DIR/issue-85255.rs:38:8
6359
|
60+
LL | impl Bar2 {
61+
| --------- methods in this implementation
6462
LL | fn a(&self) -> i32 { 5 }
6563
| ^
66-
67-
warning: method `b` is never used
68-
--> $DIR/issue-85255.rs:39:12
69-
|
7064
LL | pub fn b(&self) -> i32 { 6 }
7165
| ^
7266

73-
warning: 9 warnings emitted
67+
warning: 6 warnings emitted
7468

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ LL | #![deny(dead_code)]
1313
error: method `foo` is never used
1414
--> $DIR/lint-dead-code-3.rs:16:8
1515
|
16+
LL | impl Foo {
17+
| -------- method in this implementation
1618
LL | fn foo(&self) {
1719
| ^^^
1820

tests/ui/lint/dead-code/lint-dead-code-6.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
struct UnusedStruct; //~ ERROR struct `UnusedStruct` is never constructed
44
impl UnusedStruct {
5-
fn unused_impl_fn_1() { //~ ERROR associated function `unused_impl_fn_1` is never used
5+
fn unused_impl_fn_1() {
6+
//~^ ERROR associated functions `unused_impl_fn_1`, `unused_impl_fn_2`, and `unused_impl_fn_3` are never used [dead_code]
67
println!("blah");
78
}
89

9-
fn unused_impl_fn_2(var: i32) { //~ ERROR associated function `unused_impl_fn_2` is never used
10+
fn unused_impl_fn_2(var: i32) {
1011
println!("foo {}", var);
1112
}
1213

13-
fn unused_impl_fn_3( //~ ERROR associated function `unused_impl_fn_3` is never used
14-
var: i32,
15-
) {
14+
fn unused_impl_fn_3(var: i32) {
1615
println!("bar {}", var);
1716
}
1817
}

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

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

13-
error: associated function `unused_impl_fn_1` is never used
13+
error: associated functions `unused_impl_fn_1`, `unused_impl_fn_2`, and `unused_impl_fn_3` are never used
1414
--> $DIR/lint-dead-code-6.rs:5:8
1515
|
16+
LL | impl UnusedStruct {
17+
| ----------------- associated functions in this implementation
1618
LL | fn unused_impl_fn_1() {
1719
| ^^^^^^^^^^^^^^^^
18-
19-
error: associated function `unused_impl_fn_2` is never used
20-
--> $DIR/lint-dead-code-6.rs:9:8
21-
|
20+
...
2221
LL | fn unused_impl_fn_2(var: i32) {
2322
| ^^^^^^^^^^^^^^^^
24-
25-
error: associated function `unused_impl_fn_3` is never used
26-
--> $DIR/lint-dead-code-6.rs:13:8
27-
|
28-
LL | fn unused_impl_fn_3(
23+
...
24+
LL | fn unused_impl_fn_3(var: i32) {
2925
| ^^^^^^^^^^^^^^^^
3026

31-
error: aborting due to 4 previous errors
27+
error: aborting due to 2 previous errors
3228

tests/ui/lint/dead-code/unused-assoc-fns.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ struct Foo;
44

55
impl Foo {
66
fn one() {}
7-
//~^ ERROR associated function `one` is never used [dead_code]
7+
//~^ ERROR associated functions `one`, `two`, and `three` are never used [dead_code]
88

99
fn two(&self) {}
10-
//~^ ERROR method `two` is never used [dead_code]
1110

1211
// seperation between functions
1312
// ...
@@ -16,7 +15,6 @@ impl Foo {
1615
fn used() {}
1716

1817
fn three(&self) {
19-
//~^ ERROR method `three` is never used [dead_code]
2018
Foo::one();
2119
// ...
2220
}
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
error: associated function `one` is never used
1+
error: associated functions `one`, `two`, and `three` are never used
22
--> $DIR/unused-assoc-fns.rs:6:8
33
|
4+
LL | impl Foo {
5+
| -------- associated functions in this implementation
46
LL | fn one() {}
57
| ^^^
8+
...
9+
LL | fn two(&self) {}
10+
| ^^^
11+
...
12+
LL | fn three(&self) {
13+
| ^^^^^
614
|
715
note: the lint level is defined here
816
--> $DIR/unused-assoc-fns.rs:1:9
@@ -11,17 +19,5 @@ LL | #![deny(unused)]
1119
| ^^^^^^
1220
= note: `#[deny(dead_code)]` implied by `#[deny(unused)]`
1321

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
22+
error: aborting due to previous error
2723

0 commit comments

Comments
 (0)