From 1a881a487b3f217bb1e47a8a16925ca519051583 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 19 Apr 2022 21:25:26 +0200 Subject: [PATCH] Introduce opt_ident. --- compiler/rustc_middle/src/hir/map/mod.rs | 25 ++++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 1cc3b8987b3..f3cb9a16df5 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -910,8 +910,10 @@ impl<'hir> Map<'hir> { } } - pub(super) fn opt_ident_span(self, id: HirId) -> Option { - let ident = match self.get(id) { + #[inline] + fn opt_ident(self, id: HirId) -> Option { + match self.get(id) { + Node::Binding(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident), // A `Ctor` doesn't have an identifier itself, but its parent // struct/variant does. Compare with `hir::Map::opt_span`. Node::Ctor(..) => match self.find(self.get_parent_node(id))? { @@ -920,20 +922,17 @@ impl<'hir> Map<'hir> { _ => unreachable!(), }, node => node.ident(), - }; - ident.map(|ident| ident.span) + } } + #[inline] + pub(super) fn opt_ident_span(self, id: HirId) -> Option { + self.opt_ident(id).map(|ident| ident.span) + } + + #[inline] pub fn opt_name(self, id: HirId) -> Option { - match self.get(id) { - Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => Some(l.name), - Node::Ctor(..) => match self.find(self.get_parent_node(id))? { - Node::Item(item) => Some(item.ident.name), - Node::Variant(variant) => Some(variant.ident.name), - _ => unreachable!(), - }, - node => node.ident().map(|i| i.name), - } + self.opt_ident(id).map(|ident| ident.name) } pub fn name(self, id: HirId) -> Symbol {