Skip to content

Commit 3530e4a

Browse files
author
Jakub Wieczorek
committed
Use more descriptive names in dead code messages
1 parent 2ec795b commit 3530e4a

10 files changed

+59
-31
lines changed

src/librustc/middle/dead.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -507,29 +507,31 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
507507
fn warn_dead_code(&mut self,
508508
id: ast::NodeId,
509509
span: codemap::Span,
510-
ident: ast::Ident) {
510+
ident: ast::Ident,
511+
node_type: &str) {
511512
let name = ident.as_str();
512513
if !name.starts_with("_") {
513514
self.tcx
514515
.sess
515516
.add_lint(lint::builtin::DEAD_CODE,
516517
id,
517518
span,
518-
format!("code is never used: `{}`", name));
519+
format!("{} is never used: `{}`", node_type, name));
519520
}
520521
}
521522
}
522523

523524
impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
524525
fn visit_item(&mut self, item: &ast::Item) {
525526
if self.should_warn_about_item(item) {
526-
self.warn_dead_code(item.id, item.span, item.ident);
527+
self.warn_dead_code(item.id, item.span, item.ident, item.node.descriptive_variant());
527528
} else {
528529
match item.node {
529530
ast::ItemEnum(ref enum_def, _) => {
530531
for variant in enum_def.variants.iter() {
531532
if self.should_warn_about_variant(&variant.node) {
532-
self.warn_dead_code(variant.node.id, variant.span, variant.node.name);
533+
self.warn_dead_code(variant.node.id, variant.span,
534+
variant.node.name, "variant");
533535
}
534536
}
535537
},
@@ -541,7 +543,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
541543

542544
fn visit_foreign_item(&mut self, fi: &ast::ForeignItem) {
543545
if !self.symbol_is_live(fi.id, None) {
544-
self.warn_dead_code(fi.id, fi.span, fi.ident);
546+
self.warn_dead_code(fi.id, fi.span, fi.ident, fi.node.descriptive_variant());
545547
}
546548
visit::walk_foreign_item(self, fi);
547549
}
@@ -553,7 +555,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
553555
match fk {
554556
visit::FkMethod(name, _, _) => {
555557
if !self.symbol_is_live(id, None) {
556-
self.warn_dead_code(id, span, name);
558+
self.warn_dead_code(id, span, name, "method");
557559
}
558560
}
559561
_ => ()
@@ -563,7 +565,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
563565

564566
fn visit_struct_field(&mut self, field: &ast::StructField) {
565567
if self.should_warn_about_field(&field.node) {
566-
self.warn_dead_code(field.node.id, field.span, field.node.ident().unwrap());
568+
self.warn_dead_code(field.node.id, field.span,
569+
field.node.ident().unwrap(), "struct field");
567570
}
568571

569572
visit::walk_struct_field(self, field);

src/libsyntax/ast.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,22 @@ pub enum Item_ {
13231323
ItemMac(Mac),
13241324
}
13251325

1326+
impl Item_ {
1327+
pub fn descriptive_variant(&self) -> &str {
1328+
match *self {
1329+
ItemStatic(..) => "static item",
1330+
ItemFn(..) => "function",
1331+
ItemMod(..) => "module",
1332+
ItemForeignMod(..) => "foreign module",
1333+
ItemTy(..) => "type alias",
1334+
ItemEnum(..) => "enum",
1335+
ItemStruct(..) => "struct",
1336+
ItemTrait(..) => "trait",
1337+
_ => "item"
1338+
}
1339+
}
1340+
}
1341+
13261342
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
13271343
pub struct ForeignItem {
13281344
pub ident: Ident,
@@ -1339,6 +1355,15 @@ pub enum ForeignItem_ {
13391355
ForeignItemStatic(P<Ty>, /* is_mutbl */ bool),
13401356
}
13411357

1358+
impl ForeignItem_ {
1359+
pub fn descriptive_variant(&self) -> &str {
1360+
match *self {
1361+
ForeignItemFn(..) => "foreign function",
1362+
ForeignItemStatic(..) => "foreign static item"
1363+
}
1364+
}
1365+
}
1366+
13421367
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
13431368
pub enum UnboxedClosureKind {
13441369
FnUnboxedClosureKind,

src/test/compile-fail-fulldeps/syntax-extension-regex-unused-static.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ extern crate regex;
2626
// unused variable warning).
2727

2828
fn main() {
29-
static fubar: regex::Regex = regex!("abc"); //~ ERROR code is never used: `fubar`
29+
static fubar: regex::Regex = regex!("abc"); //~ ERROR static item is never used: `fubar`
3030
}

src/test/compile-fail/fail-no-dead-code-core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#[phase(link, plugin)] extern crate core;
1616

1717

18-
fn foo() { //~ ERROR code is never used
18+
fn foo() { //~ ERROR function is never used
1919

2020
// none of these should have any dead_code exposed to the user
2121
fail!();

src/test/compile-fail/fail-no-dead-code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![deny(dead_code)]
1212
#![allow(unreachable_code)]
1313

14-
fn foo() { //~ ERROR code is never used
14+
fn foo() { //~ ERROR function is never used
1515

1616
// none of these should have any dead_code exposed to the user
1717
fail!();

src/test/compile-fail/lint-dead-code-1.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ pub use foo2::Bar2;
2222
pub trait Sized {}
2323

2424
mod foo {
25-
pub struct Bar; //~ ERROR: code is never used
25+
pub struct Bar; //~ ERROR: struct is never used
2626
}
2727

2828
mod foo2 {
2929
pub struct Bar2;
3030
}
3131

3232
pub static pub_static: int = 0;
33-
static priv_static: int = 0; //~ ERROR: code is never used
33+
static priv_static: int = 0; //~ ERROR: static item is never used
3434
static used_static: int = 0;
3535
pub static used_static2: int = used_static;
3636
static USED_STATIC: int = 0;
3737
static STATIC_USED_IN_ENUM_DISCRIMINANT: int = 10;
3838

3939
pub type typ = *const UsedStruct4;
4040
pub struct PubStruct;
41-
struct PrivStruct; //~ ERROR: code is never used
41+
struct PrivStruct; //~ ERROR: struct is never used
4242
struct UsedStruct1 {
4343
#[allow(dead_code)]
4444
x: int
@@ -64,10 +64,10 @@ pub enum pub_enum { foo1, bar1 }
6464
pub enum pub_enum2 { a(*const StructUsedInEnum) }
6565
pub enum pub_enum3 { Foo = STATIC_USED_IN_ENUM_DISCRIMINANT }
6666

67-
enum priv_enum { foo2, bar2 } //~ ERROR: code is never used
67+
enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used
6868
enum used_enum {
6969
foo3,
70-
bar3 //~ ERROR code is never used
70+
bar3 //~ ERROR variant is never used
7171
}
7272

7373
fn f<T>() {}
@@ -87,17 +87,17 @@ pub fn pub_fn() {
8787
}
8888
f::<StructUsedInGeneric>();
8989
}
90-
fn priv_fn() { //~ ERROR: code is never used
90+
fn priv_fn() { //~ ERROR: function is never used
9191
let unused_struct = PrivStruct;
9292
}
9393
fn used_fn() {}
9494

95-
fn foo() { //~ ERROR: code is never used
95+
fn foo() { //~ ERROR: function is never used
9696
bar();
9797
let unused_enum = foo2;
9898
}
9999

100-
fn bar() { //~ ERROR: code is never used
100+
fn bar() { //~ ERROR: function is never used
101101
foo();
102102
}
103103

src/test/compile-fail/lint-dead-code-2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ impl Bar for Foo {
2828

2929
fn live_fn() {}
3030

31-
fn dead_fn() {} //~ ERROR: code is never used
31+
fn dead_fn() {} //~ ERROR: function is never used
3232

3333
#[main]
34-
fn dead_fn2() {} //~ ERROR: code is never used
34+
fn dead_fn2() {} //~ ERROR: function is never used
3535

3636
fn used_fn() {}
3737

@@ -44,7 +44,7 @@ fn start(_: int, _: *const *const u8) -> int {
4444
}
4545

4646
// this is not main
47-
fn main() { //~ ERROR: code is never used
47+
fn main() { //~ ERROR: function is never used
4848
dead_fn();
4949
dead_fn2();
5050
}

src/test/compile-fail/lint-dead-code-3.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ extern {
2121
fn extern_foo();
2222
}
2323

24-
struct Foo; //~ ERROR: code is never used
24+
struct Foo; //~ ERROR: struct is never used
2525
impl Foo {
26-
fn foo(&self) { //~ ERROR: code is never used
26+
fn foo(&self) { //~ ERROR: method is never used
2727
bar()
2828
}
2929
}
3030

31-
fn bar() { //~ ERROR: code is never used
32-
fn baz() {} //~ ERROR: code is never used
31+
fn bar() { //~ ERROR: function is never used
32+
fn baz() {} //~ ERROR: function is never used
3333

3434
Foo.foo();
3535
baz();
@@ -68,9 +68,9 @@ mod blah {
6868
}
6969
}
7070

71-
enum c_void {} //~ ERROR: code is never used
71+
enum c_void {} //~ ERROR: enum is never used
7272
extern {
73-
fn free(p: *const c_void); //~ ERROR: code is never used
73+
fn free(p: *const c_void); //~ ERROR: foreign function is never used
7474
}
7575

7676
// Check provided method

src/test/compile-fail/lint-dead-code-4.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::num;
1919

2020
struct Foo {
2121
x: uint,
22-
b: bool, //~ ERROR: code is never used
22+
b: bool, //~ ERROR: struct field is never used
2323
marker: std::kinds::marker::NoCopy
2424
}
2525

@@ -31,7 +31,7 @@ enum XYZ {
3131
X, //~ ERROR variant is never used
3232
Y { //~ ERROR variant is never used
3333
a: String,
34-
b: int //~ ERROR: code is never used
34+
b: int //~ ERROR: struct field is never used
3535
},
3636
Z
3737
}
@@ -44,7 +44,7 @@ fn field_match_in_patterns(b: XYZ) -> String {
4444
}
4545

4646
struct Bar {
47-
x: uint, //~ ERROR: code is never used
47+
x: uint, //~ ERROR: struct field is never used
4848
b: bool,
4949
_guard: ()
5050
}

src/test/compile-fail/lint-dead-code-5.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
enum Enum1 {
1616
Variant1(int),
17-
Variant2 //~ ERROR: code is never used
17+
Variant2 //~ ERROR: variant is never used
1818
}
1919

2020
enum Enum2 {

0 commit comments

Comments
 (0)