Skip to content

Commit

Permalink
Fixed semicolon getting moved into comment (fixes rust-lang#4646)
Browse files Browse the repository at this point in the history
  • Loading branch information
vallentin committed Jan 17, 2021
1 parent a97fd77 commit 67b61b1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl<'a> FmtVisitor<'a> {
let context = self.get_context();

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

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

let (mut result, _) = rewrite_fn_base(
let (mut result, ends_with_comment, _) = rewrite_fn_base(
&context,
indent,
ident,
Expand All @@ -434,6 +434,11 @@ impl<'a> FmtVisitor<'a> {
FnBraceStyle::None,
)?;

// If `result` ends with a comment, then remember to add a newline
if ends_with_comment {
result.push_str(&indent.to_string_with_newline(context.config));
}

// Re-attach semicolon
result.push(';');

Expand Down Expand Up @@ -2296,7 +2301,7 @@ fn rewrite_fn_base(
fn_sig: &FnSig<'_>,
span: Span,
fn_brace_style: FnBraceStyle,
) -> Option<(String, bool)> {
) -> Option<(String, bool, bool)> {
let mut force_new_line_for_brace = false;

let where_clause = &fn_sig.generics.where_clause;
Expand Down Expand Up @@ -2601,10 +2606,11 @@ fn rewrite_fn_base(

result.push_str(&where_clause_str);

force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
let ends_with_comment = last_line_contains_single_line_comment(&result);
force_new_line_for_brace |= ends_with_comment;
force_new_line_for_brace |=
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
Some((result, force_new_line_for_brace))
Some((result, ends_with_comment, force_new_line_for_brace))
}

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

0 comments on commit 67b61b1

Please sign in to comment.