Skip to content

Commit 63395cb

Browse files
[clang-format] CSharp don't allow there not to be a space between is and [
as `is` is a keyword in C# ensure there is always a space before the `[` regardless of `SpaceBeforeSquareBrackets` setting Fixes: #61965 #61965 Reviewed By: owenpan, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D148472
1 parent 8456120 commit 63395cb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,6 +3917,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
39173917
}
39183918
}
39193919
}
3920+
if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
3921+
return true;
39203922
const auto SpaceRequiredForArrayInitializerLSquare =
39213923
[](const FormatToken &LSquareTok, const FormatStyle &Style) {
39223924
return Style.SpacesInContainerLiterals ||

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,14 @@ foreach ((A a, B b) in someList) {
11761176
verifyFormat(R"(override (string name, int age) methodTuple() {})", Style);
11771177
verifyFormat(R"(async (string name, int age) methodTuple() {})", Style);
11781178
verifyFormat(R"(unsafe (string name, int age) methodTuple() {})", Style);
1179+
1180+
Style.SpacesInSquareBrackets = false;
1181+
Style.SpaceBeforeSquareBrackets = true;
1182+
verifyFormat("return a is [1, 2, 3];", Style);
1183+
verifyFormat("return a is [..];", Style);
1184+
Style.SpaceBeforeSquareBrackets = false;
1185+
verifyFormat("return a is [1, 2, 3];", Style);
1186+
verifyFormat("return a is [..];", Style);
11791187
}
11801188

11811189
TEST_F(FormatTestCSharp, CSharpNullableTypes) {

0 commit comments

Comments
 (0)