@@ -28,6 +28,7 @@ use crate::config::lists::*;
28
28
use crate :: expr:: { rewrite_array, rewrite_assign_rhs, RhsAssignKind } ;
29
29
use crate :: lists:: { itemize_list, write_list, ListFormatting } ;
30
30
use crate :: overflow;
31
+ use crate :: parse:: macros:: lazy_static:: parse_lazy_static;
31
32
use crate :: parse:: macros:: { build_parser, parse_macro_args, ParsedMacroArgs } ;
32
33
use crate :: rewrite:: { Rewrite , RewriteContext } ;
33
34
use crate :: shape:: { Indent , Shape } ;
@@ -1297,50 +1298,18 @@ fn format_lazy_static(
1297
1298
ts : TokenStream ,
1298
1299
) -> Option < String > {
1299
1300
let mut result = String :: with_capacity ( 1024 ) ;
1300
- let mut parser = build_parser ( context, ts) ;
1301
1301
let nested_shape = shape
1302
1302
. block_indent ( context. config . tab_spaces ( ) )
1303
1303
. with_max_width ( context. config ) ;
1304
1304
1305
1305
result. push_str ( "lazy_static! {" ) ;
1306
1306
result. push_str ( & nested_shape. indent . to_string_with_newline ( context. config ) ) ;
1307
1307
1308
- macro_rules! parse_or {
1309
- ( $method: ident $( , ) * $( $arg: expr) ,* $( , ) * ) => {
1310
- match parser. $method( $( $arg, ) * ) {
1311
- Ok ( val) => {
1312
- if parser. sess. span_diagnostic. has_errors( ) {
1313
- parser. sess. span_diagnostic. reset_err_count( ) ;
1314
- return None ;
1315
- } else {
1316
- val
1317
- }
1318
- }
1319
- Err ( mut err) => {
1320
- err. cancel( ) ;
1321
- parser. sess. span_diagnostic. reset_err_count( ) ;
1322
- return None ;
1323
- }
1324
- }
1325
- }
1326
- }
1327
-
1328
- while parser. token . kind != TokenKind :: Eof {
1329
- // Parse a `lazy_static!` item.
1330
- let vis = crate :: utils:: format_visibility (
1331
- context,
1332
- & parse_or ! ( parse_visibility, rustc_parse:: parser:: FollowedByType :: No ) ,
1333
- ) ;
1334
- parser. eat_keyword ( kw:: Static ) ;
1335
- parser. eat_keyword ( kw:: Ref ) ;
1336
- let id = parse_or ! ( parse_ident) ;
1337
- parser. eat ( & TokenKind :: Colon ) ;
1338
- let ty = parse_or ! ( parse_ty) ;
1339
- parser. eat ( & TokenKind :: Eq ) ;
1340
- let expr = parse_or ! ( parse_expr) ;
1341
- parser. eat ( & TokenKind :: Semi ) ;
1342
-
1308
+ let parsed_elems = parse_lazy_static ( context, ts) ?;
1309
+ let last = parsed_elems. len ( ) - 1 ;
1310
+ for ( i, ( vis, id, ty, expr) ) in parsed_elems. iter ( ) . enumerate ( ) {
1343
1311
// Rewrite as a static item.
1312
+ let vis = crate :: utils:: format_visibility ( context, vis) ;
1344
1313
let mut stmt = String :: with_capacity ( 128 ) ;
1345
1314
stmt. push_str ( & format ! (
1346
1315
"{}static ref {}: {} =" ,
@@ -1356,7 +1325,7 @@ fn format_lazy_static(
1356
1325
nested_shape. sub_width ( 1 ) ?,
1357
1326
) ?) ;
1358
1327
result. push ( ';' ) ;
1359
- if parser . token . kind != TokenKind :: Eof {
1328
+ if i != last {
1360
1329
result. push_str ( & nested_shape. indent . to_string_with_newline ( context. config ) ) ;
1361
1330
}
1362
1331
}
0 commit comments