@@ -541,38 +541,31 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
541
541
542
542
let link = ori_link. replace ( "`" , "" ) ;
543
543
let parts = link. split ( '#' ) . collect :: < Vec < _ > > ( ) ;
544
- let ( link, extra_fragment) = if parts. len ( ) > 2 {
545
- build_diagnostic (
546
- cx,
547
- & item,
548
- & link,
549
- & dox,
550
- link_range,
551
- "has an issue with the link anchor." ,
552
- "only one `#` is allowed in a link" ,
553
- None ,
554
- ) ;
555
- continue ;
556
- } else if parts. len ( ) == 2 {
557
- if parts[ 0 ] . trim ( ) . is_empty ( ) {
558
- // This is an anchor to an element of the current page, nothing to do in here!
544
+ let ( link, extra_fragment) = match * parts {
545
+ [ ] => unreachable ! ( "`str::split` always returns a non-empty list" ) ,
546
+ [ a] => ( a. to_owned ( ) , None ) ,
547
+ // This is an anchor to an element of the current page, nothing to do in here!
548
+ [ a, _] if a. trim ( ) . is_empty ( ) => continue ,
549
+ [ a, b] => ( a. to_owned ( ) , Some ( b. to_owned ( ) ) ) ,
550
+ [ _, _, _, ..] => {
551
+ build_diagnostic (
552
+ cx,
553
+ & item,
554
+ & link,
555
+ & dox,
556
+ link_range,
557
+ "has an issue with the link anchor." ,
558
+ "only one `#` is allowed in a link" ,
559
+ None ,
560
+ ) ;
559
561
continue ;
560
562
}
561
- ( parts[ 0 ] . to_owned ( ) , Some ( parts[ 1 ] . to_owned ( ) ) )
562
- } else {
563
- ( parts[ 0 ] . to_owned ( ) , None )
564
563
} ;
565
564
let resolved_self;
566
565
let mut path_str;
567
566
let ( res, fragment) = {
568
- let mut kind = None ;
569
- path_str = if let Some ( prefix) = [ "struct@" , "enum@" , "type@" , "trait@" , "union@" ]
570
- . iter ( )
571
- . find ( |p| link. starts_with ( * * p) )
572
- {
573
- kind = Some ( TypeNS ) ;
574
- link. trim_start_matches ( prefix)
575
- } else if let Some ( prefix) = [
567
+ static TYPES : & [ & str ] = & [ "struct@" , "enum@" , "type@" , "trait@" , "union@" ] ;
568
+ static TY_KINDS : & [ & str ] = & [
576
569
"const@" ,
577
570
"static@" ,
578
571
"value@" ,
@@ -581,28 +574,27 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
581
574
"fn@" ,
582
575
"module@" ,
583
576
"method@" ,
584
- ]
585
- . iter ( )
586
- . find ( |p| link. starts_with ( * * p) )
587
- {
588
- kind = Some ( ValueNS ) ;
589
- link. trim_start_matches ( prefix)
590
- } else if link. ends_with ( "()" ) {
591
- kind = Some ( ValueNS ) ;
592
- link. trim_end_matches ( "()" )
593
- } else if link. starts_with ( "macro@" ) {
594
- kind = Some ( MacroNS ) ;
595
- link. trim_start_matches ( "macro@" )
596
- } else if link. starts_with ( "derive@" ) {
597
- kind = Some ( MacroNS ) ;
598
- link. trim_start_matches ( "derive@" )
599
- } else if link. ends_with ( '!' ) {
600
- kind = Some ( MacroNS ) ;
601
- link. trim_end_matches ( '!' )
602
- } else {
603
- & link[ ..]
604
- }
605
- . trim ( ) ;
577
+ ] ;
578
+ let ( kind, path) =
579
+ if let Some ( tail) = TYPES . iter ( ) . filter_map ( |& p| link. strip_prefix ( p) ) . next ( ) {
580
+ ( Some ( TypeNS ) , tail)
581
+ } else if let Some ( tail) =
582
+ TY_KINDS . iter ( ) . filter_map ( |& p| link. strip_prefix ( p) ) . next ( )
583
+ {
584
+ ( Some ( ValueNS ) , tail)
585
+ } else if let Some ( head) = link. strip_suffix ( "()" ) {
586
+ ( Some ( ValueNS ) , head)
587
+ } else if let Some ( tail) =
588
+ link. strip_prefix ( "macro@" ) . or_else ( || link. strip_prefix ( "derive@" ) )
589
+ {
590
+ ( Some ( MacroNS ) , tail)
591
+ } else if let Some ( head) = link. strip_suffix ( '!' ) {
592
+ ( Some ( MacroNS ) , head)
593
+ } else {
594
+ ( None , & link[ ..] )
595
+ } ;
596
+
597
+ path_str = path. trim ( ) ;
606
598
607
599
if path_str. contains ( |ch : char | !( ch. is_alphanumeric ( ) || ch == ':' || ch == '_' ) ) {
608
600
continue ;
@@ -623,9 +615,9 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
623
615
if item. is_mod ( ) && item. attrs . inner_docs { None } else { parent_node } ;
624
616
625
617
// replace `Self` with suitable item's parent name
626
- if path_str. starts_with ( "Self::" ) {
618
+ if let Some ( item ) = path_str. strip_prefix ( "Self::" ) {
627
619
if let Some ( ref name) = parent_name {
628
- resolved_self = format ! ( "{}::{}" , name, & path_str [ 6 .. ] ) ;
620
+ resolved_self = format ! ( "{}::{}" , name, item ) ;
629
621
path_str = & resolved_self;
630
622
}
631
623
}
0 commit comments