Search code examples
clang-format

How to tell clang-format do not make short lambda expression on a single line?


#include <iostream>

int main()
{
    auto func = []() { 
        std::cout << "hello, world" << std::endl; 
    };
    func();
}

I would like to keep my short lambda expression in this way. But clang-format always make it on a single line.

auto func = []() { std::cout << "hello, world" << std::endl; };

I have read the documentation but I can't find the correct settings. Here is my configure:

BasedOnStyle:   LLVM
Language:   Cpp
AccessModifierOffset:   -4
AlignAfterOpenBracket:  Align
AlignConsecutiveAssignments:    true
AlignConsecutiveDeclarations:   true
AlignEscapedNewlines: Left
AlignOperands:  true
AlignTrailingComments:  true
AllowAllParametersOfDeclarationOnNextLine:  true
AllowShortBlocksOnASingleLine:  false
AllowShortCaseLabelsOnASingleLine:  false
AllowShortFunctionsOnASingleLine:   Empty
AllowShortIfStatementsOnASingleLine:    true
AllowShortLoopsOnASingleLine:   false
AlwaysBreakAfterDefinitionReturnType:   None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings:  false
AlwaysBreakTemplateDeclarations:    false
BinPackArguments:   false
BinPackParameters:  false
BraceWrapping:   
  AfterClass:   true
  AfterControlStatement: true   
  AfterEnum: true   
  AfterFunction:    true
  AfterNamespace:   false
  AfterObjCDeclaration: false
  AfterStruct:  true
  AfterUnion:   false
  BeforeCatch:  true
  BeforeElse:   true
  IndentBraces: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces:  Custom
BreakBeforeTernaryOperators:    true
BreakConstructorInitializersBeforeComma:    true
ColumnLimit:    120
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth:  4
ContinuationIndentWidth:    4
Cpp11BracedListStyle:   false
DerivePointerAlignment: false
DisableFormat:  false
ExperimentalAutoDetectBinPacking:   false
ForEachMacros:  [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories: 
  - Regex:  '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:   2
  - Regex:  '^(<|"(gtest|isl|json)/)'
    Priority:   3
  - Regex:  '.*'
    Priority:   1
IndentCaseLabels:   true
IndentWidth:    4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks:   true
MacroBlockBegin:    ''
MacroBlockEnd:  ''
MaxEmptyLinesToKeep:    1
NamespaceIndentation:   Inner
ObjCBlockIndentWidth:   4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList:    true
PenaltyBreakBeforeFirstCallParameter:   19
PenaltyBreakComment:    300
PenaltyBreakFirstLessLess:  120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine:  60
PointerAlignment:   Right
ReflowComments: true
SortIncludes:   true
SortUsingDeclarations: true
SpaceAfterCStyleCast:   false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens:  ControlStatements
SpaceInEmptyParentheses:    false
SpacesBeforeTrailingComments:   1
SpacesInAngles: false
SpacesInContainerLiterals:  true
SpacesInCStyleCastParentheses:  false
SpacesInParentheses:    false
SpacesInSquareBrackets: false
Standard:   Cpp11
TabWidth:   4
UseTab: Never

My clang-format version is 7.0.0. If the lambda expression have more than two lines, it works well. But when it only one line. clang-format always make it in one line. Sometimes a short function which body only one line, clang-format also make it in one line.


Solution

  • Since version 9.0 there is AllowShortLambdasOnASingleLine option. See official docs.

    In your case you need option's value to be Inline, or Empty, or None.