Skip to content

Commit 25a9a5f

Browse files
author
Alexander Glusker
committed
fix additional semicolon during changing parentheses style
1 parent 202fa22 commit 25a9a5f

File tree

8 files changed

+72
-38
lines changed

8 files changed

+72
-38
lines changed

src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ pub(crate) fn format_expr(
225225
| ast::ExprKind::MethodCall(..)
226226
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
227227
ast::ExprKind::MacCall(ref mac) => {
228-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
228+
let (rewrite, _) = rewrite_macro(mac, None, context, shape, MacroPosition::Expression);
229+
rewrite.or_else(|| {
229230
wrap_str(
230231
context.snippet(expr.span).to_owned(),
231232
context.config.max_width(),

src/items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3374,7 +3374,8 @@ impl Rewrite for ast::ForeignItem {
33743374
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
33753375
}
33763376
ast::ForeignItemKind::MacCall(ref mac) => {
3377-
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
3377+
let (rewrite, _) = rewrite_macro(mac, None, context, shape, MacroPosition::Item);
3378+
rewrite
33783379
}
33793380
}?;
33803381

src/macros.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ pub(crate) fn rewrite_macro(
156156
context: &RewriteContext<'_>,
157157
shape: Shape,
158158
position: MacroPosition,
159-
) -> Option<String> {
159+
) -> (Option<String>, Option<Delimiter>) {
160160
let should_skip = context
161161
.skip_context
162162
.macros
163163
.skip(context.snippet(mac.path.span));
164164
if should_skip {
165-
None
165+
(None, None)
166166
} else {
167167
let guard = context.enter_macro();
168168
let result = catch_unwind(AssertUnwindSafe(|| {
@@ -176,27 +176,29 @@ pub(crate) fn rewrite_macro(
176176
)
177177
}));
178178
match result {
179-
Err(..) | Ok(None) => {
179+
Err(..) | Ok((None, ..)) => {
180180
context.macro_rewrite_failure.replace(true);
181-
None
181+
(None, None)
182182
}
183183
Ok(rw) => rw,
184184
}
185185
}
186186
}
187187

188+
//We return not only string but also new delimiter if it changes
189+
//It needs to remove semicolon if delimiter changes in some situations
188190
fn rewrite_macro_inner(
189191
mac: &ast::MacCall,
190192
extra_ident: Option<symbol::Ident>,
191193
context: &RewriteContext<'_>,
192194
shape: Shape,
193195
position: MacroPosition,
194196
is_nested_macro: bool,
195-
) -> Option<String> {
197+
) -> (Option<String>, Option<Delimiter>) {
196198
if context.config.use_try_shorthand() {
197199
if let Some(expr) = convert_try_mac(mac, context) {
198200
context.leave_macro();
199-
return expr.rewrite(context, shape);
201+
return (expr.rewrite(context, shape), None);
200202
}
201203
}
202204

@@ -214,7 +216,7 @@ fn rewrite_macro_inner(
214216
let ts = mac.args.tokens.clone();
215217
let has_comment = contains_comment(context.snippet(mac.span()));
216218
if ts.is_empty() && !has_comment {
217-
return match style {
219+
let rewrite = match style {
218220
Delimiter::Parenthesis if position == MacroPosition::Item => {
219221
Some(format!("{macro_name}();"))
220222
}
@@ -226,11 +228,12 @@ fn rewrite_macro_inner(
226228
Delimiter::Brace => Some(format!("{macro_name} {{}}")),
227229
_ => unreachable!(),
228230
};
231+
return (rewrite, Some(style));
229232
}
230233
// Format well-known macros which cannot be parsed as a valid AST.
231234
if macro_name == "lazy_static!" && !has_comment {
232235
if let success @ Some(..) = format_lazy_static(context, shape, ts.clone()) {
233-
return success;
236+
return (success, Some(Delimiter::Brace));
234237
}
235238
}
236239

@@ -241,17 +244,14 @@ fn rewrite_macro_inner(
241244
} = match parse_macro_args(context, ts, style, is_forced_bracket) {
242245
Some(args) => args,
243246
None => {
244-
return return_macro_parse_failure_fallback(
245-
context,
246-
shape.indent,
247-
position,
248-
mac.span(),
249-
);
247+
let rewrite =
248+
return_macro_parse_failure_fallback(context, shape.indent, position, mac.span());
249+
return (rewrite, None);
250250
}
251251
};
252252

253253
if !arg_vec.is_empty() && arg_vec.iter().all(MacroArg::is_item) {
254-
return rewrite_macro_with_items(
254+
let rewrite = rewrite_macro_with_items(
255255
context,
256256
&arg_vec,
257257
&macro_name,
@@ -261,9 +261,10 @@ fn rewrite_macro_inner(
261261
position,
262262
mac.span(),
263263
);
264+
return (rewrite, Some(style));
264265
}
265266

266-
match style {
267+
let rewrite = match style {
267268
Delimiter::Parenthesis => {
268269
// Handle special case: `vec!(expr; expr)`
269270
if vec_with_semi {
@@ -309,20 +310,22 @@ fn rewrite_macro_inner(
309310
force_trailing_comma = Some(SeparatorTactic::Vertical);
310311
};
311312
}
312-
let rewrite = rewrite_array(
313+
let Some(rewrite) = rewrite_array(
313314
&macro_name,
314315
arg_vec.iter(),
315316
mac.span(),
316317
context,
317318
shape,
318319
force_trailing_comma,
319320
Some(original_style),
320-
)?;
321+
) else {
322+
return (None, None);
323+
};
324+
321325
let comma = match position {
322326
MacroPosition::Item => ";",
323327
_ => "",
324328
};
325-
326329
Some(format!("{rewrite}{comma}"))
327330
}
328331
}
@@ -337,7 +340,8 @@ fn rewrite_macro_inner(
337340
}
338341
}
339342
_ => unreachable!(),
340-
}
343+
};
344+
return (rewrite, Some(style));
341345
}
342346

343347
fn handle_vec_semi(

src/patterns.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ impl Rewrite for Pat {
269269
shape,
270270
),
271271
PatKind::MacCall(ref mac) => {
272-
rewrite_macro(mac, None, context, shape, MacroPosition::Pat)
272+
let (rewrite, _) = rewrite_macro(mac, None, context, shape, MacroPosition::Pat);
273+
rewrite
273274
}
274275
PatKind::Paren(ref pat) => pat
275276
.rewrite(context, shape.offset_left(1)?.sub_width(1)?)

src/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,9 @@ impl Rewrite for ast::Ty {
829829
ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
830830
ast::TyKind::Never => Some(String::from("!")),
831831
ast::TyKind::MacCall(ref mac) => {
832-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
832+
let (rewrite, _) =
833+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression);
834+
rewrite
833835
}
834836
ast::TyKind::ImplicitSelf => Some(String::from("")),
835837
ast::TyKind::ImplTrait(_, ref it) => {

src/visitor.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -683,28 +683,38 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
683683

684684
// 1 = ;
685685
let shape = self.shape().saturating_sub_width(1);
686-
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
686+
let original_style = macro_style(mac, &self.get_context());
687+
let (rewrite, rewrite_style) =
688+
self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
687689
// As of v638 of the rustc-ap-* crates, the associated span no longer includes
688690
// the trailing semicolon. This determines the correct span to ensure scenarios
689691
// with whitespace between the delimiters and trailing semi (i.e. `foo!(abc) ;`)
690692
// are formatted correctly.
691-
let (span, rewrite) = match macro_style(mac, &self.get_context()) {
692-
Delimiter::Bracket | Delimiter::Parenthesis if MacroPosition::Item == pos => {
693-
let search_span = mk_sp(mac.span().hi(), self.snippet_provider.end_pos());
694-
let hi = self.snippet_provider.span_before(search_span, ";");
695-
let target_span = mk_sp(mac.span().lo(), hi + BytePos(1));
696-
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| {
697696
if !rw.ends_with(';') {
698697
format!("{};", rw)
699698
} else {
700699
rw
701700
}
702-
});
703-
(target_span, rewrite)
704-
}
705-
_ => (mac.span(), rewrite),
701+
}),
702+
_ => rewrite,
706703
};
707704

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(),
717+
};
708718
self.push_rewrite(span, rewrite);
709719
}
710720

@@ -989,13 +999,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
989999
}
9901000
}
9911001

992-
pub(crate) fn with_context<F>(&mut self, f: F) -> Option<String>
1002+
pub(crate) fn with_context<F, RESULT>(&mut self, f: F) -> RESULT
9931003
where
994-
F: Fn(&RewriteContext<'_>) -> Option<String>,
1004+
F: Fn(&RewriteContext<'_>) -> RESULT,
9951005
{
9961006
let context = self.get_context();
9971007
let result = f(&context);
998-
9991008
self.macro_rewrite_failure |= context.macro_rewrite_failure.get();
10001009
result
10011010
}

tests/source/issue-6013/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
lazy_static!(
3+
static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
4+
static ref CASCADE_IP: String = std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
5+
static ref CASCADE_PORT: String = std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
6+
) ;
7+
}

tests/target/issue-6013/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
lazy_static! {
3+
static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
4+
static ref CASCADE_IP: String =
5+
std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
6+
static ref CASCADE_PORT: String =
7+
std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
8+
}
9+
}

0 commit comments

Comments
 (0)