Skip to content

Commit 9315bdc

Browse files
authored
Rollup merge of rust-lang#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
2 parents 6df1002 + 056fe96 commit 9315bdc

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

compiler/rustc_hir/src/hir.rs

+36-56
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;
206206

207207
impl Path<'_> {
208208
pub fn is_global(&self) -> bool {
209-
!self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot
209+
self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot)
210210
}
211211
}
212212

@@ -1061,10 +1061,7 @@ impl Attribute {
10611061

10621062
pub fn value_lit(&self) -> Option<&MetaItemLit> {
10631063
match &self.kind {
1064-
AttrKind::Normal(n) => match n.as_ref() {
1065-
AttrItem { args: AttrArgs::Eq { expr, .. }, .. } => Some(expr),
1066-
_ => None,
1067-
},
1064+
AttrKind::Normal(box AttrItem { args: AttrArgs::Eq { expr, .. }, .. }) => Some(expr),
10681065
_ => None,
10691066
}
10701067
}
@@ -1077,12 +1074,9 @@ impl AttributeExt for Attribute {
10771074

10781075
fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> {
10791076
match &self.kind {
1080-
AttrKind::Normal(n) => match n.as_ref() {
1081-
AttrItem { args: AttrArgs::Delimited(d), .. } => {
1082-
ast::MetaItemKind::list_from_tokens(d.tokens.clone())
1083-
}
1084-
_ => None,
1085-
},
1077+
AttrKind::Normal(box AttrItem { args: AttrArgs::Delimited(d), .. }) => {
1078+
ast::MetaItemKind::list_from_tokens(d.tokens.clone())
1079+
}
10861080
_ => None,
10871081
}
10881082
}
@@ -1098,23 +1092,16 @@ impl AttributeExt for Attribute {
10981092
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
10991093
fn ident(&self) -> Option<Ident> {
11001094
match &self.kind {
1101-
AttrKind::Normal(n) => {
1102-
if let [ident] = n.path.segments.as_ref() {
1103-
Some(*ident)
1104-
} else {
1105-
None
1106-
}
1107-
}
1108-
AttrKind::DocComment(..) => None,
1095+
AttrKind::Normal(box AttrItem {
1096+
path: AttrPath { segments: box [ident], .. }, ..
1097+
}) => Some(*ident),
1098+
_ => None,
11091099
}
11101100
}
11111101

11121102
fn path_matches(&self, name: &[Symbol]) -> bool {
11131103
match &self.kind {
1114-
AttrKind::Normal(n) => {
1115-
n.path.segments.len() == name.len()
1116-
&& n.path.segments.iter().zip(name).all(|(s, n)| s.name == *n)
1117-
}
1104+
AttrKind::Normal(n) => n.path.segments.iter().map(|segment| &segment.name).eq(name),
11181105
AttrKind::DocComment(..) => false,
11191106
}
11201107
}
@@ -1128,12 +1115,7 @@ impl AttributeExt for Attribute {
11281115
}
11291116

11301117
fn is_word(&self) -> bool {
1131-
match &self.kind {
1132-
AttrKind::Normal(n) => {
1133-
matches!(n.args, AttrArgs::Empty)
1134-
}
1135-
AttrKind::DocComment(..) => false,
1136-
}
1118+
matches!(self.kind, AttrKind::Normal(box AttrItem { args: AttrArgs::Empty, .. }))
11371119
}
11381120

11391121
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
@@ -1990,7 +1972,7 @@ impl fmt::Display for ConstContext {
19901972
}
19911973

19921974
// NOTE: `IntoDiagArg` impl for `ConstContext` lives in `rustc_errors`
1993-
// due to a cyclical dependency between hir that crate.
1975+
// due to a cyclical dependency between hir and that crate.
19941976

19951977
/// A literal.
19961978
pub type Lit = Spanned<LitKind>;
@@ -3604,10 +3586,10 @@ impl<'hir> FnRetTy<'hir> {
36043586
}
36053587

36063588
pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> {
3607-
if let Self::Return(ty) = self {
3608-
if ty.is_suggestable_infer_ty() {
3609-
return Some(*ty);
3610-
}
3589+
if let Self::Return(ty) = self
3590+
&& ty.is_suggestable_infer_ty()
3591+
{
3592+
return Some(*ty);
36113593
}
36123594
None
36133595
}
@@ -3976,11 +3958,11 @@ pub struct FnHeader {
39763958

39773959
impl FnHeader {
39783960
pub fn is_async(&self) -> bool {
3979-
matches!(&self.asyncness, IsAsync::Async(_))
3961+
matches!(self.asyncness, IsAsync::Async(_))
39803962
}
39813963

39823964
pub fn is_const(&self) -> bool {
3983-
matches!(&self.constness, Constness::Const)
3965+
matches!(self.constness, Constness::Const)
39843966
}
39853967

39863968
pub fn is_unsafe(&self) -> bool {
@@ -4076,16 +4058,16 @@ pub struct Impl<'hir> {
40764058

40774059
impl ItemKind<'_> {
40784060
pub fn generics(&self) -> Option<&Generics<'_>> {
4079-
Some(match *self {
4080-
ItemKind::Fn { ref generics, .. }
4081-
| ItemKind::TyAlias(_, ref generics)
4082-
| ItemKind::Const(_, ref generics, _)
4083-
| ItemKind::Enum(_, ref generics)
4084-
| ItemKind::Struct(_, ref generics)
4085-
| ItemKind::Union(_, ref generics)
4086-
| ItemKind::Trait(_, _, ref generics, _, _)
4087-
| ItemKind::TraitAlias(ref generics, _)
4088-
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
4061+
Some(match self {
4062+
ItemKind::Fn { generics, .. }
4063+
| ItemKind::TyAlias(_, generics)
4064+
| ItemKind::Const(_, generics, _)
4065+
| ItemKind::Enum(_, generics)
4066+
| ItemKind::Struct(_, generics)
4067+
| ItemKind::Union(_, generics)
4068+
| ItemKind::Trait(_, _, generics, _, _)
4069+
| ItemKind::TraitAlias(generics, _)
4070+
| ItemKind::Impl(Impl { generics, .. }) => generics,
40894071
_ => return None,
40904072
})
40914073
}
@@ -4484,16 +4466,14 @@ impl<'hir> Node<'hir> {
44844466

44854467
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
44864468
pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
4487-
match self {
4488-
Node::Item(Item { kind: ItemKind::Impl(impl_block), .. })
4489-
if impl_block
4490-
.of_trait
4491-
.and_then(|trait_ref| trait_ref.trait_def_id())
4492-
.is_some_and(|trait_id| trait_id == trait_def_id) =>
4493-
{
4494-
Some(impl_block)
4495-
}
4496-
_ => None,
4469+
if let Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) = self
4470+
&& let Some(trait_ref) = impl_block.of_trait
4471+
&& let Some(trait_id) = trait_ref.trait_def_id()
4472+
&& trait_id == trait_def_id
4473+
{
4474+
Some(impl_block)
4475+
} else {
4476+
None
44974477
}
44984478
}
44994479

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// tidy-alphabetical-start
66
#![allow(internal_features)]
77
#![feature(associated_type_defaults)]
8+
#![feature(box_patterns)]
89
#![feature(closure_track_caller)]
910
#![feature(debug_closure_helpers)]
1011
#![feature(exhaustive_patterns)]

0 commit comments

Comments
 (0)