@@ -1328,21 +1328,16 @@ impl Disambiguator {
13281328 }
13291329 }
13301330
1331- /// Return (description of the change, suggestion)
1332- fn suggestion_for ( self , path_str : & str ) -> ( & ' static str , String ) {
1333- const PREFIX : & str = "prefix with the item kind" ;
1334- const FUNCTION : & str = "add parentheses" ;
1335- const MACRO : & str = "add an exclamation mark" ;
1336-
1331+ fn suggestion ( self ) -> Suggestion {
13371332 let kind = match self {
1338- Disambiguator :: Primitive => return ( PREFIX , format ! ( "prim@{}" , path_str ) ) ,
1333+ Disambiguator :: Primitive => return Suggestion :: Prefix ( "prim" ) ,
13391334 Disambiguator :: Kind ( kind) => kind,
13401335 Disambiguator :: Namespace ( _) => panic ! ( "display_for cannot be used on namespaces" ) ,
13411336 } ;
13421337 if kind == DefKind :: Macro ( MacroKind :: Bang ) {
1343- return ( MACRO , format ! ( "{}!" , path_str ) ) ;
1338+ return Suggestion :: Macro ;
13441339 } else if kind == DefKind :: Fn || kind == DefKind :: AssocFn {
1345- return ( FUNCTION , format ! ( "{}()" , path_str ) ) ;
1340+ return Suggestion :: Function ;
13461341 }
13471342
13481343 let prefix = match kind {
@@ -1367,8 +1362,7 @@ impl Disambiguator {
13671362 } ,
13681363 } ;
13691364
1370- // FIXME: if this is an implied shortcut link, it's bad style to suggest `@`
1371- ( PREFIX , format ! ( "{}@{}" , prefix, path_str) )
1365+ Suggestion :: Prefix ( prefix)
13721366 }
13731367
13741368 fn ns ( self ) -> Namespace {
@@ -1400,6 +1394,31 @@ impl Disambiguator {
14001394 }
14011395}
14021396
1397+ enum Suggestion {
1398+ Prefix ( & ' static str ) ,
1399+ Function ,
1400+ Macro ,
1401+ }
1402+
1403+ impl Suggestion {
1404+ fn descr ( & self ) -> Cow < ' static , str > {
1405+ match self {
1406+ Self :: Prefix ( x) => format ! ( "prefix with `{}@`" , x) . into ( ) ,
1407+ Self :: Function => "add parentheses" . into ( ) ,
1408+ Self :: Macro => "add an exclamation mark" . into ( ) ,
1409+ }
1410+ }
1411+
1412+ fn as_help ( & self , path_str : & str ) -> String {
1413+ // FIXME: if this is an implied shortcut link, it's bad style to suggest `@`
1414+ match self {
1415+ Self :: Prefix ( prefix) => format ! ( "{}@{}" , prefix, path_str) ,
1416+ Self :: Function => format ! ( "{}()" , path_str) ,
1417+ Self :: Macro => format ! ( "{}!" , path_str) ,
1418+ }
1419+ }
1420+ }
1421+
14031422/// Reports a diagnostic for an intra-doc link.
14041423///
14051424/// If no link range is provided, or the source span of the link cannot be determined, the span of
@@ -1695,18 +1714,20 @@ fn suggest_disambiguator(
16951714 sp : Option < rustc_span:: Span > ,
16961715 link_range : & Option < Range < usize > > ,
16971716) {
1698- let ( action , mut suggestion) = disambiguator. suggestion_for ( path_str ) ;
1699- let help = format ! ( "to link to the {}, {}" , disambiguator. descr( ) , action ) ;
1717+ let suggestion = disambiguator. suggestion ( ) ;
1718+ let help = format ! ( "to link to the {}, {}" , disambiguator. descr( ) , suggestion . descr ( ) ) ;
17001719
17011720 if let Some ( sp) = sp {
17021721 let link_range = link_range. as_ref ( ) . expect ( "must have a link range if we have a span" ) ;
1703- if dox. bytes ( ) . nth ( link_range. start ) == Some ( b'`' ) {
1704- suggestion = format ! ( "`{}`" , suggestion) ;
1705- }
1722+ let msg = if dox. bytes ( ) . nth ( link_range. start ) == Some ( b'`' ) {
1723+ format ! ( "`{}`" , suggestion. as_help( path_str) )
1724+ } else {
1725+ suggestion. as_help ( path_str)
1726+ } ;
17061727
1707- diag. span_suggestion ( sp, & help, suggestion , Applicability :: MaybeIncorrect ) ;
1728+ diag. span_suggestion ( sp, & help, msg , Applicability :: MaybeIncorrect ) ;
17081729 } else {
1709- diag. help ( & format ! ( "{}: {}" , help, suggestion) ) ;
1730+ diag. help ( & format ! ( "{}: {}" , help, suggestion. as_help ( path_str ) ) ) ;
17101731 }
17111732}
17121733
0 commit comments