[clang] [clang-format] Fix short functions with nested braced-init (PR #213550)

Gauarv Chaudhary via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 2 08:21:13 PDT 2026


https://github.com/ANAMASGARD updated https://github.com/llvm/llvm-project/pull/213550

>From 90d847e03f0b61b759b870d79f5a9a4ae0db31ee Mon Sep 17 00:00:00 2001
From: Gaurav Chaudhary <chaudharygaurav2004 at gmail.com>
Date: Sun, 2 Aug 2026 20:47:16 +0530
Subject: [PATCH] [clang-format] Fix nested braced-init short functions

Signed-off-by: Gaurav Chaudhary <chaudharygaurav2004 at gmail.com>
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 12 ++++++++----
 clang/unittests/Format/FormatTest.cpp       |  1 +
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 7ea424349d923..7afc7a46dd1c0 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -983,13 +983,17 @@ class LineJoiner {
         if (!nextTwoLinesFitInto(I, Limit))
           return 0;
 
-        // Second, check that the next line does not contain any braces - if it
-        // does, readability declines when putting it into a single line.
+        // Second, check that the next line does not contain non-braced-init
+        // braces - if it does, readability declines when putting it into a
+        // single line.
         if (I[1]->Last->is(TT_LineComment))
           return 0;
         do {
-          if (Tok->isOneOf(tok::l_brace, tok::r_brace) &&
-              Tok->isNot(BK_BracedInit)) {
+          if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit))
+            return 0;
+          if (Tok->is(tok::r_brace) &&
+              (!Tok->MatchingParen ||
+               Tok->MatchingParen->isNot(BK_BracedInit))) {
             return 0;
           }
           Tok = Tok->Next;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b72a683ac1fff..6f604167f785c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15368,6 +15368,7 @@ TEST_F(FormatTest, CustomShortFunctionOptions) {
   // All functions should be on a single line if they fit
   verifyFormat("int f() { return 42; }", CustomAll);
   verifyFormat("int g() { return f() + h(); }", CustomAll);
+  verifyFormat("pair<int, int> g() { return {1, {}}; }", CustomAll);
   verifyFormat("class C {\n"
                "  int f() { return 42; }\n"
                "};",



More information about the cfe-commits mailing list