Remove redundant descr/descriptive_variant methods from HIR.

This commit is contained in:
Eduard-Mihai Burtescu 2020-04-17 23:00:12 +03:00 committed by mark
parent 18be5a08a2
commit d00f94ffc1
13 changed files with 34 additions and 81 deletions

View File

@ -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, _)
@ -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 {

View File

@ -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()
});
}
}
@ -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);
@ -618,13 +619,7 @@ 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);
}
@ -632,20 +627,14 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
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);
}
@ -658,7 +647,6 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
impl_item.hir_id,
impl_item.span,
impl_item.ident.name,
"associated const",
"used",
);
}
@ -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)
}

View File

@ -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));
}
}
}
@ -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);
}
}
@ -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());
}

View File

@ -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() {

View File

@ -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;

View File

@ -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() {}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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()
}
}
@ -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

View File

@ -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) {
@ -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);

View File

@ -1,4 +1,4 @@
#![feature(staged_api)]
//~^ ERROR crate has missing stability attribute
//~^ ERROR module has missing stability attribute
fn main() {}

View File

@ -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)]