@@ -312,19 +312,27 @@ fn calculate_best_path(
312312 if max_len <= 1 {
313313 return None ;
314314 }
315+
315316 let mut best_path = None ;
316- let update_best_path =
317- |best_path : & mut Option < _ > , new_path : ( ModPath , Stability ) | match best_path {
317+ let mut best_path_len = max_len;
318+ let mut process = |mut path : ( ModPath , Stability ) , name, best_path_len : & mut _ | {
319+ path. 0 . push_segment ( name) ;
320+ let new_path = match best_path. take ( ) {
321+ Some ( best_path) => select_best_path ( best_path, path, ctx. cfg ) ,
322+ None => path,
323+ } ;
324+ if new_path. 1 == Stable {
325+ * best_path_len = new_path. 0 . len ( ) ;
326+ }
327+ match & mut best_path {
318328 Some ( ( old_path, old_stability) ) => {
319329 * old_path = new_path. 0 ;
320330 * old_stability = zip_stability ( * old_stability, new_path. 1 ) ;
321331 }
322- None => * best_path = Some ( new_path) ,
323- } ;
324-
332+ None => best_path = Some ( new_path) ,
333+ }
334+ } ;
325335 let db = ctx. db ;
326-
327- let mut best_path_len = max_len;
328336 if item. krate ( db) == Some ( ctx. from . krate ) {
329337 // Item was defined in the same crate that wants to import it. It cannot be found in any
330338 // dependency in this case.
@@ -335,19 +343,10 @@ fn calculate_best_path(
335343 }
336344 // we are looking for paths of length up to best_path_len, any longer will make it be
337345 // less optimal. The -1 is due to us pushing name onto it afterwards.
338- if let Some ( mut path) =
346+ if let Some ( path) =
339347 find_path_for_module ( ctx, def_map, visited_modules, module_id, best_path_len - 1 )
340348 {
341- path. 0 . push_segment ( name) ;
342-
343- let new_path = match best_path. take ( ) {
344- Some ( best_path) => select_best_path ( best_path, path, ctx. cfg ) ,
345- None => path,
346- } ;
347- if new_path. 1 == Stable {
348- best_path_len = new_path. 0 . len ( ) ;
349- }
350- update_best_path ( & mut best_path, new_path) ;
349+ process ( path, name, & mut best_path_len) ;
351350 }
352351 }
353352 } else {
@@ -373,25 +372,16 @@ fn calculate_best_path(
373372 info. container ,
374373 best_path_len - 1 ,
375374 ) ;
376- let Some ( ( mut path, path_stability) ) = path else {
375+ let Some ( ( path, path_stability) ) = path else {
377376 continue ;
378377 } ;
379378 cov_mark:: hit!( partially_imported) ;
380- path. push_segment ( info. name . clone ( ) ) ;
381-
382- let path_with_stab = (
379+ let path = (
383380 path,
384381 zip_stability ( path_stability, if info. is_unstable { Unstable } else { Stable } ) ,
385382 ) ;
386383
387- let new_path = match best_path. take ( ) {
388- Some ( best_path) => select_best_path ( best_path, path_with_stab, ctx. cfg ) ,
389- None => path_with_stab,
390- } ;
391- if new_path. 1 == Stable {
392- best_path_len = new_path. 0 . len ( ) ;
393- }
394- update_best_path ( & mut best_path, new_path) ;
384+ process ( path, info. name . clone ( ) , & mut best_path_len) ;
395385 }
396386 }
397387 }
0 commit comments