Skip to content

Commit

Permalink
[clang-format] CSharp don't allow there not to be a space between `is…
Browse files Browse the repository at this point in the history
…` 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
  • Loading branch information
mydeveloperday committed Apr 17, 2023
1 parent 8456120 commit 63395cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
}
}
}
if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
return true;
const auto SpaceRequiredForArrayInitializerLSquare =
[](const FormatToken &LSquareTok, const FormatStyle &Style) {
return Style.SpacesInContainerLiterals ||
Expand Down
8 changes: 8 additions & 0 deletions clang/unittests/Format/FormatTestCSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,14 @@ foreach ((A a, B b) in someList) {
verifyFormat(R"(override (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(async (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(unsafe (string name, int age) methodTuple() {})", Style);

Style.SpacesInSquareBrackets = false;
Style.SpaceBeforeSquareBrackets = true;
verifyFormat("return a is [1, 2, 3];", Style);
verifyFormat("return a is [..];", Style);
Style.SpaceBeforeSquareBrackets = false;
verifyFormat("return a is [1, 2, 3];", Style);
verifyFormat("return a is [..];", Style);
}

TEST_F(FormatTestCSharp, CSharpNullableTypes) {
Expand Down

0 comments on commit 63395cb

Please sign in to comment.