Skip to content

Commit 61d78d0

Browse files
authored
[clang-format] Correctly handle C# where clause (#137295)
Fix #74947
1 parent 5c19723 commit 61d78d0

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
295295
do {
296296
switch (FormatTok->Tok.getKind()) {
297297
case tok::l_brace:
298+
case tok::semi:
298299
return;
299300
default:
300301
if (FormatTok->is(Keywords.kw_where)) {

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,12 @@ TEST_F(FormatTestCSharp, CSharpGenericTypeConstraints) {
13151315
"}",
13161316
Style);
13171317

1318+
verifyFormat("namespace A {\n"
1319+
" delegate T MyDelegate<T>()\n"
1320+
" where T : new();\n"
1321+
"}",
1322+
Style);
1323+
13181324
// When the "where" line is not to be formatted, following lines should not
13191325
// take on its indentation.
13201326
verifyFormat("class ItemFactory<T>\n"

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,6 +3099,16 @@ TEST_F(TokenAnnotatorTest, CSharpNullableTypes) {
30993099
EXPECT_TOKEN(Tokens[1], tok::question, TT_ConditionalExpr);
31003100
}
31013101

3102+
TEST_F(TokenAnnotatorTest, CSharpGenericTypeConstraint) {
3103+
auto Tokens = annotate("namespace A {\n"
3104+
" delegate T MyDelegate<T>()\n"
3105+
" where T : new();\n"
3106+
"}",
3107+
getGoogleStyle(FormatStyle::LK_CSharp));
3108+
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
3109+
EXPECT_TOKEN(Tokens[18], tok::r_brace, TT_NamespaceRBrace);
3110+
}
3111+
31023112
TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
31033113
auto Tokens = annotate("{ x: break; }");
31043114
ASSERT_EQ(Tokens.size(), 7u) << Tokens;

0 commit comments

Comments
 (0)