diff --git a/rustfmt-core/rustfmt-lib/src/expr.rs b/rustfmt-core/rustfmt-lib/src/expr.rs index c06a05201be..5ac6c26ef88 100644 --- a/rustfmt-core/rustfmt-lib/src/expr.rs +++ b/rustfmt-core/rustfmt-lib/src/expr.rs @@ -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 diff --git a/rustfmt-core/rustfmt-lib/tests/source/issue_3979.rs b/rustfmt-core/rustfmt-lib/tests/source/issue_3979.rs new file mode 100644 index 00000000000..478a9c95117 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/source/issue_3979.rs @@ -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); +} \ No newline at end of file diff --git a/rustfmt-core/rustfmt-lib/tests/target/issue_3979.rs b/rustfmt-core/rustfmt-lib/tests/target/issue_3979.rs new file mode 100644 index 00000000000..956250f5dad --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/issue_3979.rs @@ -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); +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/issue_4049.rs b/rustfmt-core/rustfmt-lib/tests/target/issue_4049.rs new file mode 100644 index 00000000000..fe025a0f649 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/issue_4049.rs @@ -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"); + } + } + } +}