Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dead code lint to say "never constructed" for variants #46103

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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