Skip to content

Commit 19abe8d

Browse files
fix: comment handling in control flows (#4055)
1 parent 71a983f commit 19abe8d

File tree

4 files changed

+106
-25
lines changed

4 files changed

+106
-25
lines changed

rustfmt-core/rustfmt-lib/src/expr.rs

+45-25
Original file line numberDiff line numberDiff line change
@@ -831,33 +831,53 @@ impl<'a> ControlFlow<'a> {
831831
let comments_lo = context
832832
.snippet_provider
833833
.span_after(self.span, self.connector.trim());
834-
let missing_comments = if let Some(comment) =
835-
rewrite_missing_comment(mk_sp(comments_lo, expr.span.lo()), cond_shape, context)
836-
{
837-
if !self.connector.is_empty() && !comment.is_empty() {
838-
if comment_style(&comment, false).is_line_comment() || comment.contains('\n') {
839-
let newline = &pat_shape
840-
.indent
841-
.block_indent(context.config)
842-
.to_string_with_newline(context.config);
843-
// An extra space is added when the lhs and rhs are joined
844-
// so we need to remove one space from the end to ensure
845-
// the comment and rhs are aligned.
846-
let mut suffix = newline.as_ref().to_string();
847-
if !suffix.is_empty() {
848-
suffix.truncate(suffix.len() - 1);
849-
}
850-
format!("{}{}{}", newline, comment, suffix)
851-
} else {
852-
format!(" {}", comment)
853-
}
854-
} else {
855-
comment
834+
let comments_span = mk_sp(comments_lo, expr.span.lo());
835+
836+
let missing_comments = match rewrite_missing_comment(
837+
comments_span,
838+
cond_shape,
839+
context,
840+
) {
841+
None => "".to_owned(),
842+
Some(comment) if self.connector.is_empty() || comment.is_empty() => comment,
843+
// Handle same-line block comments:
844+
// if let Some(foo) = /*bar*/ baz { ... }
845+
// if let Some(ref /*def*/ mut /*abc*/ state)...
846+
Some(comment)
847+
if !comment_style(&comment, false).is_line_comment()
848+
&& !comment.contains('\n') =>
849+
{
850+
format!(" {}", comment)
851+
}
852+
// Handle sequence of multiple inline comments:
853+
// if let Some(n) =
854+
// // this is a test comment
855+
// // with another
856+
// foo { .... }
857+
Some(_) => {
858+
let newline = &cond_shape
859+
.indent
860+
.block_indent(context.config)
861+
.to_string_with_newline(context.config);
862+
let shape = pat_shape.block_indent(context.config.tab_spaces());
863+
let comment = format!(
864+
"{}{}",
865+
newline,
866+
rewrite_missing_comment(comments_span, shape, context)?,
867+
);
868+
let lhs = format!("{}{}{}{}", matcher, pat_string, self.connector, comment);
869+
let orig_rhs = Some(format!("{}{}", newline, expr.rewrite(context, shape)?));
870+
let rhs = choose_rhs(
871+
context,
872+
expr,
873+
cond_shape,
874+
orig_rhs,
875+
RhsTactics::Default,
876+
true,
877+
)?;
878+
return Some(format!("{}{}", lhs, rhs));
856879
}
857-
} else {
858-
"".to_owned()
859880
};
860-
861881
let result = format!(
862882
"{}{}{}{}",
863883
matcher, pat_string, self.connector, missing_comments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://github.com/rust-lang/rustfmt/issues/3979
2+
3+
fn main() {
4+
let o_num = Some(123);
5+
6+
let x = if let Some(n) =
7+
// this is a test comment
8+
// to see if rustfmt will break
9+
// after using the lateset version
10+
o_num {
11+
n * 2
12+
} else {
13+
0
14+
};
15+
16+
println!("Number: {}", x);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://github.com/rust-lang/rustfmt/issues/3979
2+
3+
fn main() {
4+
let o_num = Some(123);
5+
6+
let x = if let Some(n) =
7+
// this is a test comment
8+
// to see if rustfmt will break
9+
// after using the lateset version
10+
o_num
11+
{
12+
n * 2
13+
} else {
14+
0
15+
};
16+
17+
println!("Number: {}", x);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// rustfmt-max_width: 110
2+
// rustfmt-use_small_heuristics: Max
3+
// rustfmt-hard_tabs: true
4+
// rustfmt-use_field_init_shorthand: true
5+
// rustfmt-overflow_delimited_expr: true
6+
7+
// https://github.com/rust-lang/rustfmt/issues/4049
8+
fn foo() {
9+
{
10+
{
11+
if let Some(MpcEv::PlayDrum(pitch, vel)) =
12+
// self.mpc.handle_input(e, /*btn_ctrl_down,*/ tx_launch_to_daw, state_view)
13+
self.mpc.handle_input(e, &mut MyBorrowedState { tx_launch_to_daw, state_view })
14+
{
15+
println!("bar");
16+
}
17+
18+
if let Some(e) =
19+
// self.note_input.handle_input(e, /*btn_ctrl_down,*/ tx_launch_to_daw, state_view)
20+
self.note_input.handle_input(e, &mut MyBorrowedState { tx_launch_to_daw, state_view })
21+
{
22+
println!("baz");
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)