Skip to content

Commit

Permalink
Rollup merge of rust-lang#46103 - zackmdavis:dead_code_lint_should_sa…
Browse files Browse the repository at this point in the history
…y_never_constructed_for_variants, r=arielb1

dead code lint to say "never constructed" for variants

As reported in rust-lang#19140, rust-lang#44083, and rust-lang#44565, some users were confused when
the dead-code lint reported an enum variant to be "unused" when it was
matched on (but not constructed). This wording change makes it clearer
that the lint is in fact checking for construction.

We continue to say "used" for all other items (it's tempting to say
"called" for functions and methods, but this turns out not to be
correct: functions can be passed as arguments and the dead-code lint
isn't special-casing that or anything).

Resolves rust-lang#19140.

r? @pnkfelix
  • Loading branch information
kennytm authored Nov 21, 2017
2 parents 0af67a4 + 1a9dc2e commit 7eb2e79
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
23 changes: 14 additions & 9 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,15 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
id: ast::NodeId,
span: syntax_pos::Span,
name: ast::Name,
node_type: &str) {
node_type: &str,
participle: &str) {
if !name.as_str().starts_with("_") {
self.tcx
.lint_node(lint::builtin::DEAD_CODE,
id,
span,
&format!("{} is never used: `{}`", node_type, name));
&format!("{} is never {}: `{}`",
node_type, participle, name));
}
}
}
Expand Down Expand Up @@ -570,7 +572,8 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
item.id,
span,
item.name,
item.node.descriptive_variant()
item.node.descriptive_variant(),
"used",
);
} else {
// Only continue if we didn't warn
Expand All @@ -583,23 +586,24 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
g: &'tcx hir::Generics,
id: ast::NodeId) {
if self.should_warn_about_variant(&variant.node) {
self.warn_dead_code(variant.node.data.id(), variant.span, variant.node.name, "variant");
self.warn_dead_code(variant.node.data.id(), variant.span, variant.node.name,
"variant", "constructed");
} else {
intravisit::walk_variant(self, variant, g, id);
}
}

fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem) {
if self.should_warn_about_foreign_item(fi) {
self.warn_dead_code(fi.id, fi.span, fi.name, fi.node.descriptive_variant());
self.warn_dead_code(fi.id, fi.span, fi.name,
fi.node.descriptive_variant(), "used");
}
intravisit::walk_foreign_item(self, fi);
}

fn visit_struct_field(&mut self, field: &'tcx hir::StructField) {
if self.should_warn_about_field(&field) {
self.warn_dead_code(field.id, field.span,
field.name, "field");
self.warn_dead_code(field.id, field.span, field.name, "field", "used");
}
intravisit::walk_struct_field(self, field);
}
Expand All @@ -611,14 +615,15 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
self.warn_dead_code(impl_item.id,
impl_item.span,
impl_item.name,
"associated const");
"associated const",
"used");
}
self.visit_nested_body(body_id)
}
hir::ImplItemKind::Method(_, body_id) => {
if !self.symbol_is_live(impl_item.id, None) {
let span = self.tcx.sess.codemap().def_span(impl_item.span);
self.warn_dead_code(impl_item.id, span, impl_item.name, "method");
self.warn_dead_code(impl_item.id, span, impl_item.name, "method", "used");
}
self.visit_nested_body(body_id)
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/lint-dead-code-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub enum pub_enum3 {
enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used
enum used_enum {
foo3,
bar3 //~ ERROR variant is never used
bar3 //~ ERROR variant is never constructed
}

fn f<T>() {}
Expand Down
8 changes: 4 additions & 4 deletions src/test/compile-fail/lint-dead-code-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn field_read(f: Foo) -> usize {
}

enum XYZ {
X, //~ ERROR variant is never used
Y { //~ ERROR variant is never used
X, //~ ERROR variant is never constructed
Y { //~ ERROR variant is never constructed
a: String,
b: i32,
c: i32,
Expand All @@ -43,13 +43,13 @@ enum ABC { //~ ERROR enum is never used

// ensure struct variants get warning for their fields
enum IJK {
I, //~ ERROR variant is never used
I, //~ ERROR variant is never constructed
J {
a: String,
b: i32, //~ ERROR field is never used
c: i32, //~ ERROR field is never used
},
K //~ ERROR variant is never used
K //~ ERROR variant is never constructed

}

Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/lint-dead-code-5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

enum Enum1 {
Variant1(isize),
Variant2 //~ ERROR: variant is never used
Variant2 //~ ERROR: variant is never constructed
}

enum Enum2 {
Variant3(bool),
#[allow(dead_code)]
Variant4(isize),
Variant5 { _x: isize }, //~ ERROR: variant is never used: `Variant5`
Variant6(isize), //~ ERROR: variant is never used: `Variant6`
Variant5 { _x: isize }, //~ ERROR: variant is never constructed: `Variant5`
Variant6(isize), //~ ERROR: variant is never constructed: `Variant6`
_Variant7,
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/lint-dead-code-variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#[derive(Clone)]
enum Enum {
Variant1, //~ ERROR: variant is never used
Variant1, //~ ERROR: variant is never constructed
Variant2,
}

Expand Down

0 comments on commit 7eb2e79

Please sign in to comment.