From 3530e4a6471b6948c719f709bc10923c3f5524a5 Mon Sep 17 00:00:00 2001 From: Jakub Wieczorek Date: Sat, 20 Sep 2014 14:08:10 +0200 Subject: [PATCH] Use more descriptive names in dead code messages --- src/librustc/middle/dead.rs | 17 +++++++------ src/libsyntax/ast.rs | 25 +++++++++++++++++++ .../syntax-extension-regex-unused-static.rs | 2 +- .../compile-fail/fail-no-dead-code-core.rs | 2 +- src/test/compile-fail/fail-no-dead-code.rs | 2 +- src/test/compile-fail/lint-dead-code-1.rs | 16 ++++++------ src/test/compile-fail/lint-dead-code-2.rs | 6 ++--- src/test/compile-fail/lint-dead-code-3.rs | 12 ++++----- src/test/compile-fail/lint-dead-code-4.rs | 6 ++--- src/test/compile-fail/lint-dead-code-5.rs | 2 +- 10 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 7110c5f5720..1b2f62a9385 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -507,7 +507,8 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> { fn warn_dead_code(&mut self, id: ast::NodeId, span: codemap::Span, - ident: ast::Ident) { + ident: ast::Ident, + node_type: &str) { let name = ident.as_str(); if !name.starts_with("_") { self.tcx @@ -515,7 +516,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> { .add_lint(lint::builtin::DEAD_CODE, id, span, - format!("code is never used: `{}`", name)); + format!("{} is never used: `{}`", node_type, name)); } } } @@ -523,13 +524,14 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> { impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { fn visit_item(&mut self, item: &ast::Item) { if self.should_warn_about_item(item) { - self.warn_dead_code(item.id, item.span, item.ident); + self.warn_dead_code(item.id, item.span, item.ident, item.node.descriptive_variant()); } else { match item.node { ast::ItemEnum(ref enum_def, _) => { for variant in enum_def.variants.iter() { if self.should_warn_about_variant(&variant.node) { - self.warn_dead_code(variant.node.id, variant.span, variant.node.name); + self.warn_dead_code(variant.node.id, variant.span, + variant.node.name, "variant"); } } }, @@ -541,7 +543,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { fn visit_foreign_item(&mut self, fi: &ast::ForeignItem) { if !self.symbol_is_live(fi.id, None) { - self.warn_dead_code(fi.id, fi.span, fi.ident); + self.warn_dead_code(fi.id, fi.span, fi.ident, fi.node.descriptive_variant()); } visit::walk_foreign_item(self, fi); } @@ -553,7 +555,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { match fk { visit::FkMethod(name, _, _) => { if !self.symbol_is_live(id, None) { - self.warn_dead_code(id, span, name); + self.warn_dead_code(id, span, name, "method"); } } _ => () @@ -563,7 +565,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { fn visit_struct_field(&mut self, field: &ast::StructField) { if self.should_warn_about_field(&field.node) { - self.warn_dead_code(field.node.id, field.span, field.node.ident().unwrap()); + self.warn_dead_code(field.node.id, field.span, + field.node.ident().unwrap(), "struct field"); } visit::walk_struct_field(self, field); diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 74c69762be1..38d8136c1a1 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1323,6 +1323,22 @@ pub enum Item_ { ItemMac(Mac), } +impl Item_ { + pub fn descriptive_variant(&self) -> &str { + match *self { + ItemStatic(..) => "static item", + ItemFn(..) => "function", + ItemMod(..) => "module", + ItemForeignMod(..) => "foreign module", + ItemTy(..) => "type alias", + ItemEnum(..) => "enum", + ItemStruct(..) => "struct", + ItemTrait(..) => "trait", + _ => "item" + } + } +} + #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] pub struct ForeignItem { pub ident: Ident, @@ -1339,6 +1355,15 @@ pub enum ForeignItem_ { ForeignItemStatic(P, /* is_mutbl */ bool), } +impl ForeignItem_ { + pub fn descriptive_variant(&self) -> &str { + match *self { + ForeignItemFn(..) => "foreign function", + ForeignItemStatic(..) => "foreign static item" + } + } +} + #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] pub enum UnboxedClosureKind { FnUnboxedClosureKind, diff --git a/src/test/compile-fail-fulldeps/syntax-extension-regex-unused-static.rs b/src/test/compile-fail-fulldeps/syntax-extension-regex-unused-static.rs index d7135bc5c91..095acf56e48 100644 --- a/src/test/compile-fail-fulldeps/syntax-extension-regex-unused-static.rs +++ b/src/test/compile-fail-fulldeps/syntax-extension-regex-unused-static.rs @@ -26,5 +26,5 @@ extern crate regex; // unused variable warning). fn main() { - static fubar: regex::Regex = regex!("abc"); //~ ERROR code is never used: `fubar` + static fubar: regex::Regex = regex!("abc"); //~ ERROR static item is never used: `fubar` } diff --git a/src/test/compile-fail/fail-no-dead-code-core.rs b/src/test/compile-fail/fail-no-dead-code-core.rs index b2b04edc787..58ecdec538e 100644 --- a/src/test/compile-fail/fail-no-dead-code-core.rs +++ b/src/test/compile-fail/fail-no-dead-code-core.rs @@ -15,7 +15,7 @@ #[phase(link, plugin)] extern crate core; -fn foo() { //~ ERROR code is never used +fn foo() { //~ ERROR function is never used // none of these should have any dead_code exposed to the user fail!(); diff --git a/src/test/compile-fail/fail-no-dead-code.rs b/src/test/compile-fail/fail-no-dead-code.rs index da59722c3ff..897710609fd 100644 --- a/src/test/compile-fail/fail-no-dead-code.rs +++ b/src/test/compile-fail/fail-no-dead-code.rs @@ -11,7 +11,7 @@ #![deny(dead_code)] #![allow(unreachable_code)] -fn foo() { //~ ERROR code is never used +fn foo() { //~ ERROR function is never used // none of these should have any dead_code exposed to the user fail!(); diff --git a/src/test/compile-fail/lint-dead-code-1.rs b/src/test/compile-fail/lint-dead-code-1.rs index 8c40ea2afe2..3e563e9e138 100644 --- a/src/test/compile-fail/lint-dead-code-1.rs +++ b/src/test/compile-fail/lint-dead-code-1.rs @@ -22,7 +22,7 @@ pub use foo2::Bar2; pub trait Sized {} mod foo { - pub struct Bar; //~ ERROR: code is never used + pub struct Bar; //~ ERROR: struct is never used } mod foo2 { @@ -30,7 +30,7 @@ mod foo2 { } pub static pub_static: int = 0; -static priv_static: int = 0; //~ ERROR: code is never used +static priv_static: int = 0; //~ ERROR: static item is never used static used_static: int = 0; pub static used_static2: int = used_static; static USED_STATIC: int = 0; @@ -38,7 +38,7 @@ static STATIC_USED_IN_ENUM_DISCRIMINANT: int = 10; pub type typ = *const UsedStruct4; pub struct PubStruct; -struct PrivStruct; //~ ERROR: code is never used +struct PrivStruct; //~ ERROR: struct is never used struct UsedStruct1 { #[allow(dead_code)] x: int @@ -64,10 +64,10 @@ pub enum pub_enum { foo1, bar1 } pub enum pub_enum2 { a(*const StructUsedInEnum) } pub enum pub_enum3 { Foo = STATIC_USED_IN_ENUM_DISCRIMINANT } -enum priv_enum { foo2, bar2 } //~ ERROR: code is never used +enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used enum used_enum { foo3, - bar3 //~ ERROR code is never used + bar3 //~ ERROR variant is never used } fn f() {} @@ -87,17 +87,17 @@ pub fn pub_fn() { } f::(); } -fn priv_fn() { //~ ERROR: code is never used +fn priv_fn() { //~ ERROR: function is never used let unused_struct = PrivStruct; } fn used_fn() {} -fn foo() { //~ ERROR: code is never used +fn foo() { //~ ERROR: function is never used bar(); let unused_enum = foo2; } -fn bar() { //~ ERROR: code is never used +fn bar() { //~ ERROR: function is never used foo(); } diff --git a/src/test/compile-fail/lint-dead-code-2.rs b/src/test/compile-fail/lint-dead-code-2.rs index 4ad56ce9154..eb284c4d054 100644 --- a/src/test/compile-fail/lint-dead-code-2.rs +++ b/src/test/compile-fail/lint-dead-code-2.rs @@ -28,10 +28,10 @@ impl Bar for Foo { fn live_fn() {} -fn dead_fn() {} //~ ERROR: code is never used +fn dead_fn() {} //~ ERROR: function is never used #[main] -fn dead_fn2() {} //~ ERROR: code is never used +fn dead_fn2() {} //~ ERROR: function is never used fn used_fn() {} @@ -44,7 +44,7 @@ fn start(_: int, _: *const *const u8) -> int { } // this is not main -fn main() { //~ ERROR: code is never used +fn main() { //~ ERROR: function is never used dead_fn(); dead_fn2(); } diff --git a/src/test/compile-fail/lint-dead-code-3.rs b/src/test/compile-fail/lint-dead-code-3.rs index 41e6f24d79c..f73c19b5fc9 100644 --- a/src/test/compile-fail/lint-dead-code-3.rs +++ b/src/test/compile-fail/lint-dead-code-3.rs @@ -21,15 +21,15 @@ extern { fn extern_foo(); } -struct Foo; //~ ERROR: code is never used +struct Foo; //~ ERROR: struct is never used impl Foo { - fn foo(&self) { //~ ERROR: code is never used + fn foo(&self) { //~ ERROR: method is never used bar() } } -fn bar() { //~ ERROR: code is never used - fn baz() {} //~ ERROR: code is never used +fn bar() { //~ ERROR: function is never used + fn baz() {} //~ ERROR: function is never used Foo.foo(); baz(); @@ -68,9 +68,9 @@ mod blah { } } -enum c_void {} //~ ERROR: code is never used +enum c_void {} //~ ERROR: enum is never used extern { - fn free(p: *const c_void); //~ ERROR: code is never used + fn free(p: *const c_void); //~ ERROR: foreign function is never used } // Check provided method diff --git a/src/test/compile-fail/lint-dead-code-4.rs b/src/test/compile-fail/lint-dead-code-4.rs index 826faad32c9..ba02faf11c3 100644 --- a/src/test/compile-fail/lint-dead-code-4.rs +++ b/src/test/compile-fail/lint-dead-code-4.rs @@ -19,7 +19,7 @@ use std::num; struct Foo { x: uint, - b: bool, //~ ERROR: code is never used + b: bool, //~ ERROR: struct field is never used marker: std::kinds::marker::NoCopy } @@ -31,7 +31,7 @@ enum XYZ { X, //~ ERROR variant is never used Y { //~ ERROR variant is never used a: String, - b: int //~ ERROR: code is never used + b: int //~ ERROR: struct field is never used }, Z } @@ -44,7 +44,7 @@ fn field_match_in_patterns(b: XYZ) -> String { } struct Bar { - x: uint, //~ ERROR: code is never used + x: uint, //~ ERROR: struct field is never used b: bool, _guard: () } diff --git a/src/test/compile-fail/lint-dead-code-5.rs b/src/test/compile-fail/lint-dead-code-5.rs index c26ae1a7023..62afa089bbe 100644 --- a/src/test/compile-fail/lint-dead-code-5.rs +++ b/src/test/compile-fail/lint-dead-code-5.rs @@ -14,7 +14,7 @@ enum Enum1 { Variant1(int), - Variant2 //~ ERROR: code is never used + Variant2 //~ ERROR: variant is never used } enum Enum2 {