@@ -186,7 +186,7 @@ pub(crate) fn rewrite_macro(
186186}
187187
188188//We return not only string but also new delimiter if it changes
189- //It needs to remove semicolon if delimiter changes
189+ //It needs to remove semicolon if delimiter changes in some situations
190190fn rewrite_macro_inner (
191191 mac : & ast:: MacCall ,
192192 extra_ident : Option < symbol:: Ident > ,
@@ -233,14 +233,7 @@ fn rewrite_macro_inner(
233233 // Format well-known macros which cannot be parsed as a valid AST.
234234 if macro_name == "lazy_static!" && !has_comment {
235235 if let success @ Some ( ..) = format_lazy_static ( context, shape, ts. clone ( ) ) {
236- return (
237- success,
238- if original_style != Delimiter :: Brace {
239- Some ( Delimiter :: Brace )
240- } else {
241- None
242- } ,
243- ) ;
236+ return ( success, Some ( Delimiter :: Brace ) ) ;
244237 }
245238 }
246239
@@ -271,90 +264,84 @@ fn rewrite_macro_inner(
271264 return ( rewrite, Some ( style) ) ;
272265 }
273266
274- (
275- match style {
276- Delimiter :: Parenthesis => {
277- // Handle special case: `vec!(expr; expr)`
278- if vec_with_semi {
279- handle_vec_semi ( context, shape, arg_vec, macro_name, style)
280- } else {
281- // Format macro invocation as function call, preserve the trailing
282- // comma because not all macros support them.
283- overflow:: rewrite_with_parens (
284- context,
285- & macro_name,
286- arg_vec. iter ( ) ,
287- shape,
288- mac. span ( ) ,
289- context. config . fn_call_width ( ) ,
290- if trailing_comma {
291- Some ( SeparatorTactic :: Always )
292- } else {
293- Some ( SeparatorTactic :: Never )
294- } ,
295- )
296- . map ( |rw| match position {
297- MacroPosition :: Item => format ! ( "{};" , rw) ,
298- _ => rw,
299- } )
300- }
301- }
302- Delimiter :: Bracket => {
303- // Handle special case: `vec![expr; expr]`
304- if vec_with_semi {
305- handle_vec_semi ( context, shape, arg_vec, macro_name, style)
306- } else {
307- // If we are rewriting `vec!` macro or other special macros,
308- // then we can rewrite this as a usual array literal.
309- // Otherwise, we must preserve the original existence of trailing comma.
310- let mut force_trailing_comma = if trailing_comma {
267+ let rewrite = match style {
268+ Delimiter :: Parenthesis => {
269+ // Handle special case: `vec!(expr; expr)`
270+ if vec_with_semi {
271+ handle_vec_semi ( context, shape, arg_vec, macro_name, style)
272+ } else {
273+ // Format macro invocation as function call, preserve the trailing
274+ // comma because not all macros support them.
275+ overflow:: rewrite_with_parens (
276+ context,
277+ & macro_name,
278+ arg_vec. iter ( ) ,
279+ shape,
280+ mac. span ( ) ,
281+ context. config . fn_call_width ( ) ,
282+ if trailing_comma {
311283 Some ( SeparatorTactic :: Always )
312284 } else {
313285 Some ( SeparatorTactic :: Never )
286+ } ,
287+ )
288+ . map ( |rw| match position {
289+ MacroPosition :: Item => format ! ( "{};" , rw) ,
290+ _ => rw,
291+ } )
292+ }
293+ }
294+ Delimiter :: Bracket => {
295+ // Handle special case: `vec![expr; expr]`
296+ if vec_with_semi {
297+ handle_vec_semi ( context, shape, arg_vec, macro_name, style)
298+ } else {
299+ // If we are rewriting `vec!` macro or other special macros,
300+ // then we can rewrite this as a usual array literal.
301+ // Otherwise, we must preserve the original existence of trailing comma.
302+ let mut force_trailing_comma = if trailing_comma {
303+ Some ( SeparatorTactic :: Always )
304+ } else {
305+ Some ( SeparatorTactic :: Never )
306+ } ;
307+ if is_forced_bracket && !is_nested_macro {
308+ context. leave_macro ( ) ;
309+ if context. use_block_indent ( ) {
310+ force_trailing_comma = Some ( SeparatorTactic :: Vertical ) ;
314311 } ;
315- if is_forced_bracket && !is_nested_macro {
316- context. leave_macro ( ) ;
317- if context. use_block_indent ( ) {
318- force_trailing_comma = Some ( SeparatorTactic :: Vertical ) ;
319- } ;
320- }
321- rewrite_array (
322- & macro_name,
323- arg_vec. iter ( ) ,
324- mac. span ( ) ,
325- context,
326- shape,
327- force_trailing_comma,
328- Some ( original_style) ,
329- )
330- . map ( |rewrite| {
331- let comma = match position {
332- MacroPosition :: Item => ";" ,
333- _ => "" ,
334- } ;
335-
336- format ! ( "{rewrite}{comma}" )
337- } )
338312 }
313+ let Some ( rewrite) = rewrite_array (
314+ & macro_name,
315+ arg_vec. iter ( ) ,
316+ mac. span ( ) ,
317+ context,
318+ shape,
319+ force_trailing_comma,
320+ Some ( original_style) ,
321+ ) else {
322+ return ( None , None ) ;
323+ } ;
324+
325+ let comma = match position {
326+ MacroPosition :: Item => ";" ,
327+ _ => "" ,
328+ } ;
329+ Some ( format ! ( "{rewrite}{comma}" ) )
339330 }
340- Delimiter :: Brace => {
341- // For macro invocations with braces, always put a space between
342- // the `macro_name!` and `{ /* macro_body */ }` but skip modifying
343- // anything in between the braces (for now).
344- let snippet = context . snippet ( mac . span ( ) ) . trim_start_matches ( |c| c != '{' ) ;
345- match trim_left_preserve_layout ( snippet, shape . indent , context. config ) {
346- Some ( macro_body ) => Some ( format ! ( "{macro_name} {macro_body}" ) ) ,
347- None => Some ( format ! ( "{macro_name} {snippet }" ) ) ,
348- }
331+ }
332+ Delimiter :: Brace => {
333+ // For macro invocations with braces, always put a space between
334+ // the `macro_name!` and `{ /* macro_body */ }` but skip modifying
335+ // anything in between the braces (for now).
336+ let snippet = context. snippet ( mac . span ( ) ) . trim_start_matches ( |c| c != '{' ) ;
337+ match trim_left_preserve_layout ( snippet , shape . indent , context . config ) {
338+ Some ( macro_body ) => Some ( format ! ( "{macro_name} {macro_body }" ) ) ,
339+ None => Some ( format ! ( "{macro_name} {snippet}" ) ) ,
349340 }
350- _ => unreachable ! ( ) ,
351- } ,
352- if original_style != style {
353- Some ( style)
354- } else {
355- None
356- } ,
357- )
341+ }
342+ _ => unreachable ! ( ) ,
343+ } ;
344+ return ( rewrite, Some ( style) ) ;
358345}
359346
360347fn handle_vec_semi (
0 commit comments