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

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 16 09:25:16 PDT 2023


MyDeveloperDay created this revision.
MyDeveloperDay added a reviewer: owenpan.
MyDeveloperDay added a project: clang-format.
Herald added projects: All, clang.
Herald added reviewers: rymiel, HazardyKnusperkeks.
MyDeveloperDay requested review of this revision.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148472

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


Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -1176,6 +1176,14 @@
   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) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3915,6 +3915,8 @@
       }
     }
   }
+  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 ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148472.514032.patch
Type: text/x-patch
Size: 1347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230416/c72cf0e8/attachment.bin>


More information about the cfe-commits mailing list