@@ -28,6 +28,7 @@ use crate::config::lists::*;
2828use crate :: expr:: { rewrite_array, rewrite_assign_rhs, RhsAssignKind } ;
2929use crate :: lists:: { itemize_list, write_list, ListFormatting } ;
3030use crate :: overflow;
31+ use crate :: parse:: macros:: lazy_static:: parse_lazy_static;
3132use crate :: parse:: macros:: { build_parser, parse_macro_args, ParsedMacroArgs } ;
3233use crate :: rewrite:: { Rewrite , RewriteContext } ;
3334use crate :: shape:: { Indent , Shape } ;
@@ -1297,50 +1298,18 @@ fn format_lazy_static(
12971298 ts : TokenStream ,
12981299) -> Option < String > {
12991300 let mut result = String :: with_capacity ( 1024 ) ;
1300- let mut parser = build_parser ( context, ts) ;
13011301 let nested_shape = shape
13021302 . block_indent ( context. config . tab_spaces ( ) )
13031303 . with_max_width ( context. config ) ;
13041304
13051305 result. push_str ( "lazy_static! {" ) ;
13061306 result. push_str ( & nested_shape. indent . to_string_with_newline ( context. config ) ) ;
13071307
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 ( ) {
13431311 // Rewrite as a static item.
1312+ let vis = crate :: utils:: format_visibility ( context, vis) ;
13441313 let mut stmt = String :: with_capacity ( 128 ) ;
13451314 stmt. push_str ( & format ! (
13461315 "{}static ref {}: {} =" ,
@@ -1356,7 +1325,7 @@ fn format_lazy_static(
13561325 nested_shape. sub_width ( 1 ) ?,
13571326 ) ?) ;
13581327 result. push ( ';' ) ;
1359- if parser . token . kind != TokenKind :: Eof {
1328+ if i != last {
13601329 result. push_str ( & nested_shape. indent . to_string_with_newline ( context. config ) ) ;
13611330 }
13621331 }
0 commit comments