Rollup merge of #136219 - yotamofek:pr/hir-cleanup, r=compiler-errors

Misc. `rustc_hir` cleanups 🧹

Each commit stands on its own, but I think all of them make the code a bit cleaner
This commit is contained in:
Matthias Krüger 2025-02-06 21:56:26 +01:00 committed by GitHub
commit 3e54c2eb75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 56 deletions

View file

@ -206,7 +206,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;
impl Path<'_> { impl Path<'_> {
pub fn is_global(&self) -> bool { pub fn is_global(&self) -> bool {
!self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot)
} }
} }
@ -1061,10 +1061,7 @@ impl Attribute {
pub fn value_lit(&self) -> Option<&MetaItemLit> { pub fn value_lit(&self) -> Option<&MetaItemLit> {
match &self.kind { match &self.kind {
AttrKind::Normal(n) => match n.as_ref() { AttrKind::Normal(box AttrItem { args: AttrArgs::Eq { expr, .. }, .. }) => Some(expr),
AttrItem { args: AttrArgs::Eq { expr, .. }, .. } => Some(expr),
_ => None,
},
_ => None, _ => None,
} }
} }
@ -1077,13 +1074,10 @@ impl AttributeExt for Attribute {
fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> { fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> {
match &self.kind { match &self.kind {
AttrKind::Normal(n) => match n.as_ref() { AttrKind::Normal(box AttrItem { args: AttrArgs::Delimited(d), .. }) => {
AttrItem { args: AttrArgs::Delimited(d), .. } => {
ast::MetaItemKind::list_from_tokens(d.tokens.clone()) ast::MetaItemKind::list_from_tokens(d.tokens.clone())
} }
_ => None, _ => None,
},
_ => None,
} }
} }
@ -1098,23 +1092,16 @@ impl AttributeExt for Attribute {
/// For a single-segment attribute, returns its name; otherwise, returns `None`. /// For a single-segment attribute, returns its name; otherwise, returns `None`.
fn ident(&self) -> Option<Ident> { fn ident(&self) -> Option<Ident> {
match &self.kind { match &self.kind {
AttrKind::Normal(n) => { AttrKind::Normal(box AttrItem {
if let [ident] = n.path.segments.as_ref() { path: AttrPath { segments: box [ident], .. }, ..
Some(*ident) }) => Some(*ident),
} else { _ => None,
None
}
}
AttrKind::DocComment(..) => None,
} }
} }
fn path_matches(&self, name: &[Symbol]) -> bool { fn path_matches(&self, name: &[Symbol]) -> bool {
match &self.kind { match &self.kind {
AttrKind::Normal(n) => { AttrKind::Normal(n) => n.path.segments.iter().map(|segment| &segment.name).eq(name),
n.path.segments.len() == name.len()
&& n.path.segments.iter().zip(name).all(|(s, n)| s.name == *n)
}
AttrKind::DocComment(..) => false, AttrKind::DocComment(..) => false,
} }
} }
@ -1128,12 +1115,7 @@ impl AttributeExt for Attribute {
} }
fn is_word(&self) -> bool { fn is_word(&self) -> bool {
match &self.kind { matches!(self.kind, AttrKind::Normal(box AttrItem { args: AttrArgs::Empty, .. }))
AttrKind::Normal(n) => {
matches!(n.args, AttrArgs::Empty)
}
AttrKind::DocComment(..) => false,
}
} }
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> { fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
@ -1990,7 +1972,7 @@ impl fmt::Display for ConstContext {
} }
// NOTE: `IntoDiagArg` impl for `ConstContext` lives in `rustc_errors` // NOTE: `IntoDiagArg` impl for `ConstContext` lives in `rustc_errors`
// due to a cyclical dependency between hir that crate. // due to a cyclical dependency between hir and that crate.
/// A literal. /// A literal.
pub type Lit = Spanned<LitKind>; pub type Lit = Spanned<LitKind>;
@ -3604,11 +3586,11 @@ impl<'hir> FnRetTy<'hir> {
} }
pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> { pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> {
if let Self::Return(ty) = self { if let Self::Return(ty) = self
if ty.is_suggestable_infer_ty() { && ty.is_suggestable_infer_ty()
{
return Some(*ty); return Some(*ty);
} }
}
None None
} }
} }
@ -3976,11 +3958,11 @@ pub struct FnHeader {
impl FnHeader { impl FnHeader {
pub fn is_async(&self) -> bool { pub fn is_async(&self) -> bool {
matches!(&self.asyncness, IsAsync::Async(_)) matches!(self.asyncness, IsAsync::Async(_))
} }
pub fn is_const(&self) -> bool { pub fn is_const(&self) -> bool {
matches!(&self.constness, Constness::Const) matches!(self.constness, Constness::Const)
} }
pub fn is_unsafe(&self) -> bool { pub fn is_unsafe(&self) -> bool {
@ -4076,16 +4058,16 @@ pub struct Impl<'hir> {
impl ItemKind<'_> { impl ItemKind<'_> {
pub fn generics(&self) -> Option<&Generics<'_>> { pub fn generics(&self) -> Option<&Generics<'_>> {
Some(match *self { Some(match self {
ItemKind::Fn { ref generics, .. } ItemKind::Fn { generics, .. }
| ItemKind::TyAlias(_, ref generics) | ItemKind::TyAlias(_, generics)
| ItemKind::Const(_, ref generics, _) | ItemKind::Const(_, generics, _)
| ItemKind::Enum(_, ref generics) | ItemKind::Enum(_, generics)
| ItemKind::Struct(_, ref generics) | ItemKind::Struct(_, generics)
| ItemKind::Union(_, ref generics) | ItemKind::Union(_, generics)
| ItemKind::Trait(_, _, ref generics, _, _) | ItemKind::Trait(_, _, generics, _, _)
| ItemKind::TraitAlias(ref generics, _) | ItemKind::TraitAlias(generics, _)
| ItemKind::Impl(Impl { ref generics, .. }) => generics, | ItemKind::Impl(Impl { generics, .. }) => generics,
_ => return None, _ => return None,
}) })
} }
@ -4484,16 +4466,14 @@ impl<'hir> Node<'hir> {
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`. /// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> { pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
match self { if let Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) = self
Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) && let Some(trait_ref) = impl_block.of_trait
if impl_block && let Some(trait_id) = trait_ref.trait_def_id()
.of_trait && trait_id == trait_def_id
.and_then(|trait_ref| trait_ref.trait_def_id())
.is_some_and(|trait_id| trait_id == trait_def_id) =>
{ {
Some(impl_block) Some(impl_block)
} } else {
_ => None, None
} }
} }

View file

@ -5,6 +5,7 @@
// tidy-alphabetical-start // tidy-alphabetical-start
#![allow(internal_features)] #![allow(internal_features)]
#![feature(associated_type_defaults)] #![feature(associated_type_defaults)]
#![feature(box_patterns)]
#![feature(closure_track_caller)] #![feature(closure_track_caller)]
#![feature(debug_closure_helpers)] #![feature(debug_closure_helpers)]
#![feature(exhaustive_patterns)] #![feature(exhaustive_patterns)]