[clang] [clang-format] Fix erroneous BraceWrapping.BeforeLambdaBody column calcs (PR #76673)

James Grant via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 29 02:05:57 PST 2025


================
@@ -366,8 +366,14 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
   const auto &CurrentState = State.Stack.back();
   if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore &&
       Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) {
-    auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
-    return LambdaBodyLength > getColumnLimit(State);
+    if (Current.MatchingParen->MustBreakBefore)
----------------
jamesg-nz wrote:

@HazardyKnusperkeks - sorry late response. After looking into it, I don't believe there's an issue.

The code I was talking about above: https://github.com/llvm/llvm-project/blob/0e01c72c5645259d9a08a1a7ed39cb5cc41ce311/clang/lib/Format/TokenAnnotator.cpp#L5297-L5299

Results in `MustBreakBefore` being set for the token (the left lambda brace), which therefore results in `CanBreakBefore` being set. The code exists as there's conditions (e.g. [`Left.is(TT_PointerOrReference)`](https://github.com/llvm/llvm-project/blob/0e01c72c5645259d9a08a1a7ed39cb5cc41ce311/clang/lib/Format/TokenAnnotator.cpp#L5523-L5524)) in `TokenAnnotator::canBreakBefore()` which return `false` for the left lambda brace in lambdas like these:

```c++
auto select = [this]() -> const Library::Object * { return MyAssignment::SelectFromList(this); };
auto select = [this]() -> const Library::Object & { return MyAssignment::SelectFromList(this); };
auto select = [this]() -> std::unique_ptr<Object> { return MyAssignment::SelectFromList(this); };"
```

It's the fact it sets `CanBreakBefore` that matters so it enters the lambda section in `ContinuationIndenter::mustBreak()`.

Just checking `Current.MatchingParen->MustBreakBefore` inside is sufficient as TokenAnnotator::mustBreakBefore() returns `true` when it is called for the right lambda:
https://github.com/llvm/llvm-project/blob/0e01c72c5645259d9a08a1a7ed39cb5cc41ce311/clang/lib/Format/TokenAnnotator.cpp#L5283-L5293

Thus there is no need to look at `Current.MustBreakBefore`. However the generic condition that checks `Current.MustBreakBefore` for all tokens is in the condition for the next `if()` statement down, so could move the lambda `if()` statement down beneath that. But there's no need to do this at the moment.

So this PR looks OK as-is with respect to this.

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


More information about the cfe-commits mailing list