@@ -309,7 +309,15 @@ pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {
309309
310310/// Gets the definition associated to a path.
311311#[ 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+ }
313321 fn item_child_by_name < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId , name : & str ) -> Option < & ' tcx Export < HirId > > {
314322 tcx. item_children ( def_id)
315323 . iter ( )
@@ -318,12 +326,12 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
318326
319327 let ( krate, first, path) = match * path {
320328 [ krate, first, ref path @ ..] => ( krate, first, path) ,
321- _ => return None ,
329+ _ => return Res :: Err ,
322330 } ;
323331 let tcx = cx. tcx ;
324332 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) ) ;
327335 let last = path
328336 . iter ( )
329337 . copied ( )
@@ -343,21 +351,15 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
343351 } else {
344352 None
345353 }
346- } ) ? ;
347- Some ( last. res )
354+ } ) ;
355+ try_res ! ( last) . res
348356}
349357
350358/// Convenience function to get the `DefId` of a trait by path.
351359/// It could be a trait or trait alias.
352360pub 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) {
359362 Res :: Def ( DefKind :: Trait | DefKind :: TraitAlias , trait_id) => Some ( trait_id) ,
360- Res :: Err => unreachable ! ( "this trait resolution is impossible: {:?}" , & path) ,
361363 _ => None ,
362364 }
363365}
0 commit comments