Skip to content

Commit 75b6768

Browse files
committed
Adjust dead code lint to account for fields that implement Drop, see #21775
1 parent e0e2627 commit 75b6768

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/librustc/middle/dead.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,16 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
460460
Some(def_id) => self.tcx.lang_items.items().any(|(_, item)| *item == Some(def_id)),
461461
_ => false
462462
};
463+
let field_has_dtor = match field_type.ty_adt_def() {
464+
Some(def) => def.has_dtor(),
465+
_ => false
466+
};
467+
463468
is_named
464469
&& !self.symbol_is_live(node.id, None)
465470
&& !is_marker_field
466471
&& !has_allow_dead_code_or_lang_attr(&node.attrs)
472+
&& !field_has_dtor
467473
}
468474

469475
fn should_warn_about_variant(&mut self, variant: &hir::Variant_) -> bool {

src/test/compile-fail/lint-dead-code-1.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,27 @@ fn bar() { //~ ERROR: function is never used
115115
#[allow(dead_code)]
116116
fn g() { h(); }
117117
fn h() {}
118+
119+
// This implements Drop, so we should not see a warning on
120+
// 'unused' field 's'.
121+
struct S(u32);
122+
123+
impl Drop for S {
124+
fn drop(&mut self) { }
125+
}
126+
127+
struct Container {
128+
s: S,
129+
}
130+
131+
impl Container {
132+
fn new() -> Container {
133+
let s = S(4);
134+
135+
Container { s: s }
136+
}
137+
}
138+
139+
pub fn baz() {
140+
Container::new();
141+
}

0 commit comments

Comments
 (0)