Skip to content

Commit

Permalink
fix(fmt): apply multi statement block config to else block (foundry-r…
Browse files Browse the repository at this point in the history
…s#8661)

fix(fmt): apply multi statement block to else clauses
  • Loading branch information
grandizzy authored Aug 13, 2024
1 parent 1c71ab1 commit 224fe9c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@ impl<'a, W: Write> Formatter<'a, W> {
self.visit_if(*loc, cond, if_branch, else_branch, false)?;
} else {
let else_branch_is_single_line =
self.visit_stmt_as_block(else_branch, if_branch_is_single_line)?;
self.visit_stmt_as_block(else_branch, attempt_single_line)?;
if single_line_stmt_wide && !else_branch_is_single_line {
bail!(FormatterError::fmt())
}
Expand Down
14 changes: 14 additions & 0 deletions crates/fmt/testdata/Repros/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,17 @@ library MyLib {
);
// forgefmt: disable-end
}

contract IfElseTest {
function setNumber(uint256 newNumber) public {
number = newNumber;
if (newNumber = 1) {
number = 1;
} else if (newNumber = 2) {
// number = 2;
}
else {
newNumber = 3;
}
}
}
14 changes: 14 additions & 0 deletions crates/fmt/testdata/Repros/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,17 @@ library MyLib {
);
// forgefmt: disable-end
}

contract IfElseTest {
function setNumber(uint256 newNumber) public {
number = newNumber;
if (newNumber = 1) {
number = 1;
} else if (newNumber = 2) {
// number = 2;
}
else {
newNumber = 3;
}
}
}

0 comments on commit 224fe9c

Please sign in to comment.