Skip to content

Commit 7766b50

Browse files
committed
Auto merge of #38791 - dylanmckay:foreign-item-dc, r=eddyb
Don't warn about dead foreign items if the 'allow(dead_code)' attribute is present This functionality was missing, and should have existed previously. Fixes #38780
2 parents 1659d65 + 09178e4 commit 7766b50

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/librustc/middle/dead.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
445445
&& !has_allow_dead_code_or_lang_attr(&variant.attrs)
446446
}
447447

448+
fn should_warn_about_foreign_item(&mut self, fi: &hir::ForeignItem) -> bool {
449+
!self.symbol_is_live(fi.id, None)
450+
&& !has_allow_dead_code_or_lang_attr(&fi.attrs)
451+
}
452+
448453
// id := node id of an item's definition.
449454
// ctor_id := `Some` if the item is a struct_ctor (tuple struct),
450455
// `None` otherwise.
@@ -534,7 +539,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
534539
}
535540

536541
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem) {
537-
if !self.symbol_is_live(fi.id, None) {
542+
if self.should_warn_about_foreign_item(fi) {
538543
self.warn_dead_code(fi.id, fi.span, fi.name, fi.node.descriptive_variant());
539544
}
540545
intravisit::walk_foreign_item(self, fi);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
#![deny(dead_code)]
14+
15+
extern "C" {
16+
#[allow(dead_code)]
17+
static Qt: u64;
18+
}
19+
20+
fn main() {}

0 commit comments

Comments
 (0)