Skip to content

Commit d06b8e1

Browse files
committed
lint: dead_code ignores items with leading underscores.
This generalises the behaviour with struct fields (which recieve no dead_code warning if they have a leading _), and other similar lints, to all items, e.g. `fn _foo() {} fn main() {}` has no warnings.
1 parent 75a39e0 commit d06b8e1

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/librustc/middle/dead.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,14 @@ struct DeadVisitor<'a> {
412412

413413
impl<'a> DeadVisitor<'a> {
414414
fn should_warn_about_field(&mut self, node: &ast::StructField_) -> bool {
415-
let (is_named, has_leading_underscore) = match node.ident() {
416-
Some(ref ident) => (true, token::get_ident(*ident).get().as_bytes()[0] == ('_' as u8)),
417-
_ => (false, false)
418-
};
415+
let is_named = node.ident().is_some();
419416
let field_type = ty::node_id_to_type(self.tcx, node.id);
420417
let is_marker_field = match ty::ty_to_def_id(field_type) {
421418
Some(def_id) => self.tcx.lang_items.items().any(|(_, item)| *item == Some(def_id)),
422419
_ => false
423420
};
424421
is_named
425422
&& !self.symbol_is_live(node.id, None)
426-
&& !has_leading_underscore
427423
&& !is_marker_field
428424
&& !has_allow_dead_code_or_lang_attr(node.attrs.as_slice())
429425
}
@@ -468,13 +464,15 @@ impl<'a> DeadVisitor<'a> {
468464
id: ast::NodeId,
469465
span: codemap::Span,
470466
ident: ast::Ident) {
471-
self.tcx
472-
.sess
473-
.add_lint(lint::builtin::DEAD_CODE,
474-
id,
475-
span,
476-
format!("code is never used: `{}`",
477-
token::get_ident(ident)));
467+
let name = ident.as_str();
468+
if !name.starts_with("_") {
469+
self.tcx
470+
.sess
471+
.add_lint(lint::builtin::DEAD_CODE,
472+
id,
473+
span,
474+
format!("code is never used: `{}`", name));
475+
}
478476
}
479477
}
480478

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2014 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+
#![deny(dead_code)]
12+
13+
static _X: uint = 0;
14+
15+
fn _foo() {}
16+
17+
struct _Y {
18+
_z: uint
19+
}
20+
21+
enum _Z {}
22+
23+
impl _Y {
24+
fn _bar() {}
25+
}
26+
27+
type _A = int;
28+
29+
mod _bar {
30+
fn _qux() {}
31+
}
32+
33+
extern {
34+
#[link_name = "abort"]
35+
fn _abort() -> !;
36+
}
37+
38+
fn main() {}

0 commit comments

Comments
 (0)