@@ -10761,23 +10761,26 @@ static void diagnoseScopedEnums(Sema &S, const SourceLocation Loc,
10761
10761
return;
10762
10762
if (BinaryOperator::isAssignmentOp(Opc) && LHSIsScoped)
10763
10763
return;
10764
+ bool isCxx23 = S.getLangOpts().CPlusPlus23;
10765
+ unsigned diagID =
10766
+ isCxx23 ? diag::note_no_implicit_conversion_for_scoped_enum_cxx23
10767
+ : diag::note_no_implicit_conversion_for_scoped_enum;
10768
+ auto diagnosticHelper = [&S, isCxx23, diagID](const Expr *expr, const QualType type) {
10769
+ SourceLocation beginLoc = expr->getBeginLoc();
10770
+ QualType intType =
10771
+ type->castAs<EnumType>()->getDecl()->getIntegerType();
10772
+ std::string insertionString =
10773
+ isCxx23 ? "std::to_underlying("
10774
+ : "static_cast<" + intType.getAsString() + ">(";
10775
+ S.Diag(beginLoc, diagID)
10776
+ << FixItHint::CreateInsertion(beginLoc, insertionString)
10777
+ << FixItHint::CreateInsertion(expr->getEndLoc(), ")");
10778
+ };
10764
10779
if (LHSIsScoped) {
10765
- SourceLocation LHSBegin = LHSExpr->getBeginLoc();
10766
- QualType LHSIntType =
10767
- LHSType->castAs<EnumType>()->getDecl()->getIntegerType();
10768
- S.Diag(LHSBegin, diag::note_no_implicit_conversion_for_scoped_enum)
10769
- << FixItHint::CreateInsertion(
10770
- LHSBegin, "static_cast<" + LHSIntType.getAsString() + ">(")
10771
- << FixItHint::CreateInsertion(LHSExpr->getEndLoc(), ")");
10780
+ diagnosticHelper(LHSExpr, LHSType);
10772
10781
}
10773
10782
if (RHSIsScoped) {
10774
- SourceLocation RHSBegin = RHSExpr->getBeginLoc();
10775
- QualType RHSIntType =
10776
- RHSType->castAs<EnumType>()->getDecl()->getIntegerType();
10777
- S.Diag(RHSBegin, diag::note_no_implicit_conversion_for_scoped_enum)
10778
- << FixItHint::CreateInsertion(
10779
- RHSBegin, "static_cast<" + RHSIntType.getAsString() + ">(")
10780
- << FixItHint::CreateInsertion(RHSExpr->getEndLoc(), ")");
10783
+ diagnosticHelper(RHSExpr, RHSType);
10781
10784
}
10782
10785
}
10783
10786
0 commit comments