@@ -205,7 +205,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;
205
205
206
206
impl Path < ' _ > {
207
207
pub fn is_global ( & self ) -> bool {
208
- ! self . segments . is_empty ( ) && self . segments [ 0 ] . ident . name == kw:: PathRoot
208
+ matches ! ( self . segments, [ PathSegment { ident: Ident { name : kw:: PathRoot , .. } , .. } , .. ] )
209
209
}
210
210
}
211
211
@@ -244,12 +244,10 @@ impl<'hir> PathSegment<'hir> {
244
244
}
245
245
246
246
pub fn args ( & self ) -> & GenericArgs < ' hir > {
247
- if let Some ( ref args) = self . args {
248
- args
249
- } else {
247
+ self . args . unwrap_or ( {
250
248
const DUMMY : & GenericArgs < ' _ > = & GenericArgs :: none ( ) ;
251
249
DUMMY
252
- }
250
+ } )
253
251
}
254
252
}
255
253
@@ -1239,13 +1237,15 @@ impl fmt::Debug for OwnerNodes<'_> {
1239
1237
. field ( "node" , & self . nodes [ ItemLocalId :: ZERO ] )
1240
1238
. field (
1241
1239
"parents" ,
1242
- & self
1243
- . nodes
1244
- . iter_enumerated ( )
1245
- . map ( |( id, parented_node) | {
1246
- debug_fn ( move |f| write ! ( f, "({id:?}, {:?})" , parented_node. parent) )
1247
- } )
1248
- . collect :: < Vec < _ > > ( ) ,
1240
+ & fmt:: from_fn ( |f| {
1241
+ f. debug_list ( )
1242
+ . entries ( self . nodes . iter_enumerated ( ) . map (
1243
+ |( id, ParentedNode { parent, .. } ) | {
1244
+ fmt:: from_fn ( move |f| write ! ( f, "({id:?}, {parent:?})" ) )
1245
+ } ,
1246
+ ) )
1247
+ . finish ( )
1248
+ } ) ,
1249
1249
)
1250
1250
. field ( "bodies" , & self . bodies )
1251
1251
. field ( "opt_hash_including_bodies" , & self . opt_hash_including_bodies )
@@ -1504,13 +1504,13 @@ pub struct DotDotPos(u32);
1504
1504
impl DotDotPos {
1505
1505
/// Panics if n >= u32::MAX.
1506
1506
pub fn new ( n : Option < usize > ) -> Self {
1507
- match n {
1508
- Some ( n ) => {
1507
+ Self (
1508
+ n . map ( |n| {
1509
1509
assert ! ( n < u32 :: MAX as usize ) ;
1510
- Self ( n as u32 )
1511
- }
1512
- None => Self ( u32:: MAX ) ,
1513
- }
1510
+ n as u32
1511
+ } )
1512
+ . unwrap_or ( u32:: MAX ) ,
1513
+ )
1514
1514
}
1515
1515
1516
1516
pub fn as_opt_usize ( & self ) -> Option < usize > {
@@ -1930,7 +1930,7 @@ impl fmt::Display for ConstContext {
1930
1930
}
1931
1931
1932
1932
// NOTE: `IntoDiagArg` impl for `ConstContext` lives in `rustc_errors`
1933
- // due to a cyclical dependency between hir that crate.
1933
+ // due to a cyclical dependency between hir and that crate.
1934
1934
1935
1935
/// A literal.
1936
1936
pub type Lit = Spanned < LitKind > ;
@@ -3439,10 +3439,10 @@ impl<'hir> FnRetTy<'hir> {
3439
3439
}
3440
3440
3441
3441
pub fn is_suggestable_infer_ty ( & self ) -> Option < & ' hir Ty < ' hir > > {
3442
- if let Self :: Return ( ty) = self {
3443
- if ty. is_suggestable_infer_ty ( ) {
3444
- return Some ( * ty ) ;
3445
- }
3442
+ if let Self :: Return ( ty) = self
3443
+ && ty. is_suggestable_infer_ty ( )
3444
+ {
3445
+ return Some ( * ty ) ;
3446
3446
}
3447
3447
None
3448
3448
}
@@ -3811,11 +3811,11 @@ pub struct FnHeader {
3811
3811
3812
3812
impl FnHeader {
3813
3813
pub fn is_async ( & self ) -> bool {
3814
- matches ! ( & self . asyncness, IsAsync :: Async ( _) )
3814
+ matches ! ( self . asyncness, IsAsync :: Async ( _) )
3815
3815
}
3816
3816
3817
3817
pub fn is_const ( & self ) -> bool {
3818
- matches ! ( & self . constness, Constness :: Const )
3818
+ matches ! ( self . constness, Constness :: Const )
3819
3819
}
3820
3820
3821
3821
pub fn is_unsafe ( & self ) -> bool {
@@ -3911,16 +3911,16 @@ pub struct Impl<'hir> {
3911
3911
3912
3912
impl ItemKind < ' _ > {
3913
3913
pub fn generics ( & self ) -> Option < & Generics < ' _ > > {
3914
- Some ( match * self {
3915
- ItemKind :: Fn { ref generics, .. }
3916
- | ItemKind :: TyAlias ( _, ref generics)
3917
- | ItemKind :: Const ( _, ref generics, _)
3918
- | ItemKind :: Enum ( _, ref generics)
3919
- | ItemKind :: Struct ( _, ref generics)
3920
- | ItemKind :: Union ( _, ref generics)
3921
- | ItemKind :: Trait ( _, _, ref generics, _, _)
3922
- | ItemKind :: TraitAlias ( ref generics, _)
3923
- | ItemKind :: Impl ( Impl { ref generics, .. } ) => generics,
3914
+ Some ( match self {
3915
+ ItemKind :: Fn { generics, .. }
3916
+ | ItemKind :: TyAlias ( _, generics)
3917
+ | ItemKind :: Const ( _, generics, _)
3918
+ | ItemKind :: Enum ( _, generics)
3919
+ | ItemKind :: Struct ( _, generics)
3920
+ | ItemKind :: Union ( _, generics)
3921
+ | ItemKind :: Trait ( _, _, generics, _, _)
3922
+ | ItemKind :: TraitAlias ( generics, _)
3923
+ | ItemKind :: Impl ( Impl { generics, .. } ) => generics,
3924
3924
_ => return None ,
3925
3925
} )
3926
3926
}
@@ -4317,16 +4317,14 @@ impl<'hir> Node<'hir> {
4317
4317
4318
4318
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
4319
4319
pub fn impl_block_of_trait ( self , trait_def_id : DefId ) -> Option < & ' hir Impl < ' hir > > {
4320
- match self {
4321
- Node :: Item ( Item { kind : ItemKind :: Impl ( impl_block) , .. } )
4322
- if impl_block
4323
- . of_trait
4324
- . and_then ( |trait_ref| trait_ref. trait_def_id ( ) )
4325
- . is_some_and ( |trait_id| trait_id == trait_def_id) =>
4326
- {
4327
- Some ( impl_block)
4328
- }
4329
- _ => None ,
4320
+ if let Node :: Item ( Item { kind : ItemKind :: Impl ( impl_block) , .. } ) = self
4321
+ && let Some ( trait_ref) = impl_block. of_trait
4322
+ && let Some ( trait_id) = trait_ref. trait_def_id ( )
4323
+ && trait_id == trait_def_id
4324
+ {
4325
+ Some ( impl_block)
4326
+ } else {
4327
+ None
4330
4328
}
4331
4329
}
4332
4330
@@ -4345,23 +4343,22 @@ impl<'hir> Node<'hir> {
4345
4343
/// Get the type for constants, assoc types, type aliases and statics.
4346
4344
pub fn ty ( self ) -> Option < & ' hir Ty < ' hir > > {
4347
4345
match self {
4348
- Node :: Item ( it) => match it. kind {
4349
- ItemKind :: TyAlias ( ty, _)
4350
- | ItemKind :: Static ( ty, _, _)
4351
- | ItemKind :: Const ( ty, _, _) => Some ( ty) ,
4352
- ItemKind :: Impl ( impl_item) => Some ( & impl_item. self_ty ) ,
4353
- _ => None ,
4354
- } ,
4355
- Node :: TraitItem ( it) => match it. kind {
4356
- TraitItemKind :: Const ( ty, _) => Some ( ty) ,
4357
- TraitItemKind :: Type ( _, ty) => ty,
4358
- _ => None ,
4359
- } ,
4360
- Node :: ImplItem ( it) => match it. kind {
4361
- ImplItemKind :: Const ( ty, _) => Some ( ty) ,
4362
- ImplItemKind :: Type ( ty) => Some ( ty) ,
4363
- _ => None ,
4364
- } ,
4346
+ Node :: Item ( Item {
4347
+ kind :
4348
+ ItemKind :: TyAlias ( ty, _)
4349
+ | ItemKind :: Static ( ty, _, _)
4350
+ | ItemKind :: Const ( ty, _, _)
4351
+ | ItemKind :: Impl ( Impl { self_ty : ty, .. } ) ,
4352
+ ..
4353
+ } )
4354
+ | Node :: TraitItem ( TraitItem {
4355
+ kind : TraitItemKind :: Const ( ty, _) | TraitItemKind :: Type ( _, Some ( ty) ) ,
4356
+ ..
4357
+ } )
4358
+ | Node :: ImplItem ( ImplItem {
4359
+ kind : ImplItemKind :: Const ( ty, _) | ImplItemKind :: Type ( ty) ,
4360
+ ..
4361
+ } ) => Some ( ty) ,
4365
4362
_ => None ,
4366
4363
}
4367
4364
}
@@ -4377,29 +4374,25 @@ impl<'hir> Node<'hir> {
4377
4374
pub fn associated_body ( & self ) -> Option < ( LocalDefId , BodyId ) > {
4378
4375
match self {
4379
4376
Node :: Item ( Item {
4380
- owner_id,
4377
+ owner_id : OwnerId { def_id } ,
4381
4378
kind :
4382
4379
ItemKind :: Const ( _, _, body) | ItemKind :: Static ( .., body) | ItemKind :: Fn { body, .. } ,
4383
4380
..
4384
4381
} )
4385
4382
| Node :: TraitItem ( TraitItem {
4386
- owner_id,
4383
+ owner_id : OwnerId { def_id } ,
4387
4384
kind :
4388
4385
TraitItemKind :: Const ( _, Some ( body) ) | TraitItemKind :: Fn ( _, TraitFn :: Provided ( body) ) ,
4389
4386
..
4390
4387
} )
4391
4388
| Node :: ImplItem ( ImplItem {
4392
- owner_id,
4389
+ owner_id : OwnerId { def_id } ,
4393
4390
kind : ImplItemKind :: Const ( _, body) | ImplItemKind :: Fn ( _, body) ,
4394
4391
..
4395
- } ) => Some ( ( owner_id. def_id , * body) ) ,
4396
-
4397
- Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { def_id, body, .. } ) , .. } ) => {
4398
- Some ( ( * def_id, * body) )
4399
- }
4400
-
4401
- Node :: AnonConst ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
4402
- Node :: ConstBlock ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
4392
+ } )
4393
+ | Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { def_id, body, .. } ) , .. } )
4394
+ | Node :: AnonConst ( AnonConst { def_id, body, .. } )
4395
+ | Node :: ConstBlock ( ConstBlock { def_id, body, .. } ) => Some ( ( * def_id, * body) ) ,
4403
4396
4404
4397
_ => None ,
4405
4398
}
@@ -4422,39 +4415,29 @@ impl<'hir> Node<'hir> {
4422
4415
}
4423
4416
4424
4417
pub fn as_owner ( self ) -> Option < OwnerNode < ' hir > > {
4425
- match self {
4426
- Node :: Item ( i) => Some ( OwnerNode :: Item ( i) ) ,
4427
- Node :: ForeignItem ( i) => Some ( OwnerNode :: ForeignItem ( i) ) ,
4428
- Node :: TraitItem ( i) => Some ( OwnerNode :: TraitItem ( i) ) ,
4429
- Node :: ImplItem ( i) => Some ( OwnerNode :: ImplItem ( i) ) ,
4430
- Node :: Crate ( i) => Some ( OwnerNode :: Crate ( i) ) ,
4431
- Node :: Synthetic => Some ( OwnerNode :: Synthetic ) ,
4432
- _ => None ,
4433
- }
4418
+ Some ( match self {
4419
+ Node :: Item ( i) => OwnerNode :: Item ( i) ,
4420
+ Node :: ForeignItem ( i) => OwnerNode :: ForeignItem ( i) ,
4421
+ Node :: TraitItem ( i) => OwnerNode :: TraitItem ( i) ,
4422
+ Node :: ImplItem ( i) => OwnerNode :: ImplItem ( i) ,
4423
+ Node :: Crate ( i) => OwnerNode :: Crate ( i) ,
4424
+ Node :: Synthetic => OwnerNode :: Synthetic ,
4425
+ _ => return None ,
4426
+ } )
4434
4427
}
4435
4428
4436
4429
pub fn fn_kind ( self ) -> Option < FnKind < ' hir > > {
4437
- match self {
4438
- Node :: Item ( i) => match i. kind {
4439
- ItemKind :: Fn { sig, generics, .. } => {
4440
- Some ( FnKind :: ItemFn ( i. ident , generics, sig. header ) )
4441
- }
4442
- _ => None ,
4443
- } ,
4444
- Node :: TraitItem ( ti) => match ti. kind {
4445
- TraitItemKind :: Fn ( ref sig, _) => Some ( FnKind :: Method ( ti. ident , sig) ) ,
4446
- _ => None ,
4447
- } ,
4448
- Node :: ImplItem ( ii) => match ii. kind {
4449
- ImplItemKind :: Fn ( ref sig, _) => Some ( FnKind :: Method ( ii. ident , sig) ) ,
4450
- _ => None ,
4451
- } ,
4452
- Node :: Expr ( e) => match e. kind {
4453
- ExprKind :: Closure { .. } => Some ( FnKind :: Closure ) ,
4454
- _ => None ,
4455
- } ,
4456
- _ => None ,
4457
- }
4430
+ Some ( match self {
4431
+ Node :: Item ( i @ Item { kind : ItemKind :: Fn { sig, generics, .. } , .. } ) => {
4432
+ FnKind :: ItemFn ( i. ident , generics, sig. header )
4433
+ }
4434
+ Node :: TraitItem ( TraitItem { kind : TraitItemKind :: Fn ( sig, _) , ident, .. } )
4435
+ | Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( sig, _) , ident, .. } ) => {
4436
+ FnKind :: Method ( * ident, sig)
4437
+ }
4438
+ Node :: Expr ( Expr { kind : ExprKind :: Closure { .. } , .. } ) => FnKind :: Closure ,
4439
+ _ => return None ,
4440
+ } )
4458
4441
}
4459
4442
4460
4443
expect_methods_self ! {
@@ -4527,13 +4510,3 @@ mod size_asserts {
4527
4510
static_assert_size ! ( TyKind <' _>, 32 ) ;
4528
4511
// tidy-alphabetical-end
4529
4512
}
4530
-
4531
- fn debug_fn ( f : impl Fn ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result ) -> impl fmt:: Debug {
4532
- struct DebugFn < F > ( F ) ;
4533
- impl < F : Fn ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result > fmt:: Debug for DebugFn < F > {
4534
- fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
4535
- ( self . 0 ) ( fmt)
4536
- }
4537
- }
4538
- DebugFn ( f)
4539
- }
0 commit comments