Skip to content

Commit

Permalink
[clang-format] Do not interpret C# deconstruction in a foreach as a cast
Browse files Browse the repository at this point in the history
Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits, MyDeveloperDay

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D78295
  • Loading branch information
jbcoe committed Apr 16, 2020
1 parent 11f093f commit 07c1978
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -1775,6 +1775,10 @@ class AnnotatingParser {
if (Tok.Next->is(tok::question))
return false;

// `foreach((A a, B b) in someList)` should not be seen as a cast.
if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
return false;

// Functions which end with decorations like volatile, noexcept are unlikely
// to be casts.
if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/FormatTestCSharp.cpp
Expand Up @@ -624,6 +624,7 @@ TEST_F(FormatTestCSharp, CSharpSpaces) {
Style.SpaceBeforeCpp11BracedList = true;
Style.Cpp11BracedListStyle = false;
Style.SpacesInContainerLiterals = false;
Style.SpaceAfterCStyleCast = false;

verifyFormat(R"(new Car { "Door", 0.1 })", Style);
verifyFormat(R"(new Car { 0.1, "Door" })", Style);
Expand All @@ -642,6 +643,12 @@ TEST_F(FormatTestCSharp, CSharpSpaces) {

verifyFormat(R"(char[,,] rawCharArray = MakeCharacterGrid();)", Style);

// Not seen as a C-style cast.
verifyFormat(R"(//
foreach ((A a, B b) in someList) {
})",
Style);

Style.SpacesInSquareBrackets = true;
verifyFormat(R"(private float[ , ] Values;)", Style);
verifyFormat(R"(string dirPath = args?[ 0 ];)", Style);
Expand Down

0 comments on commit 07c1978

Please sign in to comment.