如何告诉 clang-format 不要在单行上制作短的 lambda 表达式?

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();
}

我想以这种方式保留我的简短 lambda 表达式。但是 clang-format 总是在一行上。

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

我已阅读文档,但找不到正确的设置。这是我的配置:

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

我的 clang-format 版本是 7.0.0。如果 lambda 表达式多于两行,则效果很好。但是当它只有一行时。 clang-format 总是在一行中。有时一个简短的函数,其主体只有一行,clang-format 也使其成为一行。

不知道这个选项是什么时候加入的,至少从10.0版本开始就有了AllowShortLambdasOnASingleLine。参见 official docs

在您的情况下,您需要选项的值为 Inline,或 Empty,或 None