Skip to content

Commit dd662af

Browse files
Alexander Gluskerrscprof
authored andcommitted
fix additional semicolon during changing parentheses style
1 parent 7289391 commit dd662af

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
@@ -155,13 +155,13 @@ pub(crate) fn rewrite_macro(
155155
context: &RewriteContext<'_>,
156156
shape: Shape,
157157
position: MacroPosition,
158-
) -> Option<String> {
158+
) -> (Option<String>, Option<Delimiter>) {
159159
let should_skip = context
160160
.skip_context
161161
.macros
162162
.skip(context.snippet(mac.path.span));
163163
if should_skip {
164-
None
164+
(None, None)
165165
} else {
166166
let guard = context.enter_macro();
167167
let result = catch_unwind(AssertUnwindSafe(|| {
@@ -175,27 +175,29 @@ pub(crate) fn rewrite_macro(
175175
)
176176
}));
177177
match result {
178-
Err(..) | Ok(None) => {
178+
Err(..) | Ok((None, ..)) => {
179179
context.macro_rewrite_failure.replace(true);
180-
None
180+
(None, None)
181181
}
182182
Ok(rw) => rw,
183183
}
184184
}
185185
}
186186

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

@@ -213,7 +215,7 @@ fn rewrite_macro_inner(
213215
let ts = mac.args.tokens.clone();
214216
let has_comment = contains_comment(context.snippet(mac.span()));
215217
if ts.is_empty() && !has_comment {
216-
return match style {
218+
let rewrite = match style {
217219
Delimiter::Parenthesis if position == MacroPosition::Item => {
218220
Some(format!("{macro_name}();"))
219221
}
@@ -225,11 +227,12 @@ fn rewrite_macro_inner(
225227
Delimiter::Brace => Some(format!("{macro_name} {{}}")),
226228
_ => unreachable!(),
227229
};
230+
return (rewrite, Some(style));
228231
}
229232
// Format well-known macros which cannot be parsed as a valid AST.
230233
if macro_name == "lazy_static!" && !has_comment {
231234
if let success @ Some(..) = format_lazy_static(context, shape, ts.clone()) {
232-
return success;
235+
return (success, Some(Delimiter::Brace));
233236
}
234237
}
235238

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

252252
if !arg_vec.is_empty() && arg_vec.iter().all(MacroArg::is_item) {
253-
return rewrite_macro_with_items(
253+
let rewrite = rewrite_macro_with_items(
254254
context,
255255
&arg_vec,
256256
&macro_name,
@@ -260,9 +260,10 @@ fn rewrite_macro_inner(
260260
position,
261261
mac.span(),
262262
);
263+
return (rewrite, Some(style));
263264
}
264265

265-
match style {
266+
let rewrite = match style {
266267
Delimiter::Parenthesis => {
267268
// Handle special case: `vec!(expr; expr)`
268269
if vec_with_semi {
@@ -308,20 +309,22 @@ fn rewrite_macro_inner(
308309
force_trailing_comma = Some(SeparatorTactic::Vertical);
309310
};
310311
}
311-
let rewrite = rewrite_array(
312+
let Some(rewrite) = rewrite_array(
312313
&macro_name,
313314
arg_vec.iter(),
314315
mac.span(),
315316
context,
316317
shape,
317318
force_trailing_comma,
318319
Some(original_style),
319-
)?;
320+
) else {
321+
return (None, None);
322+
};
323+
320324
let comma = match position {
321325
MacroPosition::Item => ";",
322326
_ => "",
323327
};
324-
325328
Some(format!("{rewrite}{comma}"))
326329
}
327330
}
@@ -336,7 +339,8 @@ fn rewrite_macro_inner(
336339
}
337340
}
338341
_ => unreachable!(),
339-
}
342+
};
343+
return (rewrite, Some(style));
340344
}
341345

342346
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)