Skip to content

Commit 7273c88

Browse files
committed
Detect pub structs never constructed even though they impl pub trait with assoc constants
1 parent 6a207f4 commit 7273c88

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 2 additions & 2 deletions
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
{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
6+
trait Trait1 { //~ ERROR trait `Trait1` is never used
7+
const UNUSED: i32;
8+
fn unused(&self) {}
9+
}
10+
11+
pub trait Trait2 {
12+
const MAY_USED: i32;
13+
fn may_used(&self) {}
14+
}
15+
16+
impl Trait1 for T1 {
17+
const UNUSED: i32 = 0;
18+
}
19+
20+
impl Trait1 for T2 {
21+
const UNUSED: i32 = 0;
22+
}
23+
24+
impl Trait2 for T1 {
25+
const MAY_USED: i32 = 0;
26+
}
27+
28+
impl Trait2 for T2 {
29+
const MAY_USED: i32 = 0;
30+
}
31+
32+
fn main() {}
Lines changed: 26 additions & 0 deletions
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:6:7
21+
|
22+
LL | trait Trait1 {
23+
| ^^^^^^
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)