From 0ad2814c89b3a1033079dc3848e252955897abd6 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 13 May 2015 08:47:16 +0000 Subject: [PATCH] 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 --- clang/lib/Format/TokenAnnotator.cpp | 3 +++ clang/unittests/Format/FormatTest.cpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6cf3f1d5230e..777de9bf1e3c 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 371fa0f3d22d..ecd78b24d094 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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");