Skip to content

Commit 58b9c65

Browse files
authored
fix(fmt): prevent double-ind in complex ternary expr (#12317)
1 parent 71b8886 commit 58b9c65

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

crates/fmt/src/state/sol.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,34 +1565,35 @@ impl<'ast> State<'_, 'ast> {
15651565
self.s.cbox(self.ind);
15661566
self.s.ibox(0);
15671567

1568-
let mut print_ternary_expr =
1569-
|span_lo, prefix: Option<&'static str>, expr: &'ast ast::Expr<'ast>| {
1570-
match prefix {
1571-
Some(prefix) => {
1572-
if self.peek_comment_before(span_lo).is_some() {
1573-
self.space();
1574-
}
1575-
self.print_comments(span_lo, CommentConfig::skip_ws());
1576-
self.end();
1577-
if !self.is_bol_or_only_ind() {
1578-
self.space();
1579-
}
1580-
self.s.ibox(0);
1581-
self.word(prefix);
1568+
let print_sub_expr = |this: &mut Self, span_lo, prefix, expr: &'ast ast::Expr<'ast>| {
1569+
match prefix {
1570+
Some(prefix) => {
1571+
if this.peek_comment_before(span_lo).is_some() {
1572+
this.space();
15821573
}
1583-
None => {
1584-
self.print_comments(expr.span.lo(), CommentConfig::skip_ws());
1574+
this.print_comments(span_lo, CommentConfig::skip_ws());
1575+
this.end();
1576+
if !this.is_bol_or_only_ind() {
1577+
this.space();
15851578
}
1586-
};
1587-
self.print_expr(expr);
1579+
this.s.ibox(0);
1580+
this.word(prefix);
1581+
}
1582+
None => {
1583+
this.print_comments(expr.span.lo(), CommentConfig::skip_ws());
1584+
}
15881585
};
1586+
this.print_expr(expr);
1587+
};
15891588

15901589
// conditional expression
1591-
print_ternary_expr(then.span.lo(), None, cond);
1590+
self.s.ibox(-self.ind);
1591+
print_sub_expr(self, then.span.lo(), None, cond);
1592+
self.end();
15921593
// then expression
1593-
print_ternary_expr(then.span.lo(), Some("? "), then);
1594+
print_sub_expr(self, then.span.lo(), Some("? "), then);
15941595
// else expression
1595-
print_ternary_expr(els.span.lo(), Some(": "), els);
1596+
print_sub_expr(self, els.span.lo(), Some(": "), els);
15961597

15971598
self.end();
15981599
self.neverbreak();

crates/fmt/testdata/IfStatement2/120.fmt.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ contract IfStatement {
1717
: Math.mulDiv(vaultUsdValue[i], 1e18, totalDepositedTvl, Math.Rounding.Floor);
1818
}
1919
}
20+
21+
// https://github.com/foundry-rs/foundry/issues/12315
22+
function repro_longComplexExpr() {
23+
vars.expectedSnapshotTime = withdrawAmount
24+
<= getDescaledAmount(flow.getSnapshotDebtScaled(streamId), flow.getTokenDecimals(streamId))
25+
? flow.getSnapshotTime(streamId)
26+
: getBlockTimestamp();
27+
}
2028
}

crates/fmt/testdata/IfStatement2/fmt.sol

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ contract IfStatement {
2626
);
2727
}
2828
}
29+
30+
// https://github.com/foundry-rs/foundry/issues/12315
31+
function repro_longComplexExpr() {
32+
vars.expectedSnapshotTime = withdrawAmount
33+
<= getDescaledAmount(
34+
flow.getSnapshotDebtScaled(streamId),
35+
flow.getTokenDecimals(streamId)
36+
)
37+
? flow.getSnapshotTime(streamId)
38+
: getBlockTimestamp();
39+
}
2940
}

crates/fmt/testdata/IfStatement2/original.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ contract IfStatement {
1818
: Math.mulDiv(vaultUsdValue[i], 1e18, totalDepositedTvl, Math.Rounding.Floor);
1919
}
2020
}
21+
22+
// https://github.com/foundry-rs/foundry/issues/12315
23+
function repro_longComplexExpr() {
24+
vars. expectedSnapshotTime = withdrawAmount
25+
<= getDescaledAmount(flow.getSnapshotDebtScaled (streamId), flow.getTokenDecimals(streamId))
26+
? flow.getSnapshotTime(streamId)
27+
: getBlockTimestamp ();
28+
}
2129
}

0 commit comments

Comments
 (0)