[clang] [Clang] Add diagnostic when scoped enumeration requires an explicit conversion for binary operations (PR #152698)
Timothy Choi via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 15 08:59:42 PDT 2025
================
@@ -10742,9 +10742,53 @@ static void DiagnoseBadDivideOrRemainderValues(Sema& S, ExprResult &LHS,
<< IsDiv << RHS.get()->getSourceRange());
}
+static void diagnoseScopedEnums(Sema &S, const SourceLocation Loc,
+ const ExprResult &LHS, const ExprResult &RHS,
+ BinaryOperatorKind Opc) {
+ const Expr *LHSExpr = LHS.get();
+ const Expr *RHSExpr = RHS.get();
+ if (!LHSExpr || !RHSExpr)
+ return;
+ const QualType LHSType = LHSExpr->getType();
+ const QualType RHSType = RHSExpr->getType();
+ const bool LHSIsScoped = LHSType->isScopedEnumeralType();
+ const bool RHSIsScoped = RHSType->isScopedEnumeralType();
+ if (!LHSIsScoped && !RHSIsScoped)
+ return;
+ if (!LHSIsScoped && !LHSType->isIntegralOrUnscopedEnumerationType())
+ return;
+ if (!RHSIsScoped && !RHSType->isIntegralOrUnscopedEnumerationType())
+ return;
+ if (BinaryOperator::isAssignmentOp(Opc) && LHSIsScoped)
+ return;
+ bool isCxx23 = S.getLangOpts().CPlusPlus23;
+ unsigned diagID =
----------------
tinnamchoi wrote:
Would something like `no implicit conversion for scoped enum; consider casting to underlying type, or using std::to_underlying in standards >= C++23` regardless of language mode be good? It's more verbose than I'd like but feels better than suggesting something outdated.
Also, I recently found out `std::underlying_type` is a thing. I don't think it's useful enough to add and feels a bit cluttering, but I'd like to hear some feedback on it as it feels more semantically correct.
https://github.com/llvm/llvm-project/pull/152698
More information about the cfe-commits
mailing list