Skip to content

Commit d169164

Browse files
authored
fix(fmt): break try stmts in a fn header-like fashion (foundry-rs#12131)
1 parent 122d65a commit d169164

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

crates/fmt/src/state/sol.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,7 @@ impl<'ast> State<'_, 'ast> {
20632063
if let Some((first, other)) = clauses.split_first() {
20642064
// Print the 'try' clause
20652065
let ast::TryCatchClause { args, block, span: try_span, .. } = first;
2066+
self.cbox(0);
20662067
self.ibox(0);
20672068
self.print_word("try ");
20682069
self.print_comments(expr.span.lo(), CommentConfig::skip_ws());
@@ -2079,12 +2080,22 @@ impl<'ast> State<'_, 'ast> {
20792080

20802081
if !args.is_empty() {
20812082
self.print_word("returns ");
2082-
self.print_parameter_list(
2083+
self.print_word("(");
2084+
self.zerobreak();
2085+
self.end();
2086+
let span = args.span.with_hi(block.span.lo());
2087+
self.commasep(
20832088
args,
2084-
args.span.with_hi(block.span.lo()),
2085-
ListFormat::compact(),
2089+
span.lo(),
2090+
span.hi(),
2091+
|fmt, var| fmt.print_var(var, false),
2092+
get_span!(),
2093+
ListFormat::compact().no_delimiters(),
20862094
);
2095+
self.print_word(")");
20872096
self.nbsp();
2097+
} else {
2098+
self.end();
20882099
}
20892100
if block.is_empty() {
20902101
self.print_block(block, *try_span);

crates/fmt/testdata/TryStatement/fmt.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ contract TryStatement {
9393
revert();
9494
}
9595
}
96+
97+
function try_reallyLongCall() {
98+
try AggregatorV3Interface(oracle).latestRoundData() returns (
99+
uint80, int256 _price, uint256, uint256 _updatedAt, uint80
100+
) {
101+
return true;
102+
} catch {}
103+
}
96104
}

crates/fmt/testdata/TryStatement/original.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ contract TryStatement {
8585
revert();
8686
}
8787
}
88+
89+
function try_reallyLongCall() {
90+
try AggregatorV3Interface(oracle).latestRoundData() returns (uint80, int256 _price, uint256, uint256 _updatedAt, uint80) {
91+
return true;
92+
} catch {}
93+
}
8894
}

0 commit comments

Comments
 (0)