@@ -3182,6 +3182,20 @@ pub enum Node<'hir> {
3182
3182
}
3183
3183
3184
3184
impl < ' hir > Node < ' hir > {
3185
+ /// Get the identifier of this `Node`, if applicable.
3186
+ ///
3187
+ /// # Edge cases
3188
+ ///
3189
+ /// Calling `.ident()` on a [`Node::Ctor`] will return `None`
3190
+ /// because `Ctor`s do not have identifiers themselves.
3191
+ /// Instead, call `.ident()` on the parent struct/variant, like so:
3192
+ ///
3193
+ /// ```ignore (illustrative)
3194
+ /// ctor
3195
+ /// .ctor_hir_id()
3196
+ /// .and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id)))
3197
+ /// .and_then(|parent| parent.ident())
3198
+ /// ```
3185
3199
pub fn ident ( & self ) -> Option < Ident > {
3186
3200
match self {
3187
3201
Node :: TraitItem ( TraitItem { ident, .. } )
@@ -3190,8 +3204,25 @@ impl<'hir> Node<'hir> {
3190
3204
| Node :: Field ( FieldDef { ident, .. } )
3191
3205
| Node :: Variant ( Variant { ident, .. } )
3192
3206
| Node :: MacroDef ( MacroDef { ident, .. } )
3193
- | Node :: Item ( Item { ident, .. } ) => Some ( * ident) ,
3194
- _ => None ,
3207
+ | Node :: Item ( Item { ident, .. } )
3208
+ | Node :: PathSegment ( PathSegment { ident, .. } ) => Some ( * ident) ,
3209
+ Node :: Lifetime ( lt) => Some ( lt. name . ident ( ) ) ,
3210
+ Node :: GenericParam ( p) => Some ( p. name . ident ( ) ) ,
3211
+ Node :: Param ( ..)
3212
+ | Node :: AnonConst ( ..)
3213
+ | Node :: Expr ( ..)
3214
+ | Node :: Stmt ( ..)
3215
+ | Node :: Block ( ..)
3216
+ | Node :: Ctor ( ..)
3217
+ | Node :: Pat ( ..)
3218
+ | Node :: Binding ( ..)
3219
+ | Node :: Arm ( ..)
3220
+ | Node :: Local ( ..)
3221
+ | Node :: Visibility ( ..)
3222
+ | Node :: Crate ( ..)
3223
+ | Node :: Ty ( ..)
3224
+ | Node :: TraitRef ( ..)
3225
+ | Node :: Infer ( ..) => None ,
3195
3226
}
3196
3227
}
3197
3228
0 commit comments