Use an exhaustive match in Node::ident()
and add docs
This should cause a compiler error in the future if more variants are added without `Node::ident()` being updated.
This commit is contained in:
parent
0fa3b4f940
commit
02ed23c031
1 changed files with 33 additions and 2 deletions
|
@ -3182,6 +3182,20 @@ pub enum Node<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> Node<'hir> {
|
||||
/// Get the identifier of this `Node`, if applicable.
|
||||
///
|
||||
/// # Edge cases
|
||||
///
|
||||
/// Calling `.ident()` on a [`Node::Ctor`] will return `None`
|
||||
/// because `Ctor`s do not have identifiers themselves.
|
||||
/// Instead, call `.ident()` on the parent struct/variant, like so:
|
||||
///
|
||||
/// ```ignore (illustrative)
|
||||
/// ctor
|
||||
/// .ctor_hir_id()
|
||||
/// .and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id)))
|
||||
/// .and_then(|parent| parent.ident())
|
||||
/// ```
|
||||
pub fn ident(&self) -> Option<Ident> {
|
||||
match self {
|
||||
Node::TraitItem(TraitItem { ident, .. })
|
||||
|
@ -3190,8 +3204,25 @@ impl<'hir> Node<'hir> {
|
|||
| Node::Field(FieldDef { ident, .. })
|
||||
| Node::Variant(Variant { ident, .. })
|
||||
| Node::MacroDef(MacroDef { ident, .. })
|
||||
| Node::Item(Item { ident, .. }) => Some(*ident),
|
||||
_ => None,
|
||||
| Node::Item(Item { ident, .. })
|
||||
| Node::PathSegment(PathSegment { ident, .. }) => Some(*ident),
|
||||
Node::Lifetime(lt) => Some(lt.name.ident()),
|
||||
Node::GenericParam(p) => Some(p.name.ident()),
|
||||
Node::Param(..)
|
||||
| Node::AnonConst(..)
|
||||
| Node::Expr(..)
|
||||
| Node::Stmt(..)
|
||||
| Node::Block(..)
|
||||
| Node::Ctor(..)
|
||||
| Node::Pat(..)
|
||||
| Node::Binding(..)
|
||||
| Node::Arm(..)
|
||||
| Node::Local(..)
|
||||
| Node::Visibility(..)
|
||||
| Node::Crate(..)
|
||||
| Node::Ty(..)
|
||||
| Node::TraitRef(..)
|
||||
| Node::Infer(..) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue