Skip to content

Commit 3065010

Browse files
Rollup merge of rust-lang#126276 - mu001999-contrib:dead/enhance, r=fee1-dead
Detect pub structs never constructed even though they impl pub trait with assoc constants Extend dead code analysis to impl items of pub assoc constants. <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> -->
2 parents d25227c + af10661 commit 3065010

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

compiler/rustc_passes/src/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
472472
&& let ItemKind::Impl(impl_ref) =
473473
self.tcx.hir().expect_item(local_impl_id).kind
474474
{
475-
if matches!(trait_item.kind, hir::TraitItemKind::Fn(..))
475+
if !matches!(trait_item.kind, hir::TraitItemKind::Type(..))
476476
&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
477477
.ty_and_all_fields_are_public
478478
{
@@ -802,7 +802,7 @@ fn check_item<'tcx>(
802802
// And we access the Map here to get HirId from LocalDefId
803803
for local_def_id in local_def_ids {
804804
// check the function may construct Self
805-
let mut may_construct_self = true;
805+
let mut may_construct_self = false;
806806
if let Some(fn_sig) =
807807
tcx.hir().fn_sig_by_hir_id(tcx.local_def_id_to_hir_id(local_def_id))
808808
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![deny(dead_code)]
2+
3+
struct T1; //~ ERROR struct `T1` is never constructed
4+
pub struct T2(i32); //~ ERROR struct `T2` is never constructed
5+
struct T3;
6+
7+
trait Trait1 { //~ ERROR trait `Trait1` is never used
8+
const UNUSED: i32;
9+
fn unused(&self) {}
10+
fn construct_self() -> Self;
11+
}
12+
13+
pub trait Trait2 {
14+
const USED: i32;
15+
fn used(&self) {}
16+
}
17+
18+
pub trait Trait3 {
19+
const USED: i32;
20+
fn construct_self() -> Self;
21+
}
22+
23+
impl Trait1 for T1 {
24+
const UNUSED: i32 = 0;
25+
fn construct_self() -> Self {
26+
Self
27+
}
28+
}
29+
30+
impl Trait1 for T2 {
31+
const UNUSED: i32 = 0;
32+
fn construct_self() -> Self {
33+
T2(0)
34+
}
35+
}
36+
37+
impl Trait2 for T1 {
38+
const USED: i32 = 0;
39+
}
40+
41+
impl Trait2 for T2 {
42+
const USED: i32 = 0;
43+
}
44+
45+
impl Trait3 for T3 {
46+
const USED: i32 = 0;
47+
fn construct_self() -> Self {
48+
Self
49+
}
50+
}
51+
52+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: struct `T1` is never constructed
2+
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:3:8
3+
|
4+
LL | struct T1;
5+
| ^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:1:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: struct `T2` is never constructed
14+
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:4:12
15+
|
16+
LL | pub struct T2(i32);
17+
| ^^
18+
19+
error: trait `Trait1` is never used
20+
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:7:7
21+
|
22+
LL | trait Trait1 {
23+
| ^^^^^^
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)