Skip to content

Commit a0236f2

Browse files
grandizzyrplusq
authored andcommitted
fix(fmt): do not panic when no named arg (foundry-rs#9114)
1 parent 982f5a7 commit a0236f2

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

crates/fmt/src/formatter.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,8 +2340,11 @@ impl<W: Write> Visitor for Formatter<'_, W> {
23402340
""
23412341
};
23422342
let closing_bracket = format!("{prefix}{}", "}");
2343-
let closing_bracket_loc = args.last().unwrap().loc.end();
2344-
write_chunk!(self, closing_bracket_loc, "{closing_bracket}")?;
2343+
if let Some(arg) = args.last() {
2344+
write_chunk!(self, arg.loc.end(), "{closing_bracket}")?;
2345+
} else {
2346+
write_chunk!(self, "{closing_bracket}")?;
2347+
}
23452348

23462349
Ok(())
23472350
}

crates/fmt/testdata/Repros/fmt.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,18 @@ contract IfElseTest {
144144
}
145145
}
146146
}
147+
148+
contract DbgFmtTest is Test {
149+
function test_argsList() public {
150+
uint256 result1 = internalNoArgs({});
151+
result2 = add({a: 1, b: 2});
152+
}
153+
154+
function add(uint256 a, uint256 b) internal pure returns (uint256) {
155+
return a + b;
156+
}
157+
158+
function internalNoArgs() internal pure returns (uint256) {
159+
return 0;
160+
}
161+
}

crates/fmt/testdata/Repros/original.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,18 @@ contract IfElseTest {
143143
}
144144
}
145145
}
146+
147+
contract DbgFmtTest is Test {
148+
function test_argsList() public {
149+
uint256 result1 = internalNoArgs({});
150+
result2 = add({a: 1, b: 2});
151+
}
152+
153+
function add(uint256 a, uint256 b) internal pure returns (uint256) {
154+
return a + b;
155+
}
156+
157+
function internalNoArgs() internal pure returns (uint256) {
158+
return 0;
159+
}
160+
}

0 commit comments

Comments
 (0)