From c5510ef42dfd1cbbec82759a193d78b54ad19e51 Mon Sep 17 00:00:00 2001 From: John Chen Date: Sun, 11 Aug 2024 15:33:03 -0400 Subject: [PATCH 1/2] Let statement should rewrite rhs with comments after equal sign --- .gitignore | 3 +++ src/items.rs | 17 +++++++++++++---- tests/source/issue-6246.rs | 20 ++++++++++++++++++++ tests/target/issue-6246.rs | 12 ++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/source/issue-6246.rs create mode 100644 tests/target/issue-6246.rs diff --git a/.gitignore b/.gitignore index 71cf88f79e6..50e734390d7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ tests/cargo-fmt/**/target .idea/ .vscode/ *~ + +# Git +.git/ diff --git a/src/items.rs b/src/items.rs index 35591df0fd8..de9002d3703 100644 --- a/src/items.rs +++ b/src/items.rs @@ -131,14 +131,23 @@ impl Rewrite for ast::Local { .sub_width(1) .max_width_error(shape.width, self.span())?; - result = rewrite_assign_rhs( + // should always find a comment span if init exists + let comment_span = context + .snippet_provider + .opt_span_after(self.span, "=") + .map(|op_lo| mk_sp(op_lo, init.span.lo())) + .unwrap(); + + result = rewrite_assign_rhs_with_comments( context, result, init, - &RhsAssignKind::Expr(&init.kind, init.span), nested_shape, - ) - .max_width_error(shape.width, self.span())?; + &RhsAssignKind::Expr(&init.kind, init.span), + RhsTactics::Default, + comment_span, + true, + )?; if let Some(block) = else_block { let else_kw_span = init.span.between(block.span); diff --git a/tests/source/issue-6246.rs b/tests/source/issue-6246.rs new file mode 100644 index 00000000000..51ac0eb8368 --- /dev/null +++ b/tests/source/issue-6246.rs @@ -0,0 +1,20 @@ +fn main() { + let foo = + // 114514 + if true { + 1919 + } else { + 810 + }; +} + +// Test a let statement without equal sign +fn main() { + let mut foo ; + // 114514 + foo = if true { + 1919 + } else { + 810 + }; +} diff --git a/tests/target/issue-6246.rs b/tests/target/issue-6246.rs new file mode 100644 index 00000000000..89889e56e58 --- /dev/null +++ b/tests/target/issue-6246.rs @@ -0,0 +1,12 @@ +fn main() { + let foo = + // 114514 + if true { 1919 } else { 810 }; +} + +// Test a let statement without equal sign +fn main() { + let mut foo; + // 114514 + foo = if true { 1919 } else { 810 }; +} From 62a326560db4eb3f26975990da43dccef2191cb1 Mon Sep 17 00:00:00 2001 From: John Chen Date: Thu, 15 Aug 2024 21:56:45 -0400 Subject: [PATCH 2/2] Added tests --- tests/source/issue-6246.rs | 22 ++++++++++++++++++++++ tests/target/issue-6246.rs | 14 ++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tests/source/issue-6246.rs b/tests/source/issue-6246.rs index 51ac0eb8368..3caf0768f83 100644 --- a/tests/source/issue-6246.rs +++ b/tests/source/issue-6246.rs @@ -8,6 +8,28 @@ fn main() { }; } +fn main() { + let foo = + // line 1 + // line 2 + if true { + 1919 + } else { + 810 + }; +} + +fn main() { + let foo = + /* line 1 + line 2 */ + if true { + 1919 + } else { + 810 + }; +} + // Test a let statement without equal sign fn main() { let mut foo ; diff --git a/tests/target/issue-6246.rs b/tests/target/issue-6246.rs index 89889e56e58..2dfefc9a8d3 100644 --- a/tests/target/issue-6246.rs +++ b/tests/target/issue-6246.rs @@ -4,6 +4,20 @@ fn main() { if true { 1919 } else { 810 }; } +fn main() { + let foo = + // line 1 + // line 2 + if true { 1919 } else { 810 }; +} + +fn main() { + let foo = + /* line 1 + line 2 */ + if true { 1919 } else { 810 }; +} + // Test a let statement without equal sign fn main() { let mut foo;