[clang-format] Fix incorrect formatting of lambdas inside brace initialisation
authorMarek Kurdej <marek.kurdej+llvm.org@gmail.com>
Mon, 3 Jan 2022 16:32:20 +0000 (17:32 +0100)
committerMarek Kurdej <marek.kurdej+llvm.org@gmail.com>
Tue, 4 Jan 2022 07:28:12 +0000 (08:28 +0100)
Fixes https://github.com/llvm/llvm-project/issues/27146.
Fixes https://github.com/llvm/llvm-project/issues/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

clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp

index 0579acf..17187b7 100644 (file)
@@ -1786,6 +1786,9 @@ bool UnwrappedLineParser::tryToParseLambda() {
     case tok::l_paren:
       parseParens();
       break;
+    case tok::l_square:
+      parseSquare();
+      break;
     case tok::amp:
     case tok::star:
     case tok::kw_const:
index 49635f3..71f0741 100644 (file)
@@ -20232,6 +20232,11 @@ TEST_F(FormatTest, FormatsLambdas) {
                "};");
   verifyFormat("[]() -> Void<T...> {};");
   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
+  verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
+  verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
+  verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
+  verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
+  verifyFormat("return int{[x = x]() { return x; }()};");
 
   // Lambdas with explicit template argument lists.
   verifyFormat(