@@ -156,13 +156,13 @@ pub(crate) fn rewrite_macro(
156
156
context : & RewriteContext < ' _ > ,
157
157
shape : Shape ,
158
158
position : MacroPosition ,
159
- ) -> Option < String > {
159
+ ) -> ( Option < String > , Option < Delimiter > ) {
160
160
let should_skip = context
161
161
. skip_context
162
162
. macros
163
163
. skip ( context. snippet ( mac. path . span ) ) ;
164
164
if should_skip {
165
- None
165
+ ( None , None )
166
166
} else {
167
167
let guard = context. enter_macro ( ) ;
168
168
let result = catch_unwind ( AssertUnwindSafe ( || {
@@ -176,27 +176,29 @@ pub(crate) fn rewrite_macro(
176
176
)
177
177
} ) ) ;
178
178
match result {
179
- Err ( ..) | Ok ( None ) => {
179
+ Err ( ..) | Ok ( ( None , .. ) ) => {
180
180
context. macro_rewrite_failure . replace ( true ) ;
181
- None
181
+ ( None , None )
182
182
}
183
183
Ok ( rw) => rw,
184
184
}
185
185
}
186
186
}
187
187
188
+ //We return not only string but also new delimiter if it changes
189
+ //It needs to remove semicolon if delimiter changes
188
190
fn rewrite_macro_inner (
189
191
mac : & ast:: MacCall ,
190
192
extra_ident : Option < symbol:: Ident > ,
191
193
context : & RewriteContext < ' _ > ,
192
194
shape : Shape ,
193
195
position : MacroPosition ,
194
196
is_nested_macro : bool ,
195
- ) -> Option < String > {
197
+ ) -> ( Option < String > , Option < Delimiter > ) {
196
198
if context. config . use_try_shorthand ( ) {
197
199
if let Some ( expr) = convert_try_mac ( mac, context) {
198
200
context. leave_macro ( ) ;
199
- return expr. rewrite ( context, shape) ;
201
+ return ( expr. rewrite ( context, shape) , None ) ;
200
202
}
201
203
}
202
204
@@ -214,26 +216,37 @@ fn rewrite_macro_inner(
214
216
let ts = mac. args . tokens . clone ( ) ;
215
217
let has_comment = contains_comment ( context. snippet ( mac. span ( ) ) ) ;
216
218
if ts. is_empty ( ) && !has_comment {
217
- return match style {
218
- Delimiter :: Parenthesis if position == MacroPosition :: Item => {
219
- Some ( format ! ( "{macro_name}();" ) )
220
- }
221
- Delimiter :: Bracket if position == MacroPosition :: Item => {
222
- Some ( format ! ( "{macro_name}[];" ) )
223
- }
224
- Delimiter :: Parenthesis => Some ( format ! ( "{macro_name}()" ) ) ,
225
- Delimiter :: Bracket => Some ( format ! ( "{macro_name}[]" ) ) ,
226
- Delimiter :: Brace => Some ( format ! ( "{macro_name} {{}}" ) ) ,
227
- _ => unreachable ! ( ) ,
228
- } ;
219
+ return (
220
+ match style {
221
+ Delimiter :: Parenthesis if position == MacroPosition :: Item => {
222
+ Some ( format ! ( "{macro_name}();" ) )
223
+ }
224
+ Delimiter :: Bracket if position == MacroPosition :: Item => {
225
+ Some ( format ! ( "{macro_name}[];" ) )
226
+ }
227
+ Delimiter :: Parenthesis => Some ( format ! ( "{macro_name}()" ) ) ,
228
+ Delimiter :: Bracket => Some ( format ! ( "{macro_name}[]" ) ) ,
229
+ Delimiter :: Brace => Some ( format ! ( "{macro_name} {{}}" ) ) ,
230
+ _ => unreachable ! ( ) ,
231
+ } ,
232
+ if original_style != style {
233
+ Some ( style)
234
+ } else {
235
+ None
236
+ } ,
237
+ ) ;
229
238
}
230
239
// Format well-known macros which cannot be parsed as a valid AST.
231
240
if macro_name == "lazy_static!" && !has_comment {
232
241
if let success @ Some ( ..) = format_lazy_static ( context, shape, ts. clone ( ) ) {
233
- if original_style == Delimiter :: Parenthesis {
234
- context. lazy_static_success . replace ( true ) ;
235
- }
236
- return success;
242
+ return (
243
+ success,
244
+ if original_style != Delimiter :: Brace {
245
+ Some ( Delimiter :: Brace )
246
+ } else {
247
+ None
248
+ } ,
249
+ ) ;
237
250
}
238
251
}
239
252
@@ -244,103 +257,117 @@ fn rewrite_macro_inner(
244
257
} = match parse_macro_args ( context, ts, style, is_forced_bracket) {
245
258
Some ( args) => args,
246
259
None => {
247
- return return_macro_parse_failure_fallback (
248
- context,
249
- shape. indent ,
250
- position,
251
- mac. span ( ) ,
260
+ return (
261
+ return_macro_parse_failure_fallback ( context, shape. indent , position, mac. span ( ) ) ,
262
+ None ,
252
263
) ;
253
264
}
254
265
} ;
255
266
256
267
if !arg_vec. is_empty ( ) && arg_vec. iter ( ) . all ( MacroArg :: is_item) {
257
- return rewrite_macro_with_items (
258
- context,
259
- & arg_vec,
260
- & macro_name,
261
- shape,
262
- style,
263
- original_style,
264
- position,
265
- mac. span ( ) ,
268
+ return (
269
+ rewrite_macro_with_items (
270
+ context,
271
+ & arg_vec,
272
+ & macro_name,
273
+ shape,
274
+ style,
275
+ original_style,
276
+ position,
277
+ mac. span ( ) ,
278
+ ) ,
279
+ if original_style != style {
280
+ Some ( style)
281
+ } else {
282
+ None
283
+ } ,
266
284
) ;
267
285
}
268
286
269
- match style {
270
- Delimiter :: Parenthesis => {
271
- // Handle special case: `vec!(expr; expr)`
272
- if vec_with_semi {
273
- handle_vec_semi ( context, shape, arg_vec, macro_name, style)
274
- } else {
275
- // Format macro invocation as function call, preserve the trailing
276
- // comma because not all macros support them.
277
- overflow:: rewrite_with_parens (
278
- context,
279
- & macro_name,
280
- arg_vec. iter ( ) ,
281
- shape,
282
- mac. span ( ) ,
283
- context. config . fn_call_width ( ) ,
284
- if trailing_comma {
287
+ (
288
+ match style {
289
+ Delimiter :: Parenthesis => {
290
+ // Handle special case: `vec!(expr; expr)`
291
+ if vec_with_semi {
292
+ handle_vec_semi ( context, shape, arg_vec, macro_name, style)
293
+ } else {
294
+ // Format macro invocation as function call, preserve the trailing
295
+ // comma because not all macros support them.
296
+ overflow:: rewrite_with_parens (
297
+ context,
298
+ & macro_name,
299
+ arg_vec. iter ( ) ,
300
+ shape,
301
+ mac. span ( ) ,
302
+ context. config . fn_call_width ( ) ,
303
+ if trailing_comma {
304
+ Some ( SeparatorTactic :: Always )
305
+ } else {
306
+ Some ( SeparatorTactic :: Never )
307
+ } ,
308
+ )
309
+ . map ( |rw| match position {
310
+ MacroPosition :: Item => format ! ( "{};" , rw) ,
311
+ _ => rw,
312
+ } )
313
+ }
314
+ }
315
+ Delimiter :: Bracket => {
316
+ // Handle special case: `vec![expr; expr]`
317
+ if vec_with_semi {
318
+ handle_vec_semi ( context, shape, arg_vec, macro_name, style)
319
+ } else {
320
+ // If we are rewriting `vec!` macro or other special macros,
321
+ // then we can rewrite this as a usual array literal.
322
+ // Otherwise, we must preserve the original existence of trailing comma.
323
+ let mut force_trailing_comma = if trailing_comma {
285
324
Some ( SeparatorTactic :: Always )
286
325
} else {
287
326
Some ( SeparatorTactic :: Never )
288
- } ,
289
- )
290
- . map ( |rw| match position {
291
- MacroPosition :: Item => format ! ( "{};" , rw) ,
292
- _ => rw,
293
- } )
294
- }
295
- }
296
- Delimiter :: Bracket => {
297
- // Handle special case: `vec![expr; expr]`
298
- if vec_with_semi {
299
- handle_vec_semi ( context, shape, arg_vec, macro_name, style)
300
- } else {
301
- // If we are rewriting `vec!` macro or other special macros,
302
- // then we can rewrite this as a usual array literal.
303
- // Otherwise, we must preserve the original existence of trailing comma.
304
- let mut force_trailing_comma = if trailing_comma {
305
- Some ( SeparatorTactic :: Always )
306
- } else {
307
- Some ( SeparatorTactic :: Never )
308
- } ;
309
- if is_forced_bracket && !is_nested_macro {
310
- context. leave_macro ( ) ;
311
- if context. use_block_indent ( ) {
312
- force_trailing_comma = Some ( SeparatorTactic :: Vertical ) ;
313
327
} ;
328
+ if is_forced_bracket && !is_nested_macro {
329
+ context. leave_macro ( ) ;
330
+ if context. use_block_indent ( ) {
331
+ force_trailing_comma = Some ( SeparatorTactic :: Vertical ) ;
332
+ } ;
333
+ }
334
+ rewrite_array (
335
+ & macro_name,
336
+ arg_vec. iter ( ) ,
337
+ mac. span ( ) ,
338
+ context,
339
+ shape,
340
+ force_trailing_comma,
341
+ Some ( original_style) ,
342
+ )
343
+ . map ( |rewrite| {
344
+ let comma = match position {
345
+ MacroPosition :: Item => ";" ,
346
+ _ => "" ,
347
+ } ;
348
+
349
+ format ! ( "{rewrite}{comma}" )
350
+ } )
314
351
}
315
- let rewrite = rewrite_array (
316
- & macro_name,
317
- arg_vec. iter ( ) ,
318
- mac. span ( ) ,
319
- context,
320
- shape,
321
- force_trailing_comma,
322
- Some ( original_style) ,
323
- ) ?;
324
- let comma = match position {
325
- MacroPosition :: Item => ";" ,
326
- _ => "" ,
327
- } ;
328
-
329
- Some ( format ! ( "{rewrite}{comma}" ) )
330
352
}
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}" ) ) ,
353
+ Delimiter :: Brace => {
354
+ // For macro invocations with braces, always put a space between
355
+ // the `macro_name!` and `{ /* macro_body */ }` but skip modifying
356
+ // anything in between the braces (for now).
357
+ let snippet = context . snippet ( mac . span ( ) ) . trim_start_matches ( |c| c != '{' ) ;
358
+ match trim_left_preserve_layout ( snippet, shape . indent , context. config ) {
359
+ Some ( macro_body ) => Some ( format ! ( "{macro_name} {macro_body}" ) ) ,
360
+ None => Some ( format ! ( "{macro_name} {snippet }" ) ) ,
361
+ }
340
362
}
341
- }
342
- _ => unreachable ! ( ) ,
343
- }
363
+ _ => unreachable ! ( ) ,
364
+ } ,
365
+ if original_style != style {
366
+ Some ( style)
367
+ } else {
368
+ None
369
+ } ,
370
+ )
344
371
}
345
372
346
373
fn handle_vec_semi (
0 commit comments