Skip to content

Commit

Permalink
fix(fmt): do not panic when no named arg (foundry-rs#9114)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored and rplusq committed Nov 29, 2024
1 parent 982f5a7 commit a0236f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2340,8 +2340,11 @@ impl<W: Write> Visitor for Formatter<'_, W> {
""
};
let closing_bracket = format!("{prefix}{}", "}");
let closing_bracket_loc = args.last().unwrap().loc.end();
write_chunk!(self, closing_bracket_loc, "{closing_bracket}")?;
if let Some(arg) = args.last() {
write_chunk!(self, arg.loc.end(), "{closing_bracket}")?;
} else {
write_chunk!(self, "{closing_bracket}")?;
}

Ok(())
}
Expand Down
15 changes: 15 additions & 0 deletions crates/fmt/testdata/Repros/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,18 @@ contract IfElseTest {
}
}
}

contract DbgFmtTest is Test {
function test_argsList() public {
uint256 result1 = internalNoArgs({});
result2 = add({a: 1, b: 2});
}

function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}

function internalNoArgs() internal pure returns (uint256) {
return 0;
}
}
15 changes: 15 additions & 0 deletions crates/fmt/testdata/Repros/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,18 @@ contract IfElseTest {
}
}
}

contract DbgFmtTest is Test {
function test_argsList() public {
uint256 result1 = internalNoArgs({});
result2 = add({a: 1, b: 2});
}

function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}

function internalNoArgs() internal pure returns (uint256) {
return 0;
}
}

0 comments on commit a0236f2

Please sign in to comment.