@@ -126,7 +126,7 @@ impl<'a> FmtVisitor<'a> {
126
126
// These are not actually rust functions,
127
127
// but we format them as such.
128
128
abi:: Abi :: Rust ,
129
- item. vis ,
129
+ & item. vis ,
130
130
span,
131
131
false ,
132
132
false ) ;
@@ -142,15 +142,16 @@ impl<'a> FmtVisitor<'a> {
142
142
ast:: ForeignItemKind :: Static ( ref ty, is_mutable) => {
143
143
// FIXME(#21): we're dropping potential comments in between the
144
144
// function keywords here.
145
+ let vis = match format_visibility ( & item. vis ) {
146
+ Some ( s) => s,
147
+ None => return ,
148
+ } ;
145
149
let mut_str = if is_mutable {
146
150
"mut "
147
151
} else {
148
152
""
149
153
} ;
150
- let prefix = format ! ( "{}static {}{}: " ,
151
- format_visibility( item. vis) ,
152
- mut_str,
153
- item. ident) ;
154
+ let prefix = format ! ( "{}static {}{}: " , vis, mut_str, item. ident) ;
154
155
let offset = self . block_indent + prefix. len ( ) ;
155
156
// 1 = ;
156
157
let width = self . config . max_width - offset. width ( ) - 1 ;
@@ -179,7 +180,7 @@ impl<'a> FmtVisitor<'a> {
179
180
unsafety : ast:: Unsafety ,
180
181
constness : ast:: Constness ,
181
182
abi : abi:: Abi ,
182
- vis : ast:: Visibility ,
183
+ vis : & ast:: Visibility ,
183
184
span : Span ,
184
185
block : & ast:: Block )
185
186
-> Option < String > {
@@ -244,7 +245,7 @@ impl<'a> FmtVisitor<'a> {
244
245
sig. unsafety,
245
246
sig. constness,
246
247
sig. abi,
247
- ast:: Visibility :: Inherited ,
248
+ & ast:: Visibility :: Inherited ,
248
249
span,
249
250
false ,
250
251
false ) ) ;
@@ -303,11 +304,14 @@ impl<'a> FmtVisitor<'a> {
303
304
304
305
pub fn visit_enum ( & mut self ,
305
306
ident : ast:: Ident ,
306
- vis : ast:: Visibility ,
307
+ vis : & ast:: Visibility ,
307
308
enum_def : & ast:: EnumDef ,
308
309
generics : & ast:: Generics ,
309
310
span : Span ) {
310
- let header_str = format_header ( "enum " , ident, vis) ;
311
+ let header_str = match format_header ( "enum " , ident, vis) {
312
+ Some ( s) => s,
313
+ None => return ,
314
+ } ;
311
315
self . buffer . push_str ( & header_str) ;
312
316
313
317
let enum_snippet = self . snippet ( span) ;
@@ -414,7 +418,7 @@ impl<'a> FmtVisitor<'a> {
414
418
format_struct ( & context,
415
419
"" ,
416
420
field. node . name ,
417
- ast:: Visibility :: Inherited ,
421
+ & ast:: Visibility :: Inherited ,
418
422
& field. node . data ,
419
423
None ,
420
424
field. span ,
@@ -451,7 +455,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
451
455
ref self_ty,
452
456
ref items) = item. node {
453
457
let mut result = String :: new ( ) ;
454
- result. push_str ( format_visibility ( item. vis ) ) ;
458
+ result. push_str ( try_opt ! ( format_visibility( & item. vis) ) ) ;
455
459
result. push_str ( format_unsafety ( unsafety) ) ;
456
460
result. push_str ( "impl" ) ;
457
461
@@ -583,7 +587,7 @@ fn is_impl_single_line(context: &RewriteContext,
583
587
pub fn format_struct ( context : & RewriteContext ,
584
588
item_name : & str ,
585
589
ident : ast:: Ident ,
586
- vis : ast:: Visibility ,
590
+ vis : & ast:: Visibility ,
587
591
struct_def : & ast:: VariantData ,
588
592
generics : Option < & ast:: Generics > ,
589
593
span : Span ,
@@ -619,7 +623,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
619
623
item. node {
620
624
let mut result = String :: new ( ) ;
621
625
let header = format ! ( "{}{}trait {}" ,
622
- format_visibility( item. vis) ,
626
+ try_opt! ( format_visibility( & item. vis) ) ,
623
627
format_unsafety( unsafety) ,
624
628
item. ident) ;
625
629
@@ -741,10 +745,10 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
741
745
}
742
746
}
743
747
744
- fn format_unit_struct ( item_name : & str , ident : ast:: Ident , vis : ast:: Visibility ) -> Option < String > {
748
+ fn format_unit_struct ( item_name : & str , ident : ast:: Ident , vis : & ast:: Visibility ) -> Option < String > {
745
749
let mut result = String :: with_capacity ( 1024 ) ;
746
750
747
- let header_str = format_header ( item_name, ident, vis) ;
751
+ let header_str = try_opt ! ( format_header( item_name, ident, vis) ) ;
748
752
result. push_str ( & header_str) ;
749
753
result. push ( ';' ) ;
750
754
@@ -754,15 +758,15 @@ fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: ast::Visibility)
754
758
fn format_struct_struct ( context : & RewriteContext ,
755
759
item_name : & str ,
756
760
ident : ast:: Ident ,
757
- vis : ast:: Visibility ,
761
+ vis : & ast:: Visibility ,
758
762
fields : & [ ast:: StructField ] ,
759
763
generics : Option < & ast:: Generics > ,
760
764
span : Span ,
761
765
offset : Indent )
762
766
-> Option < String > {
763
767
let mut result = String :: with_capacity ( 1024 ) ;
764
768
765
- let header_str = format_header ( item_name, ident, vis) ;
769
+ let header_str = try_opt ! ( format_header( item_name, ident, vis) ) ;
766
770
result. push_str ( & header_str) ;
767
771
768
772
let body_lo = context. codemap . span_after ( span, "{" ) ;
@@ -804,13 +808,13 @@ fn format_struct_struct(context: &RewriteContext,
804
808
"}" ,
805
809
|field| {
806
810
// Include attributes and doc comments, if present
807
- if !field. node . attrs . is_empty ( ) {
808
- field. node . attrs [ 0 ] . span . lo
811
+ if !field. attrs . is_empty ( ) {
812
+ field. attrs [ 0 ] . span . lo
809
813
} else {
810
814
field. span . lo
811
815
}
812
816
} ,
813
- |field| field. node . ty . span . hi ,
817
+ |field| field. ty . span . hi ,
814
818
|field| field. rewrite ( context, item_budget, item_indent) ,
815
819
context. codemap . span_after ( span, "{" ) ,
816
820
span. hi ) ;
@@ -835,15 +839,15 @@ fn format_struct_struct(context: &RewriteContext,
835
839
fn format_tuple_struct ( context : & RewriteContext ,
836
840
item_name : & str ,
837
841
ident : ast:: Ident ,
838
- vis : ast:: Visibility ,
842
+ vis : & ast:: Visibility ,
839
843
fields : & [ ast:: StructField ] ,
840
844
generics : Option < & ast:: Generics > ,
841
845
span : Span ,
842
846
offset : Indent )
843
847
-> Option < String > {
844
848
let mut result = String :: with_capacity ( 1024 ) ;
845
849
846
- let header_str = format_header ( item_name, ident, vis) ;
850
+ let header_str = try_opt ! ( format_header( item_name, ident, vis) ) ;
847
851
result. push_str ( & header_str) ;
848
852
849
853
// FIXME(#919): don't lose comments on empty tuple structs.
@@ -890,13 +894,13 @@ fn format_tuple_struct(context: &RewriteContext,
890
894
")" ,
891
895
|field| {
892
896
// Include attributes and doc comments, if present
893
- if !field. node . attrs . is_empty ( ) {
894
- field. node . attrs [ 0 ] . span . lo
897
+ if !field. attrs . is_empty ( ) {
898
+ field. attrs [ 0 ] . span . lo
895
899
} else {
896
900
field. span . lo
897
901
}
898
902
} ,
899
- |field| field. node . ty . span . hi ,
903
+ |field| field. ty . span . hi ,
900
904
|field| field. rewrite ( context, item_budget, item_indent) ,
901
905
context. codemap . span_after ( span, "(" ) ,
902
906
span. hi ) ;
@@ -924,12 +928,12 @@ pub fn rewrite_type_alias(context: &RewriteContext,
924
928
ident : ast:: Ident ,
925
929
ty : & ast:: Ty ,
926
930
generics : & ast:: Generics ,
927
- vis : ast:: Visibility ,
931
+ vis : & ast:: Visibility ,
928
932
span : Span )
929
933
-> Option < String > {
930
934
let mut result = String :: new ( ) ;
931
935
932
- result. push_str ( & format_visibility ( vis) ) ;
936
+ result. push_str ( & try_opt ! ( format_visibility( & vis) ) ) ;
933
937
result. push_str ( "type " ) ;
934
938
result. push_str ( & ident. to_string ( ) ) ;
935
939
@@ -991,21 +995,14 @@ pub fn rewrite_type_alias(context: &RewriteContext,
991
995
992
996
impl Rewrite for ast:: StructField {
993
997
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
994
- if contains_skip ( & self . node . attrs ) {
995
- let span = context. snippet ( mk_sp ( self . node . attrs [ 0 ] . span . lo , self . span . hi ) ) ;
998
+ if contains_skip ( & self . attrs ) {
999
+ let span = context. snippet ( mk_sp ( self . attrs [ 0 ] . span . lo , self . span . hi ) ) ;
996
1000
return wrap_str ( span, context. config . max_width , width, offset) ;
997
1001
}
998
1002
999
- let name = match self . node . kind {
1000
- ast:: StructFieldKind :: NamedField ( ident, _) => Some ( ident. to_string ( ) ) ,
1001
- ast:: StructFieldKind :: UnnamedField ( _) => None ,
1002
- } ;
1003
- let vis = match self . node . kind {
1004
- ast:: StructFieldKind :: NamedField ( _, vis) |
1005
- ast:: StructFieldKind :: UnnamedField ( vis) => format_visibility ( vis) ,
1006
- } ;
1007
- let mut attr_str = try_opt ! ( self . node
1008
- . attrs
1003
+ let name = self . ident ;
1004
+ let vis = try_opt ! ( format_visibility( & self . vis) ) ;
1005
+ let mut attr_str = try_opt ! ( self . attrs
1009
1006
. rewrite( context, context. config. max_width - offset. width( ) , offset) ) ;
1010
1007
if !attr_str. is_empty ( ) {
1011
1008
attr_str. push ( '\n' ) ;
@@ -1019,21 +1016,21 @@ impl Rewrite for ast::StructField {
1019
1016
1020
1017
let last_line_width = last_line_width ( & result) ;
1021
1018
let budget = try_opt ! ( width. checked_sub( last_line_width) ) ;
1022
- let rewrite = try_opt ! ( self . node . ty. rewrite( context, budget, offset + last_line_width) ) ;
1019
+ let rewrite = try_opt ! ( self . ty. rewrite( context, budget, offset + last_line_width) ) ;
1023
1020
Some ( result + & rewrite)
1024
1021
}
1025
1022
}
1026
1023
1027
1024
pub fn rewrite_static ( prefix : & str ,
1028
- vis : ast:: Visibility ,
1025
+ vis : & ast:: Visibility ,
1029
1026
ident : ast:: Ident ,
1030
1027
ty : & ast:: Ty ,
1031
1028
mutability : ast:: Mutability ,
1032
1029
expr_opt : Option < & ptr:: P < ast:: Expr > > ,
1033
1030
context : & RewriteContext )
1034
1031
-> Option < String > {
1035
1032
let prefix = format ! ( "{}{} {}{}: " ,
1036
- format_visibility( vis) ,
1033
+ try_opt! ( format_visibility( vis) ) ,
1037
1034
prefix,
1038
1035
format_mutability( mutability) ,
1039
1036
ident) ;
@@ -1239,7 +1236,7 @@ fn rewrite_fn_base(context: &RewriteContext,
1239
1236
unsafety : ast:: Unsafety ,
1240
1237
constness : ast:: Constness ,
1241
1238
abi : abi:: Abi ,
1242
- vis : ast:: Visibility ,
1239
+ vis : & ast:: Visibility ,
1243
1240
span : Span ,
1244
1241
newline_brace : bool ,
1245
1242
has_body : bool )
@@ -1252,7 +1249,7 @@ fn rewrite_fn_base(context: &RewriteContext,
1252
1249
1253
1250
let mut result = String :: with_capacity ( 1024 ) ;
1254
1251
// Vis unsafety abi.
1255
- result. push_str ( format_visibility ( vis) ) ;
1252
+ result. push_str ( try_opt ! ( format_visibility( vis) ) ) ;
1256
1253
1257
1254
if let ast:: Constness :: Const = constness {
1258
1255
result. push_str ( "const " ) ;
@@ -1808,8 +1805,8 @@ fn rewrite_where_clause(context: &RewriteContext,
1808
1805
}
1809
1806
}
1810
1807
1811
- fn format_header ( item_name : & str , ident : ast:: Ident , vis : ast:: Visibility ) -> String {
1812
- format ! ( "{}{}{}" , format_visibility( vis) , item_name, ident)
1808
+ fn format_header ( item_name : & str , ident : ast:: Ident , vis : & ast:: Visibility ) -> Option < String > {
1809
+ Some ( format ! ( "{}{}{}" , try_opt! ( format_visibility( vis) ) , item_name, ident) )
1813
1810
}
1814
1811
1815
1812
fn format_generics ( context : & RewriteContext ,
0 commit comments