Skip to content

Commit 833c6f5

Browse files
vallentindavidBar-On
authored andcommitted
Fixed semicolon getting moved into comment (fixes rust-lang#4646)
1 parent 387020a commit 833c6f5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/formatting/items.rs

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

401401
let mut fn_brace_style = newline_for_brace(self.config, &fn_sig.generics.where_clause);
402-
let (result, force_newline_brace) =
402+
let (result, _, force_newline_brace) =
403403
rewrite_fn_base(&context, indent, ident, fn_sig, span, fn_brace_style)?;
404404

405405
// 2 = ` {`
@@ -425,7 +425,7 @@ impl<'a> FmtVisitor<'a> {
425425
let span = mk_sp(span.lo(), span.hi() - BytePos(1));
426426
let context = self.get_context();
427427

428-
let (mut result, _) = rewrite_fn_base(
428+
let (mut result, ends_with_comment, _) = rewrite_fn_base(
429429
&context,
430430
indent,
431431
ident,
@@ -434,6 +434,11 @@ impl<'a> FmtVisitor<'a> {
434434
FnBraceStyle::None,
435435
)?;
436436

437+
// If `result` ends with a comment, then remember to add a newline
438+
if ends_with_comment {
439+
result.push_str(&indent.to_string_with_newline(context.config));
440+
}
441+
437442
// Re-attach semicolon
438443
result.push(';');
439444

@@ -2296,7 +2301,7 @@ fn rewrite_fn_base(
22962301
fn_sig: &FnSig<'_>,
22972302
span: Span,
22982303
fn_brace_style: FnBraceStyle,
2299-
) -> Option<(String, bool)> {
2304+
) -> Option<(String, bool, bool)> {
23002305
let mut force_new_line_for_brace = false;
23012306

23022307
let where_clause = &fn_sig.generics.where_clause;
@@ -2601,10 +2606,11 @@ fn rewrite_fn_base(
26012606

26022607
result.push_str(&where_clause_str);
26032608

2604-
force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
2609+
let ends_with_comment = last_line_contains_single_line_comment(&result);
2610+
force_new_line_for_brace |= ends_with_comment;
26052611
force_new_line_for_brace |=
26062612
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
2607-
Some((result, force_new_line_for_brace))
2613+
Some((result, ends_with_comment, force_new_line_for_brace))
26082614
}
26092615

26102616
/// Kind of spaces to put before `where`.
@@ -3274,7 +3280,7 @@ impl Rewrite for ast::ForeignItem {
32743280
span,
32753281
FnBraceStyle::None,
32763282
)
3277-
.map(|(s, _)| format!("{};", s)),
3283+
.map(|(s, _, _)| format!("{};", s)),
32783284
ast::ForeignItemKind::Static(ref ty, mutability, _) => {
32793285
// FIXME(#21): we're dropping potential comments in between the
32803286
// function kw here.

0 commit comments

Comments
 (0)