[clang] [clang-format] Stop moving lambda to new line only to indent it more. (PR #141576)

via cfe-commits cfe-commits at lists.llvm.org
Tue May 27 19:48:33 PDT 2025


================
@@ -325,13 +325,30 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
   if (Current.isMemberAccess() && CurrentState.ContainsUnwrappedBuilder)
     return false;
 
-  // Don't create a 'hanging' indent if there are multiple blocks in a single
-  // statement and we are aligning lambda blocks to their signatures.
-  if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
+  // Force a lambda onto a new line so that we don't create a 'hanging' indent
+  // if there are multiple blocks in a single statement and we are aligning
+  // lambda blocks to their signatures.
+  if (Previous.is(tok::l_brace) && State.Stack.size() > 2 &&
----------------
rmarker wrote:

I think that it is alright, and that in the situations this code targets it should always be true.
The added check below looks further up the stack, so this ensures that it is always valid.

I considered adding separate handling for the case of a smaller stack, but thought that it might just be making the code more complex for no gain.
If a case is found where it does cause a formatting regression, it should be easy enough to fix by adding in such handling.
I.e.
```cpp
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
      State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
      State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks &&
      Style.LambdaBodyIndentation == FormatStyle::LBI_Signature) {
    if (State.Stack.size() == 2)
      return false;
```

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


More information about the cfe-commits mailing list