[clang] [clang-format] Don't merge short records with directly created objects (PR #189200)

via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 28 16:27:14 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Björn Schäpers (HazardyKnusperkeks)

<details>
<summary>Changes</summary>

It did merge the wrong brace.

Fixes #<!-- -->189155

---
Full diff: https://github.com/llvm/llvm-project/pull/189200.diff


2 Files Affected:

- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+3-1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+6) 


``````````diff
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 9c28f64dc6a95..492e0901385b4 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -988,8 +988,10 @@ class LineJoiner {
         if (I[1]->Last->is(TT_LineComment))
           return 0;
         do {
-          if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit))
+          if (Tok->isOneOf(tok::l_brace, tok::r_brace) &&
+              Tok->isNot(BK_BracedInit)) {
             return 0;
+          }
           Tok = Tok->Next;
         } while (Tok);
 
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3d55a814e6027..676aa5ad42d2e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15563,6 +15563,12 @@ TEST_F(FormatTest, AllowShortRecordOnASingleLine) {
   verifyFormat("class foo {};\n"
                "class bar { int i; };",
                Style);
+  verifyFormat("namespace foo {\n"
+               "struct S {\n"
+               "} s;\n"
+               "} // namespace foo",
+               Style);
+
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterClass = true;
   verifyFormat("class foo\n"

``````````

</details>


https://github.com/llvm/llvm-project/pull/189200


More information about the cfe-commits mailing list