@@ -309,7 +309,15 @@ pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {
309
309
310
310
/// Gets the definition associated to a path.
311
311
#[ allow( clippy:: shadow_unrelated) ] // false positive #6563
312
- pub fn path_to_res ( cx : & LateContext < ' _ > , path : & [ & str ] ) -> Option < Res > {
312
+ pub fn path_to_res ( cx : & LateContext < ' _ > , path : & [ & str ] ) -> Res {
313
+ macro_rules! try_res {
314
+ ( $e: expr) => {
315
+ match $e {
316
+ Some ( e) => e,
317
+ None => return Res :: Err ,
318
+ }
319
+ } ;
320
+ }
313
321
fn item_child_by_name < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId , name : & str ) -> Option < & ' tcx Export < HirId > > {
314
322
tcx. item_children ( def_id)
315
323
. iter ( )
@@ -318,12 +326,12 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
318
326
319
327
let ( krate, first, path) = match * path {
320
328
[ krate, first, ref path @ ..] => ( krate, first, path) ,
321
- _ => return None ,
329
+ _ => return Res :: Err ,
322
330
} ;
323
331
let tcx = cx. tcx ;
324
332
let crates = tcx. crates ( ) ;
325
- let krate = crates. iter ( ) . find ( |& & num| tcx. crate_name ( num) . as_str ( ) == krate) ? ;
326
- let first = item_child_by_name ( tcx, krate. as_def_id ( ) , first) ? ;
333
+ let krate = try_res ! ( crates. iter( ) . find( |&&num| tcx. crate_name( num) . as_str( ) == krate) ) ;
334
+ let first = try_res ! ( item_child_by_name( tcx, krate. as_def_id( ) , first) ) ;
327
335
let last = path
328
336
. iter ( )
329
337
. copied ( )
@@ -343,21 +351,15 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
343
351
} else {
344
352
None
345
353
}
346
- } ) ? ;
347
- Some ( last. res )
354
+ } ) ;
355
+ try_res ! ( last) . res
348
356
}
349
357
350
358
/// Convenience function to get the `DefId` of a trait by path.
351
359
/// It could be a trait or trait alias.
352
360
pub fn get_trait_def_id ( cx : & LateContext < ' _ > , path : & [ & str ] ) -> Option < DefId > {
353
- let res = match path_to_res ( cx, path) {
354
- Some ( res) => res,
355
- None => return None ,
356
- } ;
357
-
358
- match res {
361
+ match path_to_res ( cx, path) {
359
362
Res :: Def ( DefKind :: Trait | DefKind :: TraitAlias , trait_id) => Some ( trait_id) ,
360
- Res :: Err => unreachable ! ( "this trait resolution is impossible: {:?}" , & path) ,
361
363
_ => None ,
362
364
}
363
365
}
0 commit comments