llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang-format BraceWrapping.BeforeLambdaBody: True overrides AllowShortLambdasOnASingleLine: All

Open d3matt opened this issue 2 years ago • 5 comments

Found a partial workaround here: https://stackoverflow.com/questions/67547349/clang-format-lambda-on-single-line-doesnt-work

It should be possible to have multi-line lambdas require Allman style bracing, but have short lambdas truly be a single line.

Suggest switching BeforeLambdaBody from a boolean to a tri-state (Never, OnlyMultiline, Always). With OnlyMultiLine having the new behavior I'm looking for:

auto oneliner = [](auto x){ return 2*x; };
auto multiliner = [](auto x)
{
    if (x % 2 == 0)
    {
        return x*2;
    }
    return x*3;
};

d3matt avatar Sep 09 '24 21:09 d3matt

Works as expected with 20.1.2.

owenca avatar Apr 13 '25 00:04 owenca

@owenca Why is it expected? It's not possible to have short lambdas and wrapping before the lambda body at the same time.

Vladislav-Bartalevich avatar May 28 '25 10:05 Vladislav-Bartalevich

$ cat a.cc
auto oneliner = [](auto x){ return 2*x; };
auto multiliner = [](auto x)
{
    if (x % 2 == 0)
    {
        return x*2;
    }
    return x*3;
};
$ clang-format -version
clang-format version 20.1.5
$ clang-format -style='{BreakBeforeBraces: Allman}' a.cc
auto oneliner = [](auto x) { return 2 * x; };
auto multiliner = [](auto x)
{
  if (x % 2 == 0)
  {
    return x * 2;
  }
  return x * 3;
};

This is what @d3matt was looking for.

owenca avatar May 29 '25 02:05 owenca

Hi @owenca

I think I found the culprit, there seems to be an incompatibility between BraceWrapping/BeforeLambdaBody: true and ColumnLimit: 0. Do you agree that this behaviour is wrong?

Thanks for having a look!

$ cat a.cc 
auto oneliner = [](auto x){ return 2*x; };
auto multiliner = [](auto x)
{
    if (x % 2 == 0)
    {
        return x*2;
    }
    return x*3;
};
$ clang-format --version
Ubuntu clang-format version 21.1.8 (6ubuntu1)
$ clang-format -style='{BreakBeforeBraces: Custom, BraceWrapping: {BeforeLambdaBody: true}, AllowShortLambdasOnASingleLine: true }' a.cc 
auto oneliner = [](auto x) { return 2 * x; };
auto multiliner = [](auto x)
{
  if (x % 2 == 0) {
    return x * 2;
  }
  return x * 3;
};
$ clang-format -style='{BreakBeforeBraces: Custom, BraceWrapping: {BeforeLambdaBody: true}, AllowShortLambdasOnASingleLine: true, ColumnLimit: 0 }' a.cc 
auto oneliner = [](auto x)
{ return 2 * x; };
auto multiliner = [](auto x)
{
  if (x % 2 == 0) {
    return x * 2;
  }
  return x * 3;
};

christof-arnosti-franke avatar Aug 11 '26 12:08 christof-arnosti-franke

I would like to take this on. Having recently worked on related clang-format pr.

AhmedKamel10 avatar Aug 23 '26 04:08 AhmedKamel10