Skip to content

Commit 258d7b8

Browse files
eoan-ermineowenca
authored andcommitted
[clang-format] Handle constructor invocations after new operator in C# correct
Fixes #56549. Differential Revision: https://reviews.llvm.org/D129926
1 parent 75404e9 commit 258d7b8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,6 +2975,11 @@ void UnwrappedLineParser::parseNew() {
29752975

29762976
if (Style.isCSharp()) {
29772977
do {
2978+
// Handle constructor invocation, e.g. `new(field: value)`.
2979+
if (FormatTok->is(tok::l_paren))
2980+
parseParens();
2981+
2982+
// Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
29782983
if (FormatTok->is(tok::l_brace))
29792984
parseBracedList();
29802985

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,24 @@ var x = foo(className, $@"some code:
617617
EXPECT_EQ(Code, format(Code, Style));
618618
}
619619

620+
TEST_F(FormatTestCSharp, CSharpNewOperator) {
621+
FormatStyle Style = getLLVMStyle(FormatStyle::LK_CSharp);
622+
623+
verifyFormat("public void F() {\n"
624+
" var v = new C(() => { var t = 5; });\n"
625+
"}",
626+
Style);
627+
verifyFormat("public void F() {\n"
628+
" var v = new C(() => {\n"
629+
" try {\n"
630+
" } catch {\n"
631+
" var t = 5;\n"
632+
" }\n"
633+
" });\n"
634+
"}",
635+
Style);
636+
}
637+
620638
TEST_F(FormatTestCSharp, CSharpLambdas) {
621639
FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp);
622640
FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp);

0 commit comments

Comments
 (0)