[clang] 63395cb - [clang-format] CSharp don't allow there not to be a space between `is` and `[`

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 17 11:40:06 PDT 2023


Author: mydeveloperday
Date: 2023-04-17T19:38:53+01:00
New Revision: 63395cb0b69dee69ec5d5d0d552482c607861178

URL: https://github.com/llvm/llvm-project/commit/63395cb0b69dee69ec5d5d0d552482c607861178
DIFF: https://github.com/llvm/llvm-project/commit/63395cb0b69dee69ec5d5d0d552482c607861178.diff

LOG: [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

https://github.com/llvm/llvm-project/issues/61965

Reviewed By: owenpan, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D148472

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/FormatTestCSharp.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 28e21f93c14c4..8b00b1c141774 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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 ||

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 1928a0d2da632..9f912624d1658 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -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) {


        


More information about the cfe-commits mailing list