Skip to content

[clang][Sema] Check conditional branches inside a comma operand - #220573

Open
flash1729 wants to merge 3 commits into
llvm:mainfrom
flash1729:gh185400-enum-cast-comma
Open

flash1729 wants to merge 3 commits into
llvm:mainfrom
flash1729:gh185400-enum-cast-comma

Conversation

@flash1729

Copy link
Copy Markdown
Member

In C, a conditional between two enumerators has type int, so returning
(side(), c ? A : B) from a function returning that enum made
-Wimplicit-int-enum-cast claim the code is invalid in C++, even though
compiling it as C++ accepts it. The comma path converted the whole
conditional to the context type, while CheckConditionalOperand already
checks conditionals branch by branch.

Give CheckCommaOperand the same behavior: recurse into a conditional
right operand so each branch is checked individually. A branch that
genuinely needs the conversion still warns.

Fixes #185400

Comment on lines +76 to +81
// The branches of a conditional operand are each converted to the context
// type, so a conditional between enumerators of the target type is fine in
// C++ and must not be diagnosed here either.
enum E1 comma5(int c) {
return ((void)0, c ? E1_One : E1_Zero);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a C++ test?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comma path only runs under !S.getLangOpts().CPlusPlus, so this change doesn't reach C++ at all. The third RUN line already compiles this file as C++ and comma5 passes clean there (comma6 keeps its cxx-error), do we still need a separate test then ?

@flash1729
flash1729 force-pushed the gh185400-enum-cast-comma branch from 317a581 to 4509c5d Compare September 4, 2026 19:36
@flash1729
flash1729 marked this pull request as ready for review September 4, 2026 19:36
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 4, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Aditya Medhane (flash1729)

Changes

In C, a conditional between two enumerators has type int, so returning
(side(), c ? A : B) from a function returning that enum made
-Wimplicit-int-enum-cast claim the code is invalid in C++, even though
compiling it as C++ accepts it. The comma path converted the whole
conditional to the context type, while CheckConditionalOperand already
checks conditionals branch by branch.

Give CheckCommaOperand the same behavior: recurse into a conditional
right operand so each branch is checked individually. A branch that
genuinely needs the conversion still warns.

Fixes #185400


Full diff: https://github.com/llvm/llvm-project/pull/220573.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.md (+5)
  • (modified) clang/lib/Sema/SemaChecking.cpp (+10)
  • (modified) clang/test/Sema/implicit-int-enum-conversion.c (+18)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index b11e1d28a8b8f..6ba63470ecd1b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -474,6 +474,11 @@ features cannot lower the translation-unit ABI level;
   dimension that is a zero integer constant, as in `struct Empty vla[n]` or
   `int vla[n][0]`. (#GH28328)
 
+- `-Wimplicit-int-enum-cast` no longer warns about a conditional operator used
+  as the right operand of a comma operator when each branch of the conditional
+  is already of the target enumeration type. The branches are now checked
+  individually, as they are outside of a comma operator. (#GH185400)
+
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f0a1a529841b2..f6f29c1bfaae3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14042,6 +14042,16 @@ static void CheckCommaOperand(
     bool ExtraCheckForImplicitConversion,
     llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) {
   E = E->IgnoreParenImpCasts();
+
+  // A conditional operand is fed into the context type branch by branch, the
+  // same way CheckConditionalOperand does, so that a conditional whose
+  // branches each convert cleanly is not reported through the type of the
+  // conditional as a whole. CheckConditionalOperator analyzes the
+  // subexpressions itself, so do not add this one to the work list.
+  if (ExtraCheckForImplicitConversion && E->getType() != T)
+    if (auto *CO = dyn_cast<AbstractConditionalOperator>(E))
+      return CheckConditionalOperator(S, CO, CC, T);
+
   WorkList.push_back({E, CC, false});
 
   if (ExtraCheckForImplicitConversion && E->getType() != T)
diff --git a/clang/test/Sema/implicit-int-enum-conversion.c b/clang/test/Sema/implicit-int-enum-conversion.c
index 36717f36dd083..eb5948e74d3b8 100644
--- a/clang/test/Sema/implicit-int-enum-conversion.c
+++ b/clang/test/Sema/implicit-int-enum-conversion.c
@@ -72,3 +72,21 @@ enum E1 comma4(void) {
   return ((void)1, 2); // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
                           cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'int'}}
 }
+
+// The branches of a conditional operand are each converted to the context
+// type, so a conditional between enumerators of the target type is fine in
+// C++ and must not be diagnosed here either.
+enum E1 comma5(int c) {
+  return ((void)0, c ? E1_One : E1_Zero); // Okay, no conversion in C++
+}
+
+#ifdef __cplusplus
+// In C++ the enumerators already have the enumeration type, so the conditional
+// has type E1 and there is nothing to convert.
+static_assert(__is_same(decltype(true ? E1_One : E1_Zero), E1), "");
+#endif
+
+enum E1 comma6(int c) {
+  return ((void)0, c ? E1_One : 2); // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
+                                       cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'int'}}
+}

@flash1729
flash1729 requested a review from zyn0217 September 9, 2026 02:40
A conditional operator appearing as the right operand of a comma operator
was converted to the context type as a whole. In C the type of a
conditional between two enumerators is int, so returning
'(void)0, c ? E1_One : E1_Zero' from a function returning that
enumeration was reported as an int to enum conversion, even though the
same code is valid in C++.

Recurse into the conditional so each branch is checked against the
context type, matching what CheckConditionalOperand already does outside
of a comma operator. A branch that genuinely needs the conversion is
still diagnosed.

Fixes llvm#185400
@flash1729
flash1729 force-pushed the gh185400-enum-cast-comma branch from 4d557d9 to 55ba74c Compare September 11, 2026 17:21
@cor3ntin

Copy link
Copy Markdown
Contributor

@flash1729 Please disclose any use of AI

@flash1729

Copy link
Copy Markdown
Member Author

@flash1729 Please disclose any use of AI

Apologies for that, I should have mentioned this earlier.

I used Claude Code to walk through the issue and work out where the fix belonged. I wrote it up, built it, and ran the tests myself, and I'm happy to answer questions about any part of it. I'll add an Assisted-by note to the PR description.

little heads up -- my university exams have started, so I'll get to the remaining reviews once they're done, in about two weeks.

@zyn0217
zyn0217 removed their request for review September 20, 2026 07:37

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] [diagnostics] Wrong -Wimplicit-int-enum-cast warning

3 participants