Skip to content

Commit 83f249d

Browse files
author
Alexander Glusker
committed
Working with return values instead of state
1 parent 99681ec commit 83f249d

File tree

8 files changed

+156
-138
lines changed

8 files changed

+156
-138
lines changed

src/expr.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,15 @@ 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(|| {
229-
wrap_str(
230-
context.snippet(expr.span).to_owned(),
231-
context.config.max_width(),
232-
shape,
233-
)
234-
})
228+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
229+
.0
230+
.or_else(|| {
231+
wrap_str(
232+
context.snippet(expr.span).to_owned(),
233+
context.config.max_width(),
234+
shape,
235+
)
236+
})
235237
}
236238
ast::ExprKind::Ret(None) => Some("return".to_owned()),
237239
ast::ExprKind::Ret(Some(ref expr)) => {

src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3374,7 +3374,7 @@ 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+
rewrite_macro(mac, None, context, shape, MacroPosition::Item).0
33783378
}
33793379
}?;
33803380

src/macros.rs

Lines changed: 131 additions & 104 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
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,26 +216,37 @@ 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 {
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+
);
229238
}
230239
// Format well-known macros which cannot be parsed as a valid AST.
231240
if macro_name == "lazy_static!" && !has_comment {
232241
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+
);
237250
}
238251
}
239252

@@ -244,103 +257,117 @@ fn rewrite_macro_inner(
244257
} = match parse_macro_args(context, ts, style, is_forced_bracket) {
245258
Some(args) => args,
246259
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,
252263
);
253264
}
254265
};
255266

256267
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+
},
266284
);
267285
}
268286

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 {
285324
Some(SeparatorTactic::Always)
286325
} else {
287326
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);
313327
};
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+
})
314351
}
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}"))
330352
}
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+
}
340362
}
341-
}
342-
_ => unreachable!(),
343-
}
363+
_ => unreachable!(),
364+
},
365+
if original_style != style {
366+
Some(style)
367+
} else {
368+
None
369+
},
370+
)
344371
}
345372

346373
fn handle_vec_semi(

src/missed_spans.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ impl<'a> FmtVisitor<'a> {
5050
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
5151
}
5252

53-
pub(crate) fn format_missing_ignore_semicolon(&mut self, end: BytePos) {
54-
let missing_snippet = self.snippet(mk_sp(self.last_pos, end));
55-
if missing_snippet.trim() == ";" {
56-
self.last_pos = end;
57-
return;
58-
}
59-
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
60-
}
61-
6253
pub(crate) fn format_missing_with_indent(&mut self, end: BytePos) {
6354
self.format_missing_indent(end, true)
6455
}

src/patterns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl Rewrite for Pat {
269269
shape,
270270
),
271271
PatKind::MacCall(ref mac) => {
272-
rewrite_macro(mac, None, context, shape, MacroPosition::Pat)
272+
rewrite_macro(mac, None, context, shape, MacroPosition::Pat).0
273273
}
274274
PatKind::Paren(ref pat) => pat
275275
.rewrite(context, shape.offset_left(1)?.sub_width(1)?)

src/rewrite.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub(crate) struct RewriteContext<'a> {
4343
pub(crate) report: FormatReport,
4444
pub(crate) skip_context: SkipContext,
4545
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
46-
pub(crate) lazy_static_success: Cell<bool>,
4746
}
4847

4948
pub(crate) struct InsideMacroGuard {

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ 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+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).0
833833
}
834834
ast::TyKind::ImplicitSelf => Some(String::from("")),
835835
ast::TyKind::ImplTrait(_, ref it) => {

0 commit comments

Comments
 (0)