@@ -1328,21 +1328,16 @@ impl Disambiguator {
1328
1328
}
1329
1329
}
1330
1330
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 {
1337
1332
let kind = match self {
1338
- Disambiguator :: Primitive => return ( PREFIX , format ! ( "prim@{}" , path_str ) ) ,
1333
+ Disambiguator :: Primitive => return Suggestion :: Prefix ( "prim" ) ,
1339
1334
Disambiguator :: Kind ( kind) => kind,
1340
1335
Disambiguator :: Namespace ( _) => panic ! ( "display_for cannot be used on namespaces" ) ,
1341
1336
} ;
1342
1337
if kind == DefKind :: Macro ( MacroKind :: Bang ) {
1343
- return ( MACRO , format ! ( "{}!" , path_str ) ) ;
1338
+ return Suggestion :: Macro ;
1344
1339
} else if kind == DefKind :: Fn || kind == DefKind :: AssocFn {
1345
- return ( FUNCTION , format ! ( "{}()" , path_str ) ) ;
1340
+ return Suggestion :: Function ;
1346
1341
}
1347
1342
1348
1343
let prefix = match kind {
@@ -1367,8 +1362,7 @@ impl Disambiguator {
1367
1362
} ,
1368
1363
} ;
1369
1364
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)
1372
1366
}
1373
1367
1374
1368
fn ns ( self ) -> Namespace {
@@ -1400,6 +1394,31 @@ impl Disambiguator {
1400
1394
}
1401
1395
}
1402
1396
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
+
1403
1422
/// Reports a diagnostic for an intra-doc link.
1404
1423
///
1405
1424
/// 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(
1695
1714
sp : Option < rustc_span:: Span > ,
1696
1715
link_range : & Option < Range < usize > > ,
1697
1716
) {
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 ( ) ) ;
1700
1719
1701
1720
if let Some ( sp) = sp {
1702
1721
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
+ } ;
1706
1727
1707
- diag. span_suggestion ( sp, & help, suggestion , Applicability :: MaybeIncorrect ) ;
1728
+ diag. span_suggestion ( sp, & help, msg , Applicability :: MaybeIncorrect ) ;
1708
1729
} else {
1709
- diag. help ( & format ! ( "{}: {}" , help, suggestion) ) ;
1730
+ diag. help ( & format ! ( "{}: {}" , help, suggestion. as_help ( path_str ) ) ) ;
1710
1731
}
1711
1732
}
1712
1733
0 commit comments