Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1459,8 +1459,14 @@ // followed by a curly. if (FormatTok->is(TT_JsFatArrow)) { nextToken(); - if (FormatTok->is(tok::l_brace)) + if (FormatTok->is(tok::l_brace)) { + // C# may break after => if the next character is a newline. + if (Style.isCSharp() && Style.BraceWrapping.AfterFunction == true) { + // calling `addUnwrappedLine()` here causes odd parsing errors. + FormatTok->MustBreakBefore = true; + } parseChildBlock(); + } break; } Index: clang/unittests/Format/FormatTestCSharp.cpp =================================================================== --- clang/unittests/Format/FormatTestCSharp.cpp +++ clang/unittests/Format/FormatTestCSharp.cpp @@ -525,6 +525,33 @@ EXPECT_EQ(Code, format(Code, Style)); } +TEST_F(FormatTestCSharp, CSharpLambdas) { + FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp); + FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp); + + verifyFormat(R"(// +class MyClass { + Action greet = name => { + string greeting = $"Hello {name}!"; + Console.WriteLine(greeting); + }; +})", + GoogleStyle); + + // Microsoft Style: + // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions#statement-lambdas + verifyFormat(R"(// +class MyClass +{ + Action greet = name => + { + string greeting = $"Hello {name}!"; + Console.WriteLine(greeting); + }; +})", + MicrosoftStyle); +} + TEST_F(FormatTestCSharp, CSharpObjectInitializers) { FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);