@@ -327,7 +327,12 @@ impl<'a> FnSig<'a> {
327
327
fn to_str ( & self , context : & RewriteContext < ' _ > ) -> String {
328
328
let mut result = String :: with_capacity ( 128 ) ;
329
329
// Vis defaultness constness unsafety abi.
330
- result. push_str ( & * format_visibility ( context, self . visibility ) ) ;
330
+ let vis = format_visibility ( context, self . visibility ) ;
331
+ result. push_str ( & vis) ;
332
+ if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
333
+ // format_visibility doesn't have a trailing space in Version::Two
334
+ result. push ( ' ' ) ;
335
+ }
331
336
result. push_str ( format_defaultness ( self . defaultness ) ) ;
332
337
result. push_str ( format_constness ( self . constness ) ) ;
333
338
result. push_str ( format_async ( & self . is_async ) ) ;
@@ -917,7 +922,12 @@ fn format_impl_ref_and_type(
917
922
} = * iimpl;
918
923
let mut result = String :: with_capacity ( 128 ) ;
919
924
920
- result. push_str ( & format_visibility ( context, & item. vis ) ) ;
925
+ let vis = format_visibility ( context, & item. vis ) ;
926
+ result. push_str ( & vis) ;
927
+ if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
928
+ // format_visibility doesn't have a trailing space in Version::Two
929
+ result. push ( ' ' ) ;
930
+ }
921
931
result. push_str ( format_defaultness ( defaultness) ) ;
922
932
result. push_str ( format_unsafety ( unsafety) ) ;
923
933
@@ -1126,12 +1136,16 @@ pub(crate) fn format_trait(
1126
1136
} = * * trait_kind;
1127
1137
1128
1138
let mut result = String :: with_capacity ( 128 ) ;
1129
- let header = format ! (
1130
- "{}{}{}trait " ,
1131
- format_visibility( context, & item. vis) ,
1132
- format_unsafety( unsafety) ,
1133
- format_auto( is_auto) ,
1134
- ) ;
1139
+
1140
+ let mut vis = format_visibility ( context, & item. vis ) ;
1141
+ if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
1142
+ // format_visibility doesn't have a trailing space in Version::Two
1143
+ vis += " " ;
1144
+ }
1145
+ let unsafety = format_unsafety ( unsafety) ;
1146
+ let auto = format_auto ( is_auto) ;
1147
+
1148
+ let header = format ! ( "{vis}{unsafety}{auto}trait " ) ;
1135
1149
result. push_str ( & header) ;
1136
1150
1137
1151
let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
@@ -1334,8 +1348,13 @@ pub(crate) fn format_trait_alias(
1334
1348
// 6 = "trait ", 2 = " ="
1335
1349
let g_shape = shape. offset_left ( 6 ) ?. sub_width ( 2 ) ?;
1336
1350
let generics_str = rewrite_generics ( context, alias, generics, g_shape) ?;
1337
- let vis_str = format_visibility ( context, vis) ;
1338
- let lhs = format ! ( "{vis_str}trait {generics_str} =" ) ;
1351
+ let vis = format_visibility ( context, vis) ;
1352
+ let lhs = if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
1353
+ // format_visibility doesn't have a trailing space in Version::Two
1354
+ format ! ( "{vis} trait {generics_str} =" )
1355
+ } else {
1356
+ format ! ( "{vis}trait {generics_str} =" )
1357
+ } ;
1339
1358
// 1 = ";"
1340
1359
let trait_alias_bounds = TraitAliasBounds {
1341
1360
generic_bounds,
@@ -1722,7 +1741,13 @@ fn rewrite_ty<R: Rewrite>(
1722
1741
if !after_where_predicates. is_empty ( ) {
1723
1742
return None ;
1724
1743
}
1725
- result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
1744
+ let vis = format_visibility ( context, vis) ;
1745
+ result. push_str ( & vis) ;
1746
+ if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
1747
+ // format_visibility doesn't have a trailing space in Version::Two
1748
+ result. push ( ' ' ) ;
1749
+ }
1750
+ result. push_str ( "type " ) ;
1726
1751
let ident_str = rewrite_ident ( context, ident) ;
1727
1752
1728
1753
if generics. params . is_empty ( ) {
@@ -1820,18 +1845,22 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1820
1845
pub ( crate ) fn rewrite_struct_field_prefix (
1821
1846
context : & RewriteContext < ' _ > ,
1822
1847
field : & ast:: FieldDef ,
1823
- ) -> Option < String > {
1848
+ ) -> Option < Cow < ' static , str > > {
1824
1849
let vis = format_visibility ( context, & field. vis ) ;
1825
- let type_annotation_spacing = type_annotation_spacing ( context. config ) ;
1826
- Some ( match field. ident {
1827
- Some ( name) => format ! (
1828
- "{}{}{}:" ,
1829
- vis,
1830
- rewrite_ident( context, name) ,
1831
- type_annotation_spacing. 0
1832
- ) ,
1833
- None => vis. to_string ( ) ,
1834
- } )
1850
+
1851
+ let Some ( name) = field. ident else {
1852
+ return Some ( vis) ;
1853
+ } ;
1854
+
1855
+ let ( space_before_colon, _) = type_annotation_spacing ( context. config ) ;
1856
+ let ident = rewrite_ident ( context, name) ;
1857
+
1858
+ let prefix = if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
1859
+ format ! ( "{vis} {ident}{space_before_colon}:" )
1860
+ } else {
1861
+ format ! ( "{vis}{ident}{space_before_colon}:" )
1862
+ } ;
1863
+ Some ( Cow :: from ( prefix) )
1835
1864
}
1836
1865
1837
1866
impl Rewrite for ast:: FieldDef {
@@ -1851,7 +1880,7 @@ pub(crate) fn rewrite_struct_field(
1851
1880
}
1852
1881
1853
1882
let type_annotation_spacing = type_annotation_spacing ( context. config ) ;
1854
- let mut prefix = rewrite_struct_field_prefix ( context, field) ?;
1883
+ let prefix = rewrite_struct_field_prefix ( context, field) ?;
1855
1884
1856
1885
let attrs_str = field. attrs . rewrite ( context, shape) ?;
1857
1886
let attrs_extendable = field. ident . is_none ( ) && is_attributes_extendable ( & attrs_str) ;
@@ -1883,6 +1912,14 @@ pub(crate) fn rewrite_struct_field(
1883
1912
if prefix. is_empty ( ) && !attrs_str. is_empty ( ) && attrs_extendable && spacing. is_empty ( ) {
1884
1913
spacing. push ( ' ' ) ;
1885
1914
}
1915
+
1916
+ if !prefix. is_empty ( ) && field. ident . is_none ( ) && context. config . version ( ) == Version :: Two {
1917
+ // For tuple struct fields, which only have a visibility modifier a space is needed
1918
+ // when using Version::Two since rewrite_struct_field_prefix won't add a trailing space
1919
+ // after the visibilty modifier.
1920
+ spacing. push ( ' ' ) ;
1921
+ }
1922
+
1886
1923
let orig_ty = shape
1887
1924
. offset_left ( overhead + spacing. len ( ) )
1888
1925
. and_then ( |ty_shape| field. ty . rewrite ( context, ty_shape) ) ;
@@ -1894,11 +1931,6 @@ pub(crate) fn rewrite_struct_field(
1894
1931
1895
1932
let is_prefix_empty = prefix. is_empty ( ) ;
1896
1933
// We must use multiline. We are going to put attributes and a field on different lines.
1897
- if context. config . version ( ) == Version :: Two {
1898
- // Remove any additional whitespace at the end of the prefix.
1899
- // For example if there is a space after a visibility modifier.
1900
- prefix. truncate ( prefix. trim_end ( ) . len ( ) ) ;
1901
- }
1902
1934
let field_str = rewrite_assign_rhs ( context, prefix, & * field. ty , & RhsAssignKind :: Ty , shape) ?;
1903
1935
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1904
1936
let field_str = if is_prefix_empty {
@@ -1986,15 +2018,18 @@ fn rewrite_static(
1986
2018
offset : Indent ,
1987
2019
) -> Option < String > {
1988
2020
let colon = colon_spaces ( context. config ) ;
1989
- let mut prefix = format ! (
1990
- "{}{}{} {}{}{}" ,
1991
- format_visibility( context, static_parts. vis) ,
1992
- static_parts. defaultness. map_or( "" , format_defaultness) ,
1993
- static_parts. prefix,
1994
- format_mutability( static_parts. mutability) ,
1995
- rewrite_ident( context, static_parts. ident) ,
1996
- colon,
1997
- ) ;
2021
+
2022
+ let vis = format_visibility ( context, static_parts. vis ) ;
2023
+ let defaultness = static_parts. defaultness . map_or ( "" , format_defaultness) ;
2024
+ let static_prefix = static_parts. prefix ;
2025
+ let mutability = format_mutability ( static_parts. mutability ) ;
2026
+ let ident = rewrite_ident ( context, static_parts. ident ) ;
2027
+ let mut prefix = if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
2028
+ // format_visibility doesn't have a trailing space in Version::Two
2029
+ format ! ( "{vis} {defaultness}{static_prefix} {mutability}{ident}{colon}" )
2030
+ } else {
2031
+ format ! ( "{vis}{defaultness}{static_prefix} {mutability}{ident}{colon}" )
2032
+ } ;
1998
2033
// 2 = " =".len()
1999
2034
let ty_shape =
2000
2035
Shape :: indented ( offset. block_only ( ) , context. config ) . offset_left ( prefix. len ( ) + 2 ) ?;
@@ -3148,7 +3183,13 @@ fn format_header(
3148
3183
let mut result = String :: with_capacity ( 128 ) ;
3149
3184
let shape = Shape :: indented ( offset, context. config ) ;
3150
3185
3151
- result. push_str ( format_visibility ( context, vis) . trim ( ) ) ;
3186
+ let visibility = format_visibility ( context, vis) ;
3187
+ if context. config . version ( ) == Version :: Two {
3188
+ // format_visibility doesn't have a trailing space in Version::Two
3189
+ result. push_str ( & visibility) ;
3190
+ } else {
3191
+ result. push_str ( visibility. trim ( ) ) ;
3192
+ }
3152
3193
3153
3194
// Check for a missing comment between the visibility and the item name.
3154
3195
let after_vis = vis. span . hi ( ) ;
@@ -3334,12 +3375,13 @@ impl Rewrite for ast::ForeignItem {
3334
3375
// function kw here.
3335
3376
let vis = format_visibility ( context, & self . vis ) ;
3336
3377
let mut_str = format_mutability ( mutability) ;
3337
- let prefix = format ! (
3338
- "{}static {}{}:" ,
3339
- vis,
3340
- mut_str,
3341
- rewrite_ident( context, self . ident)
3342
- ) ;
3378
+ let ident = rewrite_ident ( context, self . ident ) ;
3379
+ let prefix = if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
3380
+ // format_visibility doesn't have a trailing space in Version::Two
3381
+ format ! ( "{vis} static {mut_str}{ident}:" )
3382
+ } else {
3383
+ format ! ( "{vis}static {mut_str}{ident}:" )
3384
+ } ;
3343
3385
// 1 = ;
3344
3386
rewrite_assign_rhs (
3345
3387
context,
@@ -3417,7 +3459,12 @@ pub(crate) fn rewrite_mod(
3417
3459
attrs_shape : Shape ,
3418
3460
) -> Option < String > {
3419
3461
let mut result = String :: with_capacity ( 32 ) ;
3420
- result. push_str ( & * format_visibility ( context, & item. vis ) ) ;
3462
+ let vis = format_visibility ( context, & item. vis ) ;
3463
+ result. push_str ( & vis) ;
3464
+ if !vis. is_empty ( ) && context. config . version ( ) == Version :: Two {
3465
+ // format_visibility doesn't have a trailing space in Version::Two
3466
+ result. push ( ' ' ) ;
3467
+ }
3421
3468
result. push_str ( "mod " ) ;
3422
3469
result. push_str ( rewrite_ident ( context, item. ident ) ) ;
3423
3470
result. push ( ';' ) ;
0 commit comments