Skip to content

Commit d00f94f

Browse files
eddybmark-i-m
authored andcommitted
Remove redundant descr/descriptive_variant methods from HIR.
1 parent 18be5a0 commit d00f94f

13 files changed

+34
-81
lines changed

src/librustc_hir/hir.rs

-31
Original file line numberDiff line numberDiff line change
@@ -2452,27 +2452,6 @@ pub enum ItemKind<'hir> {
24522452
}
24532453

24542454
impl ItemKind<'_> {
2455-
pub fn descr(&self) -> &str {
2456-
match *self {
2457-
ItemKind::ExternCrate(..) => "extern crate",
2458-
ItemKind::Use(..) => "`use` import",
2459-
ItemKind::Static(..) => "static item",
2460-
ItemKind::Const(..) => "constant item",
2461-
ItemKind::Fn(..) => "function",
2462-
ItemKind::Mod(..) => "module",
2463-
ItemKind::ForeignMod(..) => "extern block",
2464-
ItemKind::GlobalAsm(..) => "global asm item",
2465-
ItemKind::TyAlias(..) => "type alias",
2466-
ItemKind::OpaqueTy(..) => "opaque type",
2467-
ItemKind::Enum(..) => "enum",
2468-
ItemKind::Struct(..) => "struct",
2469-
ItemKind::Union(..) => "union",
2470-
ItemKind::Trait(..) => "trait",
2471-
ItemKind::TraitAlias(..) => "trait alias",
2472-
ItemKind::Impl { .. } => "implementation",
2473-
}
2474-
}
2475-
24762455
pub fn generics(&self) -> Option<&Generics<'_>> {
24772456
Some(match *self {
24782457
ItemKind::Fn(_, ref generics, _)
@@ -2551,16 +2530,6 @@ pub enum ForeignItemKind<'hir> {
25512530
Type,
25522531
}
25532532

2554-
impl ForeignItemKind<'hir> {
2555-
pub fn descriptive_variant(&self) -> &str {
2556-
match *self {
2557-
ForeignItemKind::Fn(..) => "foreign function",
2558-
ForeignItemKind::Static(..) => "foreign static item",
2559-
ForeignItemKind::Type => "foreign type",
2560-
}
2561-
}
2562-
}
2563-
25642533
/// A variable captured by a closure.
25652534
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable_Generic)]
25662535
pub struct Upvar {

src/librustc_passes/dead.rs

+8-26
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,13 @@ impl DeadVisitor<'tcx> {
553553
id: hir::HirId,
554554
span: rustc_span::Span,
555555
name: ast::Name,
556-
node_type: &str,
557556
participle: &str,
558557
) {
559558
if !name.as_str().starts_with('_') {
560559
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
561-
lint.build(&format!("{} is never {}: `{}`", node_type, participle, name)).emit()
560+
let def_id = self.tcx.hir().local_def_id(id);
561+
let descr = self.tcx.def_kind(def_id).descr(def_id);
562+
lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit()
562563
});
563564
}
564565
}
@@ -604,7 +605,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
604605
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
605606
_ => "used",
606607
};
607-
self.warn_dead_code(item.hir_id, span, item.ident.name, item.kind.descr(), participle);
608+
self.warn_dead_code(item.hir_id, span, item.ident.name, participle);
608609
} else {
609610
// Only continue if we didn't warn
610611
intravisit::walk_item(self, item);
@@ -618,34 +619,22 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
618619
id: hir::HirId,
619620
) {
620621
if self.should_warn_about_variant(&variant) {
621-
self.warn_dead_code(
622-
variant.id,
623-
variant.span,
624-
variant.ident.name,
625-
"variant",
626-
"constructed",
627-
);
622+
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
628623
} else {
629624
intravisit::walk_variant(self, variant, g, id);
630625
}
631626
}
632627

633628
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
634629
if self.should_warn_about_foreign_item(fi) {
635-
self.warn_dead_code(
636-
fi.hir_id,
637-
fi.span,
638-
fi.ident.name,
639-
fi.kind.descriptive_variant(),
640-
"used",
641-
);
630+
self.warn_dead_code(fi.hir_id, fi.span, fi.ident.name, "used");
642631
}
643632
intravisit::walk_foreign_item(self, fi);
644633
}
645634

646635
fn visit_struct_field(&mut self, field: &'tcx hir::StructField<'tcx>) {
647636
if self.should_warn_about_field(&field) {
648-
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "field", "read");
637+
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
649638
}
650639
intravisit::walk_struct_field(self, field);
651640
}
@@ -658,7 +647,6 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
658647
impl_item.hir_id,
659648
impl_item.span,
660649
impl_item.ident.name,
661-
"associated const",
662650
"used",
663651
);
664652
}
@@ -667,13 +655,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
667655
hir::ImplItemKind::Fn(_, body_id) => {
668656
if !self.symbol_is_live(impl_item.hir_id) {
669657
let span = self.tcx.sess.source_map().guess_head_span(impl_item.span);
670-
self.warn_dead_code(
671-
impl_item.hir_id,
672-
span,
673-
impl_item.ident.name,
674-
"method",
675-
"used",
676-
);
658+
self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used");
677659
}
678660
self.visit_nested_body(body_id)
679661
}

src/librustc_passes/stability.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,14 @@ struct MissingStabilityAnnotations<'a, 'tcx> {
337337
}
338338

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

