Skip to content

fix comment handling in control flows #4055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 45 additions & 25 deletions rustfmt-core/rustfmt-lib/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,33 +831,53 @@ impl<'a> ControlFlow<'a> {
let comments_lo = context
.snippet_provider
.span_after(self.span, self.connector.trim());
let missing_comments = if let Some(comment) =
rewrite_missing_comment(mk_sp(comments_lo, expr.span.lo()), cond_shape, context)
{
if !self.connector.is_empty() && !comment.is_empty() {
if comment_style(&comment, false).is_line_comment() || comment.contains('\n') {
let newline = &pat_shape
.indent
.block_indent(context.config)
.to_string_with_newline(context.config);
// An extra space is added when the lhs and rhs are joined
// so we need to remove one space from the end to ensure
// the comment and rhs are aligned.
let mut suffix = newline.as_ref().to_string();
if !suffix.is_empty() {
suffix.truncate(suffix.len() - 1);
}
format!("{}{}{}", newline, comment, suffix)
} else {
format!(" {}", comment)
}
} else {
comment
let comments_span = mk_sp(comments_lo, expr.span.lo());

let missing_comments = match rewrite_missing_comment(
comments_span,
cond_shape,
context,
) {
None => "".to_owned(),
Some(comment) if self.connector.is_empty() || comment.is_empty() => comment,
// Handle same-line block comments:
// if let Some(foo) = /*bar*/ baz { ... }
// if let Some(ref /*def*/ mut /*abc*/ state)...
Some(comment)
if !comment_style(&comment, false).is_line_comment()
&& !comment.contains('\n') =>
{
format!(" {}", comment)
}
// Handle sequence of multiple inline comments:
// if let Some(n) =
// // this is a test comment
// // with another
// foo { .... }
Some(_) => {
let newline = &cond_shape
.indent
.block_indent(context.config)
.to_string_with_newline(context.config);
let shape = pat_shape.block_indent(context.config.tab_spaces());
let comment = format!(
"{}{}",
newline,
rewrite_missing_comment(comments_span, shape, context)?,
);
let lhs = format!("{}{}{}{}", matcher, pat_string, self.connector, comment);
let orig_rhs = Some(format!("{}{}", newline, expr.rewrite(context, shape)?));
let rhs = choose_rhs(
context,
expr,
cond_shape,
orig_rhs,
RhsTactics::Default,
true,
)?;
return Some(format!("{}{}", lhs, rhs));
}
} else {
"".to_owned()
};

let result = format!(
"{}{}{}{}",
matcher, pat_string, self.connector, missing_comments
Expand Down
17 changes: 17 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue_3979.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://github.com/rust-lang/rustfmt/issues/3979

fn main() {
let o_num = Some(123);

let x = if let Some(n) =
// this is a test comment
// to see if rustfmt will break
// after using the lateset version
o_num {
n * 2
} else {
0
};

println!("Number: {}", x);
}
18 changes: 18 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue_3979.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://github.com/rust-lang/rustfmt/issues/3979

fn main() {
let o_num = Some(123);

let x = if let Some(n) =
// this is a test comment
// to see if rustfmt will break
// after using the lateset version
o_num
{
n * 2
} else {
0
};

println!("Number: {}", x);
}
26 changes: 26 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue_4049.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// rustfmt-max_width: 110
// rustfmt-use_small_heuristics: Max
// rustfmt-hard_tabs: true
// rustfmt-use_field_init_shorthand: true
// rustfmt-overflow_delimited_expr: true

// https://github.com/rust-lang/rustfmt/issues/4049
fn foo() {
{
{
if let Some(MpcEv::PlayDrum(pitch, vel)) =
// self.mpc.handle_input(e, /*btn_ctrl_down,*/ tx_launch_to_daw, state_view)
self.mpc.handle_input(e, &mut MyBorrowedState { tx_launch_to_daw, state_view })
{
println!("bar");
}

if let Some(e) =
// self.note_input.handle_input(e, /*btn_ctrl_down,*/ tx_launch_to_daw, state_view)
self.note_input.handle_input(e, &mut MyBorrowedState { tx_launch_to_daw, state_view })
{
println!("baz");
}
}
}
}