16
votes

I have a lambda function formatted like this:

auto cb = [](std::string const& _param)
{
    std::cout << _param;
};

I would like to keep the opening brace on a new line, but clang-format always places it at the end of the first line. Is it possible to configure clang-format to follow the style above?

The relevant part of my current configuration looks like this:

BraceWrapping:                         
  AfterClass:      true   
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true          
  AfterNamespace:  true                
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     true
  BeforeElse:      true   
  IndentBraces:    true
BreakBeforeBraces: Allman

I would also like to do the same for extern blocks:

extern "C"
{
  // ...
}
2
What version of clang-format are you using? This might be a bug: mail-archive.com/[email protected]/msg05471.html - syntagma
I am using version 5.0.0 (tags/google/stable/2017-03-17) - Federico B.
I just found bug reports tracking the problem with lambdas bugs.llvm.org//show_bug.cgi?id=32151 and extern blocks bugs.llvm.org//show_bug.cgi?id=26689 - Federico B.
I believe all of your BraceWrapping values are ignored if you don't have BreakBeforeBraces set to Custom. - David Vereb
I believe more related is bugs.llvm.org/show_bug.cgi?id=27640 - Mikhail

2 Answers

5
votes

This appears to be addressed with BeforeLambdaBody introduced in clang-format 11.

4
votes

I don't think there is an option for this particular case. I am sorry if you don't consider this as an answer, but when I have these "fine tuning" problems with clang-format, I just format the snippet as I want and comment clang-format off just before the interesting snippet and clang-format on right after. I often do that to maintain some blank character sequence used to align code for better reading. The effect is of course that CF will not change that snippet. So your code may be

// clang-format off
auto cb = [](std::string const& _param)
{
    std::cout << _param;
};
// clang-format on