From 6b37a795815af3cc4b8cd0b9a13108c060a9e214 Mon Sep 17 00:00:00 2001 From: hdelc Date: Tue, 2 Aug 2022 09:39:50 -0400 Subject: [PATCH] Refactor `Display` impl for `Target` to `Target::name` method --- compiler/rustc_hir/src/target.rs | 90 ++++++++++++++++---------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/compiler/rustc_hir/src/target.rs b/compiler/rustc_hir/src/target.rs index 96dd00ec5cf..622a234cd9f 100644 --- a/compiler/rustc_hir/src/target.rs +++ b/compiler/rustc_hir/src/target.rs @@ -60,51 +60,7 @@ pub enum Target { impl Display for Target { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match *self { - Target::ExternCrate => "extern crate", - Target::Use => "use", - Target::Static => "static item", - Target::Const => "constant item", - Target::Fn => "function", - Target::Closure => "closure", - Target::Mod => "module", - Target::ForeignMod => "foreign module", - Target::GlobalAsm => "global asm", - Target::TyAlias => "type alias", - Target::OpaqueTy => "opaque type", - Target::Enum => "enum", - Target::Variant => "enum variant", - Target::Struct => "struct", - Target::Field => "struct field", - Target::Union => "union", - Target::Trait => "trait", - Target::TraitAlias => "trait alias", - Target::Impl => "item", - Target::Expression => "expression", - Target::Statement => "statement", - Target::Arm => "match arm", - Target::AssocConst => "associated const", - Target::Method(kind) => match kind { - MethodKind::Inherent => "inherent method", - MethodKind::Trait { body: false } => "required trait method", - MethodKind::Trait { body: true } => "provided trait method", - }, - Target::AssocTy => "associated type", - Target::ForeignFn => "foreign function", - Target::ForeignStatic => "foreign static item", - Target::ForeignTy => "foreign type", - Target::GenericParam(kind) => match kind { - GenericParamKind::Type => "type parameter", - GenericParamKind::Lifetime => "lifetime parameter", - GenericParamKind::Const => "const parameter", - }, - Target::MacroDef => "macro def", - Target::Param => "function param", - } - ) + write!(f, "{}", Self::name(&self)) } } @@ -185,4 +141,48 @@ pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target { hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const), } } + + pub fn name(&self) -> &str { + match *self { + Target::ExternCrate => "extern crate", + Target::Use => "use", + Target::Static => "static item", + Target::Const => "constant item", + Target::Fn => "function", + Target::Closure => "closure", + Target::Mod => "module", + Target::ForeignMod => "foreign module", + Target::GlobalAsm => "global asm", + Target::TyAlias => "type alias", + Target::OpaqueTy => "opaque type", + Target::Enum => "enum", + Target::Variant => "enum variant", + Target::Struct => "struct", + Target::Field => "struct field", + Target::Union => "union", + Target::Trait => "trait", + Target::TraitAlias => "trait alias", + Target::Impl => "item", + Target::Expression => "expression", + Target::Statement => "statement", + Target::Arm => "match arm", + Target::AssocConst => "associated const", + Target::Method(kind) => match kind { + MethodKind::Inherent => "inherent method", + MethodKind::Trait { body: false } => "required trait method", + MethodKind::Trait { body: true } => "provided trait method", + }, + Target::AssocTy => "associated type", + Target::ForeignFn => "foreign function", + Target::ForeignStatic => "foreign static item", + Target::ForeignTy => "foreign type", + Target::GenericParam(kind) => match kind { + GenericParamKind::Type => "type parameter", + GenericParamKind::Lifetime => "lifetime parameter", + GenericParamKind::Const => "const parameter", + }, + Target::MacroDef => "macro def", + Target::Param => "function param", + } + } }