Skip to content

Commit 41a9a72

Browse files
committed
Let statement should rewrite rhs with comments after equal sign
1 parent 40f5075 commit 41a9a72

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ tests/cargo-fmt/**/target
2323
.idea/
2424
.vscode/
2525
*~
26+
27+
# Git
28+
.git/

src/items.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,32 @@ impl Rewrite for ast::Local {
131131
.sub_width(1)
132132
.max_width_error(shape.width, self.span())?;
133133

134-
result = rewrite_assign_rhs(
135-
context,
136-
result,
137-
init,
138-
&RhsAssignKind::Expr(&init.kind, init.span),
139-
nested_shape,
140-
)
141-
.max_width_error(shape.width, self.span())?;
134+
let maybe_comment_span = context
135+
.snippet_provider
136+
.opt_span_after(self.span, "=")
137+
.map(|op_lo| mk_sp(op_lo, init.span.lo()));
138+
139+
if maybe_comment_span.map_or(true, |span| context.snippet(span).trim().len() == 0) {
140+
result = rewrite_assign_rhs(
141+
context,
142+
result,
143+
init,
144+
&RhsAssignKind::Expr(&init.kind, init.span),
145+
nested_shape,
146+
)
147+
.max_width_error(shape.width, self.span())?;
148+
} else {
149+
result = rewrite_assign_rhs_with_comments(
150+
context,
151+
result,
152+
&*init,
153+
nested_shape,
154+
&RhsAssignKind::Expr(&init.kind, init.span),
155+
RhsTactics::Default,
156+
maybe_comment_span.unwrap(),
157+
true,
158+
)?;
159+
}
142160

143161
if let Some(block) = else_block {
144162
let else_kw_span = init.span.between(block.span);

tests/source/issue-6246.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let foo =
3+
// 114514
4+
if true {
5+
1919
6+
} else {
7+
810
8+
};
9+
}

tests/target/issue-6246.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let foo =
3+
// 114514
4+
if true { 1919 } else { 810 };
5+
}

0 commit comments

Comments
 (0)