From f3d88c320d4b08e5534f2193462e6a20e27b1d85 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 3 Aug 2014 16:40:06 +1000 Subject: [PATCH] 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. --- src/librustc/middle/dead.rs | 22 +++++------ .../run-pass/dead-code-leading-underscore.rs | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 src/test/run-pass/dead-code-leading-underscore.rs diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 108bd35424ae..23ba26ad3bf6 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -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)); + } } } diff --git a/src/test/run-pass/dead-code-leading-underscore.rs b/src/test/run-pass/dead-code-leading-underscore.rs new file mode 100644 index 000000000000..b678b77095f7 --- /dev/null +++ b/src/test/run-pass/dead-code-leading-underscore.rs @@ -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 or the MIT license +// , 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() {}