Skip to content

Commit 3e61326

Browse files
vallentincalebcartwright
authored andcommitted
Fixed semicolon getting moved into comment (fixes #4646)
1 parent 1e2b0b7 commit 3e61326

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/items.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'a> FmtVisitor<'a> {
302302
let context = self.get_context();
303303

304304
let mut fn_brace_style = newline_for_brace(self.config, &fn_sig.generics.where_clause);
305-
let (result, force_newline_brace) =
305+
let (result, _, force_newline_brace) =
306306
rewrite_fn_base(&context, indent, ident, fn_sig, span, fn_brace_style)?;
307307

308308
// 2 = ` {`
@@ -328,7 +328,7 @@ impl<'a> FmtVisitor<'a> {
328328
let span = mk_sp(span.lo(), span.hi() - BytePos(1));
329329
let context = self.get_context();
330330

331-
let (mut result, _) = rewrite_fn_base(
331+
let (mut result, ends_with_comment, _) = rewrite_fn_base(
332332
&context,
333333
indent,
334334
ident,
@@ -337,6 +337,11 @@ impl<'a> FmtVisitor<'a> {
337337
FnBraceStyle::None,
338338
)?;
339339

340+
// If `result` ends with a comment, then remember to add a newline
341+
if ends_with_comment {
342+
result.push_str(&indent.to_string_with_newline(context.config));
343+
}
344+
340345
// Re-attach semicolon
341346
result.push(';');
342347

@@ -2142,7 +2147,7 @@ fn rewrite_fn_base(
21422147
fn_sig: &FnSig<'_>,
21432148
span: Span,
21442149
fn_brace_style: FnBraceStyle,
2145-
) -> Option<(String, bool)> {
2150+
) -> Option<(String, bool, bool)> {
21462151
let mut force_new_line_for_brace = false;
21472152

21482153
let where_clause = &fn_sig.generics.where_clause;
@@ -2450,10 +2455,11 @@ fn rewrite_fn_base(
24502455

24512456
result.push_str(&where_clause_str);
24522457

2453-
force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
2458+
let ends_with_comment = last_line_contains_single_line_comment(&result);
2459+
force_new_line_for_brace |= ends_with_comment;
24542460
force_new_line_for_brace |=
24552461
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
2456-
Some((result, force_new_line_for_brace))
2462+
Some((result, ends_with_comment, force_new_line_for_brace))
24572463
}
24582464

24592465
/// Kind of spaces to put before `where`.
@@ -3137,7 +3143,7 @@ impl Rewrite for ast::ForeignItem {
31373143
span,
31383144
FnBraceStyle::None,
31393145
)
3140-
.map(|(s, _)| format!("{};", s)),
3146+
.map(|(s, _, _)| format!("{};", s)),
31413147
ast::ForeignItemKind::Static(ref ty, mutability, _) => {
31423148
// FIXME(#21): we're dropping potential comments in between the
31433149
// function kw here.

0 commit comments

Comments
 (0)