Skip to content

Commit de90f56

Browse files
rchaser53topecongiro
authored andcommitted
handle None case to remove whitespace before lhs (#3925)
1 parent a35c82b commit de90f56

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/items.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,11 +1813,32 @@ fn rewrite_static(
18131813
let remaining_width = context.budget(offset.block_indent + 1);
18141814
rewrite_assign_rhs(
18151815
context,
1816-
lhs,
1816+
&lhs,
18171817
&**expr,
18181818
Shape::legacy(remaining_width, offset.block_only()),
18191819
)
18201820
.and_then(|res| recover_comment_removed(res, static_parts.span, context))
1821+
.or_else(|| {
1822+
let nested_indent = offset.block_indent(context.config);
1823+
let ty_span_hi = static_parts.ty.span.hi();
1824+
let rhs_span_lo = expr.span.lo();
1825+
let eq_pos_offset = context
1826+
.snippet(mk_sp(ty_span_hi, rhs_span_lo))
1827+
.find_uncommented("=")?;
1828+
let str_from_eq_to_rhs = context.snippet(mk_sp(
1829+
ty_span_hi + BytePos(eq_pos_offset as u32),
1830+
rhs_span_lo,
1831+
));
1832+
let rhs = context.snippet(expr.span);
1833+
let str_between_lhs_and_hrs = if str_from_eq_to_rhs.find_uncommented("\n").is_some() {
1834+
nested_indent
1835+
.to_string_with_newline(context.config)
1836+
.to_string()
1837+
} else {
1838+
String::from(" ")
1839+
};
1840+
Some(format!("{}{}{}", lhs, str_between_lhs_and_hrs, rhs.trim()))
1841+
})
18211842
.map(|s| if s.ends_with(';') { s } else { s + ";" })
18221843
} else {
18231844
Some(format!("{}{};", prefix, ty_str))

tests/source/issue-3923.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const TEST: &str =
2+
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
3+
4+
const TEST2: &str = "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222";

tests/target/issue-3923.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const TEST: &str =
2+
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
3+
4+
const TEST2: &str = "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222";

0 commit comments

Comments
 (0)