Skip to content

Commit

Permalink
Remove redundant descr/descriptive_variant methods from HIR.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb authored and mark-i-m committed Apr 24, 2020
1 parent 18be5a0 commit d00f94f
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 81 deletions.
31 changes: 0 additions & 31 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2452,27 +2452,6 @@ pub enum ItemKind<'hir> {
}

impl ItemKind<'_> {
pub fn descr(&self) -> &str {
match *self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "`use` import",
ItemKind::Static(..) => "static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
ItemKind::ForeignMod(..) => "extern block",
ItemKind::GlobalAsm(..) => "global asm item",
ItemKind::TyAlias(..) => "type alias",
ItemKind::OpaqueTy(..) => "opaque type",
ItemKind::Enum(..) => "enum",
ItemKind::Struct(..) => "struct",
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl { .. } => "implementation",
}
}

pub fn generics(&self) -> Option<&Generics<'_>> {
Some(match *self {
ItemKind::Fn(_, ref generics, _)
Expand Down Expand Up @@ -2551,16 +2530,6 @@ pub enum ForeignItemKind<'hir> {
Type,
}

impl ForeignItemKind<'hir> {
pub fn descriptive_variant(&self) -> &str {
match *self {
ForeignItemKind::Fn(..) => "foreign function",
ForeignItemKind::Static(..) => "foreign static item",
ForeignItemKind::Type => "foreign type",
}
}
}

/// A variable captured by a closure.
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable_Generic)]
pub struct Upvar {
Expand Down
34 changes: 8 additions & 26 deletions src/librustc_passes/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,13 @@ impl DeadVisitor<'tcx> {
id: hir::HirId,
span: rustc_span::Span,
name: ast::Name,
node_type: &str,
participle: &str,
) {
if !name.as_str().starts_with('_') {
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
lint.build(&format!("{} is never {}: `{}`", node_type, participle, name)).emit()
let def_id = self.tcx.hir().local_def_id(id);
let descr = self.tcx.def_kind(def_id).descr(def_id);
lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit()
});
}
}
Expand Down Expand Up @@ -604,7 +605,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
_ => "used",
};
self.warn_dead_code(item.hir_id, span, item.ident.name, item.kind.descr(), participle);
self.warn_dead_code(item.hir_id, span, item.ident.name, participle);
} else {
// Only continue if we didn't warn
intravisit::walk_item(self, item);
Expand All @@ -618,34 +619,22 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
id: hir::HirId,
) {
if self.should_warn_about_variant(&variant) {
self.warn_dead_code(
variant.id,
variant.span,
variant.ident.name,
"variant",
"constructed",
);
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
} else {
intravisit::walk_variant(self, variant, g, id);
}
}

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

fn visit_struct_field(&mut self, field: &'tcx hir::StructField<'tcx>) {
if self.should_warn_about_field(&field) {
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "field", "read");
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
}
intravisit::walk_struct_field(self, field);
}
Expand All @@ -658,7 +647,6 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
impl_item.hir_id,
impl_item.span,
impl_item.ident.name,
"associated const",
"used",
);
}
Expand All @@ -667,13 +655,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
hir::ImplItemKind::Fn(_, body_id) => {
if !self.symbol_is_live(impl_item.hir_id) {
let span = self.tcx.sess.source_map().guess_head_span(impl_item.span);
self.warn_dead_code(
impl_item.hir_id,
span,
impl_item.ident.name,
"method",
"used",
);
self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used");
}
self.visit_nested_body(body_id)
}
Expand Down
22 changes: 12 additions & 10 deletions src/librustc_passes/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,14 @@ struct MissingStabilityAnnotations<'a, 'tcx> {
}

impl<'a, 'tcx> MissingStabilityAnnotations<'a, 'tcx> {
fn check_missing_stability(&self, hir_id: HirId, span: Span, name: &str) {
fn check_missing_stability(&self, hir_id: HirId, span: Span) {
let stab = self.tcx.stability().local_stability(hir_id);
let is_error =
!self.tcx.sess.opts.test && stab.is_none() && self.access_levels.is_reachable(hir_id);
if is_error {
self.tcx.sess.span_err(span, &format!("{} has missing stability attribute", name));
let def_id = self.tcx.hir().local_def_id(hir_id);
let descr = self.tcx.def_kind(def_id).descr(def_id);
self.tcx.sess.span_err(span, &format!("{} has missing stability attribute", descr));
}
}
}
Expand All @@ -362,42 +364,42 @@ impl<'a, 'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'a, 'tcx> {
// optional. They inherit stability from their parents when unannotated.
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..) => {}

_ => self.check_missing_stability(i.hir_id, i.span, i.kind.descr()),
_ => self.check_missing_stability(i.hir_id, i.span),
}

intravisit::walk_item(self, i)
}

fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
self.check_missing_stability(ti.hir_id, ti.span, "item");
self.check_missing_stability(ti.hir_id, ti.span);
intravisit::walk_trait_item(self, ti);
}

fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
let impl_def_id = self.tcx.hir().local_def_id(self.tcx.hir().get_parent_item(ii.hir_id));
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
self.check_missing_stability(ii.hir_id, ii.span, "item");
self.check_missing_stability(ii.hir_id, ii.span);
}
intravisit::walk_impl_item(self, ii);
}

fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
self.check_missing_stability(var.id, var.span, "variant");
self.check_missing_stability(var.id, var.span);
intravisit::walk_variant(self, var, g, item_id);
}

fn visit_struct_field(&mut self, s: &'tcx StructField<'tcx>) {
self.check_missing_stability(s.hir_id, s.span, "field");
self.check_missing_stability(s.hir_id, s.span);
intravisit::walk_struct_field(self, s);
}

fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
self.check_missing_stability(i.hir_id, i.span, i.kind.descriptive_variant());
self.check_missing_stability(i.hir_id, i.span);
intravisit::walk_foreign_item(self, i);
}

fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
self.check_missing_stability(md.hir_id, md.span, "macro");
self.check_missing_stability(md.hir_id, md.span);
}
}

Expand Down Expand Up @@ -585,7 +587,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
if tcx.stability().staged_api[&LOCAL_CRATE] {
let krate = tcx.hir().krate();
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span, "crate");
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span);
intravisit::walk_crate(&mut missing, krate);
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/associated-const/associated-const-dead-code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct MyFoo;

impl MyFoo {
const BAR: u32 = 1;
//~^ ERROR associated const is never used: `BAR`
//~^ ERROR associated constant is never used: `BAR`
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: associated const is never used: `BAR`
error: associated constant is never used: `BAR`
--> $DIR/associated-const-dead-code.rs:6:5
|
LL | const BAR: u32 = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-17718-const-naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

const foo: isize = 3;
//~^ ERROR: should have an upper case name
//~^^ ERROR: constant item is never used
//~^^ ERROR: constant is never used

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-17718-const-naming.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: constant item is never used: `foo`
error: constant is never used: `foo`
--> $DIR/issue-17718-const-naming.rs:4:1
|
LL | const foo: isize = 3;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/dead-code/lint-dead-code-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ mod foo2 {
}

pub static pub_static: isize = 0;
static priv_static: isize = 0; //~ ERROR: static item is never used
static priv_static: isize = 0; //~ ERROR: static is never used
const used_static: isize = 0;
pub static used_static2: isize = used_static;
const USED_STATIC: isize = 0;
const STATIC_USED_IN_ENUM_DISCRIMINANT: isize = 10;

pub const pub_const: isize = 0;
const priv_const: isize = 0; //~ ERROR: constant item is never used
const priv_const: isize = 0; //~ ERROR: constant is never used
const used_const: isize = 0;
pub const used_const2: isize = used_const;
const USED_CONST: isize = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/dead-code/lint-dead-code-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ note: the lint level is defined here
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: static item is never used: `priv_static`
error: static is never used: `priv_static`
--> $DIR/lint-dead-code-1.rs:20:1
|
LL | static priv_static: isize = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: constant item is never used: `priv_const`
error: constant is never used: `priv_const`
--> $DIR/lint-dead-code-1.rs:27:1
|
LL | const priv_const: isize = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/dead-code/lint-dead-code-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern {

struct Foo; //~ ERROR: struct is never constructed
impl Foo {
fn foo(&self) { //~ ERROR: method is never used
fn foo(&self) { //~ ERROR: associated function is never used
bar()
}
}
Expand Down Expand Up @@ -58,7 +58,7 @@ mod blah {

enum c_void {} //~ ERROR: enum is never used
extern {
fn free(p: *const c_void); //~ ERROR: foreign function is never used
fn free(p: *const c_void); //~ ERROR: function is never used
}

// Check provided method
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/dead-code/lint-dead-code-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ note: the lint level is defined here
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: method is never used: `foo`
error: associated function is never used: `foo`
--> $DIR/lint-dead-code-3.rs:15:5
|
LL | fn foo(&self) {
Expand All @@ -28,7 +28,7 @@ error: enum is never used: `c_void`
LL | enum c_void {}
| ^^^^^^

error: foreign function is never used: `free`
error: function is never used: `free`
--> $DIR/lint-dead-code-3.rs:61:5
|
LL | fn free(p: *const c_void);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(staged_api)]
//~^ ERROR crate has missing stability attribute
//~^ ERROR module has missing stability attribute

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: crate has missing stability attribute
error: module has missing stability attribute
--> $DIR/missing-stability-attr-at-top-level.rs:1:1
|
LL | / #![feature(staged_api)]
Expand Down

0 comments on commit d00f94f

Please sign in to comment.