Created
February 4, 2026 14:12
-
-
Save gakonst/40cde0a940c0d567e225731d55848606 to your computer and use it in GitHub Desktop.
Fix for foundry-rs/foundry#13315 - forge fmt indentation bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From c55a2810408df87b48bf16e442639f349e25f40e Mon Sep 17 00:00:00 2001 | |
| From: Georgios Konstantopoulos <me@gakonst.com> | |
| Date: Wed, 4 Feb 2026 14:11:00 +0000 | |
| Subject: [PATCH] fix(fmt): correct indentation for empty contract body with | |
| comment | |
| Fixes #13315 | |
| When a contract body is empty but contains a comment, the closing brace | |
| was incorrectly indented. This was caused by not applying the offset | |
| adjustment before closing the indentation box. | |
| The fix replaces the zerobreak() with offset(-self.ind) to properly | |
| deindent the closing brace, matching the behavior of contracts with | |
| non-empty bodies. | |
| Amp-Thread-ID: https://ampcode.com/threads/T-019c28f2-dfd5-72db-bd22-5e9fdff097a8 | |
| Co-authored-by: Amp <amp@ampcode.com> | |
| --- | |
| crates/fmt/src/state/sol.rs | 2 +- | |
| crates/fmt/testdata/Repros/fmt.sol | 9 +++++++++ | |
| crates/fmt/testdata/Repros/original.sol | 11 +++++++++++ | |
| crates/fmt/testdata/Repros/sorted.fmt.sol | 9 +++++++++ | |
| crates/fmt/testdata/Repros/tab.fmt.sol | 9 +++++++++ | |
| 5 files changed, 39 insertions(+), 1 deletion(-) | |
| diff --git a/crates/fmt/src/state/sol.rs b/crates/fmt/src/state/sol.rs | |
| index fd0e5a9..59ae5f6 100644 | |
| --- a/crates/fmt/src/state/sol.rs | |
| +++ b/crates/fmt/src/state/sol.rs | |
| @@ -407,7 +407,7 @@ fn print_contract(&mut self, c: &'ast ast::ItemContract<'ast>, span: Span) { | |
| self.block_depth -= 1; | |
| } else { | |
| if self.print_comments(span.hi(), CommentConfig::skip_ws()).is_some() { | |
| - self.zerobreak(); | |
| + self.s.offset(-self.ind); | |
| } else if self.config.bracket_spacing { | |
| self.nbsp(); | |
| }; | |
| diff --git a/crates/fmt/testdata/Repros/fmt.sol b/crates/fmt/testdata/Repros/fmt.sol | |
| index 771c86e..e0e56c0 100644 | |
| --- a/crates/fmt/testdata/Repros/fmt.sol | |
| +++ b/crates/fmt/testdata/Repros/fmt.sol | |
| @@ -427,3 +427,12 @@ contract ChainedStructCall { | |
| }).pack(); | |
| } | |
| } | |
| + | |
| +// https://github.com/foundry-rs/foundry/issues/13315 | |
| +contract EmptyWithComment { | |
| + // comment only, no body items | |
| +} | |
| + | |
| +contract EmptyWithBlankLine { | |
| + // comment followed by blank line | |
| +} | |
| diff --git a/crates/fmt/testdata/Repros/original.sol b/crates/fmt/testdata/Repros/original.sol | |
| index 0a37ed6..bbad4ef 100644 | |
| --- a/crates/fmt/testdata/Repros/original.sol | |
| +++ b/crates/fmt/testdata/Repros/original.sol | |
| @@ -424,3 +424,14 @@ contract ChainedStructCall { | |
| }).pack(); | |
| } | |
| } | |
| + | |
| +// https://github.com/foundry-rs/foundry/issues/13315 | |
| +contract EmptyWithComment { | |
| + // comment only, no body items | |
| + | |
| +} | |
| + | |
| +contract EmptyWithBlankLine { | |
| + // comment followed by blank line | |
| + | |
| +} | |
| diff --git a/crates/fmt/testdata/Repros/sorted.fmt.sol b/crates/fmt/testdata/Repros/sorted.fmt.sol | |
| index 0d56601..8a68629 100644 | |
| --- a/crates/fmt/testdata/Repros/sorted.fmt.sol | |
| +++ b/crates/fmt/testdata/Repros/sorted.fmt.sol | |
| @@ -428,3 +428,12 @@ contract ChainedStructCall { | |
| }).pack(); | |
| } | |
| } | |
| + | |
| +// https://github.com/foundry-rs/foundry/issues/13315 | |
| +contract EmptyWithComment { | |
| + // comment only, no body items | |
| +} | |
| + | |
| +contract EmptyWithBlankLine { | |
| + // comment followed by blank line | |
| +} | |
| diff --git a/crates/fmt/testdata/Repros/tab.fmt.sol b/crates/fmt/testdata/Repros/tab.fmt.sol | |
| index b423e45..491c678 100644 | |
| --- a/crates/fmt/testdata/Repros/tab.fmt.sol | |
| +++ b/crates/fmt/testdata/Repros/tab.fmt.sol | |
| @@ -428,3 +428,12 @@ contract ChainedStructCall { | |
| }).pack(); | |
| } | |
| } | |
| + | |
| +// https://github.com/foundry-rs/foundry/issues/13315 | |
| +contract EmptyWithComment { | |
| + // comment only, no body items | |
| +} | |
| + | |
| +contract EmptyWithBlankLine { | |
| + // comment followed by blank line | |
| +} | |
| -- | |
| 2.43.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment