Skip to content

Commit 42466c8

Browse files
author
Alexander Glusker
committed
fix additional semicolon during changine parentheses style in lazy_static macro
1 parent c2f0fe9 commit 42466c8

File tree

2 files changed

+92
-104
lines changed

2 files changed

+92
-104
lines changed

src/macros.rs

Lines changed: 73 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -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
190190
fn 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

360347
fn handle_vec_semi(

src/visitor.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -683,36 +683,37 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
683683

684684
// 1 = ;
685685
let shape = self.shape().saturating_sub_width(1);
686-
let (rewrite, new_delimiter) =
686+
let original_style = macro_style(mac, &self.get_context());
687+
let (rewrite, rewrite_style) =
687688
self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
688689
// As of v638 of the rustc-ap-* crates, the associated span no longer includes
689690
// the trailing semicolon. This determines the correct span to ensure scenarios
690691
// with whitespace between the delimiters and trailing semi (i.e. `foo!(abc) ;`)
691692
// are formatted correctly.
692-
let (span, rewrite) = match macro_style(mac, &self.get_context()) {
693-
Delimiter::Bracket | Delimiter::Parenthesis if MacroPosition::Item == pos => {
694-
let search_span = mk_sp(mac.span().hi(), self.snippet_provider.end_pos());
695-
let hi = self.snippet_provider.span_before(search_span, ";");
696-
let target_span = mk_sp(mac.span().lo(), hi + BytePos(1));
697-
let rewrite = rewrite.map(|rw| {
693+
let rewrite = match rewrite_style.unwrap_or(original_style) {
694+
Delimiter::Bracket | Delimiter::Parenthesis if MacroPosition::Item == pos => rewrite
695+
.map(|rw| {
698696
if !rw.ends_with(';') {
699697
format!("{};", rw)
700698
} else {
701699
rw
702700
}
703-
});
704-
(target_span, rewrite)
705-
}
706-
_ => (mac.span(), rewrite),
701+
}),
702+
_ => rewrite,
707703
};
708704

709-
//we need to ignore semicolon if it is after {} instead of ()
710-
let span = if new_delimiter == Some(Delimiter::Brace) && MacroPosition::Statement == pos {
711-
let search_span = mk_sp(mac.span().hi(), self.snippet_provider.end_pos());
712-
let hi = self.snippet_provider.span_before(search_span, ";");
713-
mk_sp(mac.span().lo(), hi + BytePos(1))
714-
} else {
715-
span
705+
let span = match original_style {
706+
Delimiter::Bracket | Delimiter::Parenthesis
707+
if (MacroPosition::Item == pos)
708+
|| (MacroPosition::Statement == pos)
709+
&& rewrite_style
710+
.is_some_and(|x| x != original_style && x == Delimiter::Brace) =>
711+
{
712+
let search_span = mk_sp(mac.span().hi(), self.snippet_provider.end_pos());
713+
let hi = self.snippet_provider.span_before(search_span, ";");
714+
mk_sp(mac.span().lo(), hi + BytePos(1))
715+
}
716+
_ => mac.span(),
716717
};
717718
self.push_rewrite(span, rewrite);
718719
}

0 commit comments

Comments
 (0)