Skip to content

Commit e2b6e21

Browse files
committed
[clang-format] Fix incorrect formatting of lambdas inside brace initialisation
Fixes llvm/llvm-project#27146. Fixes llvm/llvm-project#52943. Before: ``` namespace ns { void foo() { std::variant<int, double> v; std::visit(overloaded{[](auto &&) -> int (*)[] { return nullptr; }}, v); } } // namespace ns int break_me() { int x = 42; return int{[x = x]() { return x; }()}; } ``` got formatted as: ``` namespace ns { void foo() { std::variant<int, double> v; std::visit(overloaded{[](auto &&) -> int (*)[] { return nullptr; } } // namespace ns , v); } } // namespace ns int break_me() { int x = 42; return int{[x = x](){return x; } () } ; } ``` Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D116553
1 parent 2a0e051 commit e2b6e21

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,9 @@ bool UnwrappedLineParser::tryToParseLambda() {
17861786
case tok::l_paren:
17871787
parseParens();
17881788
break;
1789+
case tok::l_square:
1790+
parseSquare();
1791+
break;
17891792
case tok::amp:
17901793
case tok::star:
17911794
case tok::kw_const:

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20232,6 +20232,11 @@ TEST_F(FormatTest, FormatsLambdas) {
2023220232
"};");
2023320233
verifyFormat("[]() -> Void<T...> {};");
2023420234
verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20235+
verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20236+
verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20237+
verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20238+
verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20239+
verifyFormat("return int{[x = x]() { return x; }()};");
2023520240

2023620241
// Lambdas with explicit template argument lists.
2023720242
verifyFormat(

0 commit comments

Comments
 (0)