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.
This commit is contained in:
Huon Wilson 2014-08-03 16:40:06 +10:00
parent d4d608fabd
commit f3d88c320d
2 changed files with 48 additions and 12 deletions

View file

@ -412,10 +412,7 @@ struct DeadVisitor<'a> {
impl<'a> DeadVisitor<'a> {
fn should_warn_about_field(&mut self, node: &ast::StructField_) -> bool {
let (is_named, has_leading_underscore) = match node.ident() {
Some(ref ident) => (true, token::get_ident(*ident).get().as_bytes()[0] == ('_' as u8)),
_ => (false, false)
};
let is_named = node.ident().is_some();
let field_type = ty::node_id_to_type(self.tcx, node.id);
let is_marker_field = match ty::ty_to_def_id(field_type) {
Some(def_id) => self.tcx.lang_items.items().any(|(_, item)| *item == Some(def_id)),
@ -423,7 +420,6 @@ impl<'a> DeadVisitor<'a> {
};
is_named
&& !self.symbol_is_live(node.id, None)
&& !has_leading_underscore
&& !is_marker_field
&& !has_allow_dead_code_or_lang_attr(node.attrs.as_slice())
}
@ -468,13 +464,15 @@ impl<'a> DeadVisitor<'a> {
id: ast::NodeId,
span: codemap::Span,
ident: ast::Ident) {
self.tcx
.sess
.add_lint(lint::builtin::DEAD_CODE,
id,
span,
format!("code is never used: `{}`",
token::get_ident(ident)));
let name = ident.as_str();
if !name.starts_with("_") {
self.tcx
.sess
.add_lint(lint::builtin::DEAD_CODE,
id,
span,
format!("code is never used: `{}`", name));
}
}
}

View file

@ -0,0 +1,38 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![deny(dead_code)]
static _X: uint = 0;
fn _foo() {}
struct _Y {
_z: uint
}
enum _Z {}
impl _Y {
fn _bar() {}
}
type _A = int;
mod _bar {
fn _qux() {}
}
extern {
#[link_name = "abort"]
fn _abort() -> !;
}
fn main() {}