Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forge): format new expressions #6408

Merged
merged 7 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,10 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
expr.visit(self)?;
stmt.visit(self)?;
}
Expression::New(_, expr) => {
write_chunk!(self, "new ")?;
self.visit_expr(expr.loc(), expr)?;
}
_ => self.visit_source(loc)?,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/fmt/testdata/ArrayExpressions/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ contract ArrayExpressions {
uint256[10] memory sample;

uint256 length = 10;
uint256[] memory sample2 = new uint[](length);
uint256[] memory sample2 = new uint256[](length);

uint256[] /* comment1 */ memory /* comment2 */ sample3; // comment3

Expand Down
3 changes: 2 additions & 1 deletion crates/fmt/testdata/ArrayExpressions/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ contract ArrayExpressions {
uint[10] memory sample;

uint256 length = 10;
uint[] memory sample2 = new uint[](length);
uint[] memory sample2 = new uint[](
length);

uint /* comment1 */ [] memory /* comment2 */ sample3 // comment3
;
Expand Down
5 changes: 5 additions & 0 deletions crates/fmt/testdata/FunctionCall/bracket-spacing.fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ contract FunctionCall {

function a(uint256 foo) {
foo;
MyContract c = new MyContract(address(0), hex"beef");
}

function b() {
a({ foo: 5 });
}

contract MyContract {
constructor(address arg, bytes memory data) { }
}
5 changes: 5 additions & 0 deletions crates/fmt/testdata/FunctionCall/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ contract FunctionCall {

function a(uint256 foo) {
foo;
MyContract c = new MyContract(address(0), hex"beef");
}

function b() {
a({foo: 5});
}

contract MyContract {
constructor(address arg, bytes memory data) {}
}
5 changes: 5 additions & 0 deletions crates/fmt/testdata/FunctionCall/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ contract FunctionCall {

function a(uint256 foo) {
foo;
MyContract c = new MyContract(address( 0),hex"beef");
}

function b() {
a( {foo: 5} );
}

contract MyContract {
constructor(address arg, bytes memory data) {}
}
4 changes: 2 additions & 2 deletions crates/fmt/testdata/FunctionType/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ library ArrayUtils {
pure
returns (uint256[] memory r)
{
r = new uint[](self.length);
r = new uint256[](self.length);
for (uint256 i = 0; i < self.length; i++) {
r[i] = f(self[i]);
}
Expand All @@ -23,7 +23,7 @@ library ArrayUtils {
}

function range(uint256 length) internal pure returns (uint256[] memory r) {
r = new uint[](length);
r = new uint256[](length);
for (uint256 i = 0; i < r.length; i++) {
r[i] = i;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fmt/testdata/FunctionType/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ library ArrayUtils {
uint[] memory r
)
{
r = new uint[](self.length);
r = new uint[]( self.length);
for (uint i = 0; i < self.length; i++) {
r[i] = f(self[i]);
}
Expand All @@ -23,7 +23,7 @@ library ArrayUtils {
}

function range(uint256 length) internal pure returns (uint[] memory r) {
r = new uint[](length);
r = new uint256[](length );
for (uint i = 0; i < r.length; i++) {
r[i] = i;
}
Expand Down
15 changes: 3 additions & 12 deletions testdata/cheats/Prank.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,7 @@ contract PrankTest is DSTest {
function testPrankConstructorSender(address sender) public {
vm.prank(sender);
ConstructorVictim victim = new ConstructorVictim(
sender,
"msg.sender was not set during prank",
tx.origin,
"tx.origin invariant failed"
sender, "msg.sender was not set during prank", tx.origin, "tx.origin invariant failed"
);

// Ensure we cleaned up correctly
Expand All @@ -290,10 +287,7 @@ contract PrankTest is DSTest {
// Perform the prank
vm.prank(sender, origin);
ConstructorVictim victim = new ConstructorVictim(
sender,
"msg.sender was not set during prank",
origin,
"tx.origin was not set during prank"
sender, "msg.sender was not set during prank", origin, "tx.origin was not set during prank"
);

// Ensure we cleaned up correctly
Expand Down Expand Up @@ -329,10 +323,7 @@ contract PrankTest is DSTest {
// Perform the prank
vm.startPrank(sender, origin);
ConstructorVictim victim = new ConstructorVictim(
sender,
"msg.sender was not set during prank",
origin,
"tx.origin was not set during prank"
sender, "msg.sender was not set during prank", origin, "tx.origin was not set during prank"
);
new ConstructorVictim(
sender,
Expand Down
6 changes: 3 additions & 3 deletions testdata/cheats/RecordAccountAccesses.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ contract RecordAccountAccessesTest is DSTest {
(succ,) = address(123469).call("hello world");
(succ,) = address(5678).call("");
// contract calls to self in constructor
SelfCaller caller = new SelfCaller{value: 2 ether}('hello2 world2');
SelfCaller caller = new SelfCaller{value: 2 ether}("hello2 world2");

Vm.AccountAccess[] memory called = cheats.stopAndReturnStateDiff();
assertEq(called.length, 6);
Expand Down Expand Up @@ -1011,8 +1011,8 @@ contract RecordAccountAccessesTest is DSTest {
function testSelfDestruct() public {
uint256 startingBalance = address(this).balance;
this.startRecordingFromLowerDepth();
address a = address(new SelfDestructor{value:1 ether}(address(this)));
address b = address(new SelfDestructor{value:1 ether}(address(bytes20("doesn't exist yet"))));
address a = address(new SelfDestructor{value: 1 ether}(address(this)));
address b = address(new SelfDestructor{value: 1 ether}(address(bytes20("doesn't exist yet"))));
Vm.AccountAccess[] memory called = cheats.stopAndReturnStateDiff();
assertEq(called.length, 5, "incorrect length");
assertEq(
Expand Down