@@ -204,7 +204,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;
204
204
205
205
impl Path < ' _ > {
206
206
pub fn is_global ( & self ) -> bool {
207
- ! self . segments . is_empty ( ) && self . segments [ 0 ] . ident . name == kw:: PathRoot
207
+ matches ! ( self . segments, [ PathSegment { ident: Ident { name : kw:: PathRoot , .. } , .. } , .. ] )
208
208
}
209
209
}
210
210
@@ -242,12 +242,10 @@ impl<'hir> PathSegment<'hir> {
242
242
}
243
243
244
244
pub fn args ( & self ) -> & GenericArgs < ' hir > {
245
- if let Some ( ref args) = self . args {
246
- args
247
- } else {
245
+ self . args . unwrap_or ( {
248
246
const DUMMY : & GenericArgs < ' _ > = & GenericArgs :: none ( ) ;
249
247
DUMMY
250
- }
248
+ } )
251
249
}
252
250
}
253
251
@@ -1234,13 +1232,15 @@ impl fmt::Debug for OwnerNodes<'_> {
1234
1232
. field ( "node" , & self . nodes [ ItemLocalId :: ZERO ] )
1235
1233
. field (
1236
1234
"parents" ,
1237
- & self
1238
- . nodes
1239
- . iter_enumerated ( )
1240
- . map ( |( id, parented_node) | {
1241
- debug_fn ( move |f| write ! ( f, "({id:?}, {:?})" , parented_node. parent) )
1242
- } )
1243
- . collect :: < Vec < _ > > ( ) ,
1235
+ & fmt:: from_fn ( |f| {
1236
+ f. debug_list ( )
1237
+ . entries ( self . nodes . iter_enumerated ( ) . map (
1238
+ |( id, ParentedNode { parent, .. } ) | {
1239
+ fmt:: from_fn ( move |f| write ! ( f, "({id:?}, {parent:?})" ) )
1240
+ } ,
1241
+ ) )
1242
+ . finish ( )
1243
+ } ) ,
1244
1244
)
1245
1245
. field ( "bodies" , & self . bodies )
1246
1246
. field ( "opt_hash_including_bodies" , & self . opt_hash_including_bodies )
@@ -1499,13 +1499,13 @@ pub struct DotDotPos(u32);
1499
1499
impl DotDotPos {
1500
1500
/// Panics if n >= u32::MAX.
1501
1501
pub fn new ( n : Option < usize > ) -> Self {
1502
- match n {
1503
- Some ( n ) => {
1502
+ Self (
1503
+ n . map ( |n| {
1504
1504
assert ! ( n < u32 :: MAX as usize ) ;
1505
- Self ( n as u32 )
1506
- }
1507
- None => Self ( u32:: MAX ) ,
1508
- }
1505
+ n as u32
1506
+ } )
1507
+ . unwrap_or ( u32:: MAX ) ,
1508
+ )
1509
1509
}
1510
1510
1511
1511
pub fn as_opt_usize ( & self ) -> Option < usize > {
@@ -1922,7 +1922,7 @@ impl fmt::Display for ConstContext {
1922
1922
}
1923
1923
1924
1924
// NOTE: `IntoDiagArg` impl for `ConstContext` lives in `rustc_errors`
1925
- // due to a cyclical dependency between hir that crate.
1925
+ // due to a cyclical dependency between hir and that crate.
1926
1926
1927
1927
/// A literal.
1928
1928
pub type Lit = Spanned < LitKind > ;
@@ -3423,10 +3423,10 @@ impl<'hir> FnRetTy<'hir> {
3423
3423
}
3424
3424
3425
3425
pub fn is_suggestable_infer_ty ( & self ) -> Option < & ' hir Ty < ' hir > > {
3426
- if let Self :: Return ( ty) = self {
3427
- if ty. is_suggestable_infer_ty ( ) {
3428
- return Some ( * ty ) ;
3429
- }
3426
+ if let Self :: Return ( ty) = self
3427
+ && ty. is_suggestable_infer_ty ( )
3428
+ {
3429
+ return Some ( * ty ) ;
3430
3430
}
3431
3431
None
3432
3432
}
@@ -3793,11 +3793,11 @@ pub struct FnHeader {
3793
3793
3794
3794
impl FnHeader {
3795
3795
pub fn is_async ( & self ) -> bool {
3796
- matches ! ( & self . asyncness, IsAsync :: Async ( _) )
3796
+ matches ! ( self . asyncness, IsAsync :: Async ( _) )
3797
3797
}
3798
3798
3799
3799
pub fn is_const ( & self ) -> bool {
3800
- matches ! ( & self . constness, Constness :: Const )
3800
+ matches ! ( self . constness, Constness :: Const )
3801
3801
}
3802
3802
3803
3803
pub fn is_unsafe ( & self ) -> bool {
@@ -3893,16 +3893,16 @@ pub struct Impl<'hir> {
3893
3893
3894
3894
impl ItemKind < ' _ > {
3895
3895
pub fn generics ( & self ) -> Option < & Generics < ' _ > > {
3896
- Some ( match * self {
3897
- ItemKind :: Fn { ref generics, .. }
3898
- | ItemKind :: TyAlias ( _, ref generics)
3899
- | ItemKind :: Const ( _, ref generics, _)
3900
- | ItemKind :: Enum ( _, ref generics)
3901
- | ItemKind :: Struct ( _, ref generics)
3902
- | ItemKind :: Union ( _, ref generics)
3903
- | ItemKind :: Trait ( _, _, ref generics, _, _)
3904
- | ItemKind :: TraitAlias ( ref generics, _)
3905
- | ItemKind :: Impl ( Impl { ref generics, .. } ) => generics,
3896
+ Some ( match self {
3897
+ ItemKind :: Fn { generics, .. }
3898
+ | ItemKind :: TyAlias ( _, generics)
3899
+ | ItemKind :: Const ( _, generics, _)
3900
+ | ItemKind :: Enum ( _, generics)
3901
+ | ItemKind :: Struct ( _, generics)
3902
+ | ItemKind :: Union ( _, generics)
3903
+ | ItemKind :: Trait ( _, _, generics, _, _)
3904
+ | ItemKind :: TraitAlias ( generics, _)
3905
+ | ItemKind :: Impl ( Impl { generics, .. } ) => generics,
3906
3906
_ => return None ,
3907
3907
} )
3908
3908
}
@@ -4299,16 +4299,14 @@ impl<'hir> Node<'hir> {
4299
4299
4300
4300
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
4301
4301
pub fn impl_block_of_trait ( self , trait_def_id : DefId ) -> Option < & ' hir Impl < ' hir > > {
4302
- match self {
4303
- Node :: Item ( Item { kind : ItemKind :: Impl ( impl_block) , .. } )
4304
- if impl_block
4305
- . of_trait
4306
- . and_then ( |trait_ref| trait_ref. trait_def_id ( ) )
4307
- . is_some_and ( |trait_id| trait_id == trait_def_id) =>
4308
- {
4309
- Some ( impl_block)
4310
- }
4311
- _ => None ,
4302
+ if let Node :: Item ( Item { kind : ItemKind :: Impl ( impl_block) , .. } ) = self
4303
+ && let Some ( trait_ref) = impl_block. of_trait
4304
+ && let Some ( trait_id) = trait_ref. trait_def_id ( )
4305
+ && trait_id == trait_def_id
4306
+ {
4307
+ Some ( impl_block)
4308
+ } else {
4309
+ None
4312
4310
}
4313
4311
}
4314
4312
@@ -4327,23 +4325,22 @@ impl<'hir> Node<'hir> {
4327
4325
/// Get the type for constants, assoc types, type aliases and statics.
4328
4326
pub fn ty ( self ) -> Option < & ' hir Ty < ' hir > > {
4329
4327
match self {
4330
- Node :: Item ( it) => match it. kind {
4331
- ItemKind :: TyAlias ( ty, _)
4332
- | ItemKind :: Static ( ty, _, _)
4333
- | ItemKind :: Const ( ty, _, _) => Some ( ty) ,
4334
- ItemKind :: Impl ( impl_item) => Some ( & impl_item. self_ty ) ,
4335
- _ => None ,
4336
- } ,
4337
- Node :: TraitItem ( it) => match it. kind {
4338
- TraitItemKind :: Const ( ty, _) => Some ( ty) ,
4339
- TraitItemKind :: Type ( _, ty) => ty,
4340
- _ => None ,
4341
- } ,
4342
- Node :: ImplItem ( it) => match it. kind {
4343
- ImplItemKind :: Const ( ty, _) => Some ( ty) ,
4344
- ImplItemKind :: Type ( ty) => Some ( ty) ,
4345
- _ => None ,
4346
- } ,
4328
+ Node :: Item ( Item {
4329
+ kind :
4330
+ ItemKind :: TyAlias ( ty, _)
4331
+ | ItemKind :: Static ( ty, _, _)
4332
+ | ItemKind :: Const ( ty, _, _)
4333
+ | ItemKind :: Impl ( Impl { self_ty : ty, .. } ) ,
4334
+ ..
4335
+ } )
4336
+ | Node :: TraitItem ( TraitItem {
4337
+ kind : TraitItemKind :: Const ( ty, _) | TraitItemKind :: Type ( _, Some ( ty) ) ,
4338
+ ..
4339
+ } )
4340
+ | Node :: ImplItem ( ImplItem {
4341
+ kind : ImplItemKind :: Const ( ty, _) | ImplItemKind :: Type ( ty) ,
4342
+ ..
4343
+ } ) => Some ( ty) ,
4347
4344
_ => None ,
4348
4345
}
4349
4346
}
@@ -4359,29 +4356,25 @@ impl<'hir> Node<'hir> {
4359
4356
pub fn associated_body ( & self ) -> Option < ( LocalDefId , BodyId ) > {
4360
4357
match self {
4361
4358
Node :: Item ( Item {
4362
- owner_id,
4359
+ owner_id : OwnerId { def_id } ,
4363
4360
kind :
4364
4361
ItemKind :: Const ( _, _, body) | ItemKind :: Static ( .., body) | ItemKind :: Fn { body, .. } ,
4365
4362
..
4366
4363
} )
4367
4364
| Node :: TraitItem ( TraitItem {
4368
- owner_id,
4365
+ owner_id : OwnerId { def_id } ,
4369
4366
kind :
4370
4367
TraitItemKind :: Const ( _, Some ( body) ) | TraitItemKind :: Fn ( _, TraitFn :: Provided ( body) ) ,
4371
4368
..
4372
4369
} )
4373
4370
| Node :: ImplItem ( ImplItem {
4374
- owner_id,
4371
+ owner_id : OwnerId { def_id } ,
4375
4372
kind : ImplItemKind :: Const ( _, body) | ImplItemKind :: Fn ( _, body) ,
4376
4373
..
4377
- } ) => Some ( ( owner_id. def_id , * body) ) ,
4378
-
4379
- Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { def_id, body, .. } ) , .. } ) => {
4380
- Some ( ( * def_id, * body) )
4381
- }
4382
-
4383
- Node :: AnonConst ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
4384
- Node :: ConstBlock ( constant) => Some ( ( constant. def_id , constant. body ) ) ,
4374
+ } )
4375
+ | Node :: Expr ( Expr { kind : ExprKind :: Closure ( Closure { def_id, body, .. } ) , .. } )
4376
+ | Node :: AnonConst ( AnonConst { def_id, body, .. } )
4377
+ | Node :: ConstBlock ( ConstBlock { def_id, body, .. } ) => Some ( ( * def_id, * body) ) ,
4385
4378
4386
4379
_ => None ,
4387
4380
}
@@ -4404,39 +4397,29 @@ impl<'hir> Node<'hir> {
4404
4397
}
4405
4398
4406
4399
pub fn as_owner ( self ) -> Option < OwnerNode < ' hir > > {
4407
- match self {
4408
- Node :: Item ( i) => Some ( OwnerNode :: Item ( i) ) ,
4409
- Node :: ForeignItem ( i) => Some ( OwnerNode :: ForeignItem ( i) ) ,
4410
- Node :: TraitItem ( i) => Some ( OwnerNode :: TraitItem ( i) ) ,
4411
- Node :: ImplItem ( i) => Some ( OwnerNode :: ImplItem ( i) ) ,
4412
- Node :: Crate ( i) => Some ( OwnerNode :: Crate ( i) ) ,
4413
- Node :: Synthetic => Some ( OwnerNode :: Synthetic ) ,
4414
- _ => None ,
4415
- }
4400
+ Some ( match self {
4401
+ Node :: Item ( i) => OwnerNode :: Item ( i) ,
4402
+ Node :: ForeignItem ( i) => OwnerNode :: ForeignItem ( i) ,
4403
+ Node :: TraitItem ( i) => OwnerNode :: TraitItem ( i) ,
4404
+ Node :: ImplItem ( i) => OwnerNode :: ImplItem ( i) ,
4405
+ Node :: Crate ( i) => OwnerNode :: Crate ( i) ,
4406
+ Node :: Synthetic => OwnerNode :: Synthetic ,
4407
+ _ => return None ,
4408
+ } )
4416
4409
}
4417
4410
4418
4411
pub fn fn_kind ( self ) -> Option < FnKind < ' hir > > {
4419
- match self {
4420
- Node :: Item ( i) => match i. kind {
4421
- ItemKind :: Fn { sig, generics, .. } => {
4422
- Some ( FnKind :: ItemFn ( i. ident , generics, sig. header ) )
4423
- }
4424
- _ => None ,
4425
- } ,
4426
- Node :: TraitItem ( ti) => match ti. kind {
4427
- TraitItemKind :: Fn ( ref sig, _) => Some ( FnKind :: Method ( ti. ident , sig) ) ,
4428
- _ => None ,
4429
- } ,
4430
- Node :: ImplItem ( ii) => match ii. kind {
4431
- ImplItemKind :: Fn ( ref sig, _) => Some ( FnKind :: Method ( ii. ident , sig) ) ,
4432
- _ => None ,
4433
- } ,
4434
- Node :: Expr ( e) => match e. kind {
4435
- ExprKind :: Closure { .. } => Some ( FnKind :: Closure ) ,
4436
- _ => None ,
4437
- } ,
4438
- _ => None ,
4439
- }
4412
+ Some ( match self {
4413
+ Node :: Item ( i @ Item { kind : ItemKind :: Fn { sig, generics, .. } , .. } ) => {
4414
+ FnKind :: ItemFn ( i. ident , generics, sig. header )
4415
+ }
4416
+ Node :: TraitItem ( TraitItem { kind : TraitItemKind :: Fn ( sig, _) , ident, .. } )
4417
+ | Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( sig, _) , ident, .. } ) => {
4418
+ FnKind :: Method ( * ident, sig)
4419
+ }
4420
+ Node :: Expr ( Expr { kind : ExprKind :: Closure { .. } , .. } ) => FnKind :: Closure ,
4421
+ _ => return None ,
4422
+ } )
4440
4423
}
4441
4424
4442
4425
expect_methods_self ! {
@@ -4509,13 +4492,3 @@ mod size_asserts {
4509
4492
static_assert_size ! ( TyKind <' _>, 32 ) ;
4510
4493
// tidy-alphabetical-end
4511
4494
}
4512
-
4513
- fn debug_fn ( f : impl Fn ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result ) -> impl fmt:: Debug {
4514
- struct DebugFn < F > ( F ) ;
4515
- impl < F : Fn ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result > fmt:: Debug for DebugFn < F > {
4516
- fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
4517
- ( self . 0 ) ( fmt)
4518
- }
4519
- }
4520
- DebugFn ( f)
4521
- }
0 commit comments