@@ -246,7 +246,7 @@ impl<'hir> Map<'hir> {
246
246
} ,
247
247
Node :: Variant ( _) => DefKind :: Variant ,
248
248
Node :: Ctor ( variant_data) => {
249
- let ctor_of = match self . find ( self . parent_id ( hir_id) ) {
249
+ let ctor_of = match self . find_parent ( hir_id) {
250
250
Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
251
251
Some ( Node :: Variant ( ..) ) => def:: CtorOf :: Variant ,
252
252
_ => unreachable ! ( ) ,
@@ -257,7 +257,7 @@ impl<'hir> Map<'hir> {
257
257
}
258
258
}
259
259
Node :: AnonConst ( _) => {
260
- let inline = match self . find ( self . parent_id ( hir_id) ) {
260
+ let inline = match self . find_parent ( hir_id) {
261
261
Some ( Node :: Expr ( & Expr {
262
262
kind : ExprKind :: ConstBlock ( ref anon_const) , ..
263
263
} ) ) if anon_const. hir_id == hir_id => true ,
@@ -317,6 +317,14 @@ impl<'hir> Map<'hir> {
317
317
. unwrap_or_else ( || bug ! ( "No parent for node {:?}" , self . node_to_string( hir_id) ) )
318
318
}
319
319
320
+ pub fn get_parent ( self , hir_id : HirId ) -> Node < ' hir > {
321
+ self . get ( self . parent_id ( hir_id) )
322
+ }
323
+
324
+ pub fn find_parent ( self , hir_id : HirId ) -> Option < Node < ' hir > > {
325
+ self . find ( self . opt_parent_id ( hir_id) ?)
326
+ }
327
+
320
328
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
321
329
pub fn find ( self , id : HirId ) -> Option < Node < ' hir > > {
322
330
if id. local_id == ItemLocalId :: from_u32 ( 0 ) {
@@ -664,7 +672,7 @@ impl<'hir> Map<'hir> {
664
672
665
673
/// Checks if the node is left-hand side of an assignment.
666
674
pub fn is_lhs ( self , id : HirId ) -> bool {
667
- match self . find ( self . parent_id ( id ) ) {
675
+ match self . find_parent ( id ) {
668
676
Some ( Node :: Expr ( expr) ) => match expr. kind {
669
677
ExprKind :: Assign ( lhs, _rhs, _span) => lhs. hir_id == id,
670
678
_ => false ,
@@ -892,7 +900,7 @@ impl<'hir> Map<'hir> {
892
900
Node :: Pat ( & Pat { kind : PatKind :: Binding ( _, _, ident, _) , .. } ) => Some ( ident) ,
893
901
// A `Ctor` doesn't have an identifier itself, but its parent
894
902
// struct/variant does. Compare with `hir::Map::opt_span`.
895
- Node :: Ctor ( ..) => match self . find ( self . parent_id ( id ) ) ? {
903
+ Node :: Ctor ( ..) => match self . find_parent ( id ) ? {
896
904
Node :: Item ( item) => Some ( item. ident ) ,
897
905
Node :: Variant ( variant) => Some ( variant. ident ) ,
898
906
_ => unreachable ! ( ) ,
@@ -1093,7 +1101,7 @@ impl<'hir> Map<'hir> {
1093
1101
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
1094
1102
/// called with the HirId for the `{ ... }` anon const
1095
1103
pub fn opt_const_param_default_param_def_id ( self , anon_const : HirId ) -> Option < LocalDefId > {
1096
- match self . get ( self . parent_id ( anon_const) ) {
1104
+ match self . get_parent ( anon_const) {
1097
1105
Node :: GenericParam ( GenericParam {
1098
1106
def_id : param_id,
1099
1107
kind : GenericParamKind :: Const { .. } ,
0 commit comments