clang-format: Prefer formatting local lambdas like functions.

Before:
  auto my_lambda =
      [](const string &some_parameter) { return some_parameter.size(); };

After:
  auto my_lambda = [](const string &some_parameter) {
    return some_parameter.size();
  };

llvm-svn: 237235
This commit is contained in:
Daniel Jasper 2015-05-13 08:47:16 +00:00
parent aba4a34ef2
commit 0ad2814c89
2 changed files with 6 additions and 0 deletions

View File

@ -1593,6 +1593,9 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Right.is(tok::l_square)) {
if (Style.Language == FormatStyle::LK_Proto)
return 1;
// Slightly prefer formatting local lambda definitions like functions.
if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
return 50;
if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare))
return 500;
}

View File

@ -9967,6 +9967,9 @@ TEST_F(FormatTest, FormatsLambdas) {
" : Field([] { // comment\n"
" int i;\n"
" }) {}");
verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
" return some_parameter.size();\n"
"};");
// Lambdas with return types.
verifyFormat("int c = []() -> int { return 2; }();\n");