365-
_ => self.check_missing_stability(i.hir_id, i.span, i.kind.descr()),
367+
_ => self.check_missing_stability(i.hir_id, i.span),
366368
}
367369

368370
intravisit::walk_item(self, i)
369371
}
370372

371373
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
372-
self.check_missing_stability(ti.hir_id, ti.span, "item");
374+
self.check_missing_stability(ti.hir_id, ti.span);
373375
intravisit::walk_trait_item(self, ti);
374376
}
375377

376378
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
377379
let impl_def_id = self.tcx.hir().local_def_id(self.tcx.hir().get_parent_item(ii.hir_id));
378380
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
379-
self.check_missing_stability(ii.hir_id, ii.span, "item");
381+
self.check_missing_stability(ii.hir_id, ii.span);
380382
}
381383
intravisit::walk_impl_item(self, ii);
382384
}
383385

384386
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
385-
self.check_missing_stability(var.id, var.span, "variant");
387+
self.check_missing_stability(var.id, var.span);
386388
intravisit::walk_variant(self, var, g, item_id);
387389
}
388390

389391
fn visit_struct_field(&mut self, s: &'tcx StructField<'tcx>) {
390-
self.check_missing_stability(s.hir_id, s.span, "field");
392+
self.check_missing_stability(s.hir_id, s.span);
391393
intravisit::walk_struct_field(self, s);
392394
}
393395

394396
fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
395-
self.check_missing_stability(i.hir_id, i.span, i.kind.descriptive_variant());
397+
self.check_missing_stability(i.hir_id, i.span);
396398
intravisit::walk_foreign_item(self, i);
397399
}
398400

399401
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
400-
self.check_missing_stability(md.hir_id, md.span, "macro");
402+
self.check_missing_stability(md.hir_id, md.span);
401403
}
402404
}
403405

@@ -585,7 +587,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
585587
if tcx.stability().staged_api[&LOCAL_CRATE] {
586588
let krate = tcx.hir().krate();
587589
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
588-
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span, "crate");
590+
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span);
589591
intravisit::walk_crate(&mut missing, krate);
590592
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
591593
}

src/test/ui/associated-const/associated-const-dead-code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct MyFoo;
44

55
impl MyFoo {
66
const BAR: u32 = 1;
7-
//~^ ERROR associated const is never used: `BAR`
7+
//~^ ERROR associated constant is never used: `BAR`
88
}
99

1010
fn main() {

src/test/ui/associated-const/associated-const-dead-code.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: associated const is never used: `BAR`
1+
error: associated constant is never used: `BAR`
22
--> $DIR/associated-const-dead-code.rs:6:5
33
|
44
LL | const BAR: u32 = 1;

src/test/ui/issues/issue-17718-const-naming.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
const foo: isize = 3;
55
//~^ ERROR: should have an upper case name
6-
//~^^ ERROR: constant item is never used
6+
//~^^ ERROR: constant is never used
77

88
fn main() {}

src/test/ui/issues/issue-17718-const-naming.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: constant item is never used: `foo`
1+
error: constant is never used: `foo`
22
--> $DIR/issue-17718-const-naming.rs:4:1
33
|
44
LL | const foo: isize = 3;

src/test/ui/lint/dead-code/lint-dead-code-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ mod foo2 {
1717
}
1818

1919
pub static pub_static: isize = 0;
20-
static priv_static: isize = 0; //~ ERROR: static item is never used
20+
static priv_static: isize = 0; //~ ERROR: static is never used
2121
const used_static: isize = 0;
2222
pub static used_static2: isize = used_static;
2323
const USED_STATIC: isize = 0;
2424
const STATIC_USED_IN_ENUM_DISCRIMINANT: isize = 10;
2525

2626
pub const pub_const: isize = 0;
27-
const priv_const: isize = 0; //~ ERROR: constant item is never used
27+
const priv_const: isize = 0; //~ ERROR: constant is never used
2828
const used_const: isize = 0;
2929
pub const used_const2: isize = used_const;
3030
const USED_CONST: isize = 1;

src/test/ui/lint/dead-code/lint-dead-code-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ note: the lint level is defined here
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

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

19-
error: constant item is never used: `priv_const`
19+
error: constant is never used: `priv_const`
2020
--> $DIR/lint-dead-code-1.rs:27:1
2121
|
2222
LL | const priv_const: isize = 0;

src/test/ui/lint/dead-code/lint-dead-code-3.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern {
1212

1313
struct Foo; //~ ERROR: struct is never constructed
1414
impl Foo {
15-
fn foo(&self) { //~ ERROR: method is never used
15+
fn foo(&self) { //~ ERROR: associated function is never used
1616
bar()
1717
}
1818
}
@@ -58,7 +58,7 @@ mod blah {
5858

5959
enum c_void {} //~ ERROR: enum is never used
6060
extern {
61-
fn free(p: *const c_void); //~ ERROR: foreign function is never used
61+
fn free(p: *const c_void); //~ ERROR: function is never used
6262
}
6363

6464
// Check provided method

src/test/ui/lint/dead-code/lint-dead-code-3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: the lint level is defined here
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

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

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

44
fn main() {}

src/test/ui/stability-attribute/missing-stability-attr-at-top-level.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: crate has missing stability attribute
1+
error: module has missing stability attribute
22
--> $DIR/missing-stability-attr-at-top-level.rs:1:1
33
|
44
LL | / #![feature(staged_api)]

0 commit comments

Comments
 (0)