Skip to content

Commit 2c5926b

Browse files
committed
Let statement should rewrite rhs with comments after equal sign
1 parent fbe0424 commit 2c5926b

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,23 @@ impl Rewrite for ast::Local {
131131
.sub_width(1)
132132
.max_width_error(shape.width, self.span())?;
133133

134-
result = rewrite_assign_rhs(
134+
// should always find a comment span
135+
let comment_span = context
136+
.snippet_provider
137+
.opt_span_after(self.span, "=")
138+
.map(|op_lo| mk_sp(op_lo, init.span.lo()))
139+
.unwrap();
140+
141+
result = rewrite_assign_rhs_with_comments(
135142
context,
136143
result,
137144
init,
138-
&RhsAssignKind::Expr(&init.kind, init.span),
139145
nested_shape,
140-
)
141-
.max_width_error(shape.width, self.span())?;
146+
&RhsAssignKind::Expr(&init.kind, init.span),
147+
RhsTactics::Default,
148+
comment_span,
149+
true,
150+
)?;
142151

143152
if let Some(block) = else_block {
144153
let else_kw_span = init.span.between(block.span);

tests/source/issue-6246.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn main() {
2+
let foo =
3+
// 114514
4+
if true {
5+
1919
6+
} else {
7+
810
8+
};
9+
}
10+
11+
// Test a let statement without equal sign
12+
fn main() {
13+
let mut foo ;
14+
// 114514
15+
foo = if true {
16+
1919
17+
} else {
18+
810
19+
};
20+
}

tests/target/issue-6246.rs

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

0 commit comments

Comments
 (0)