Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -10566,12 +10566,15 @@ assert(Match != ArgType::MatchPromotion); // Look through unscoped enums to their underlying type. bool IsEnum = false; + bool IsScopedEnum = false; if (auto EnumTy = ExprTy->getAs()) { if (EnumTy->isUnscopedEnumerationType()) { ExprTy = EnumTy->getDecl()->getIntegerType(); // This controls whether we're talking about the underlying type or not, // which we only want to do when it's an unscoped enum. IsEnum = true; + } else { + IsScopedEnum = true; } } @@ -10637,7 +10640,7 @@ CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); - if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) { + if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) { unsigned Diag; switch (Match) { case ArgType::Match: @@ -10674,11 +10677,17 @@ SmallString<16> CastBuf; llvm::raw_svector_ostream CastFix(CastBuf); CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); - IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); + if (IsScopedEnum) { + CastFix << AT.getRepresentativeType(S.Context).getAsString( + S.Context.getPrintingPolicy()); + } else { + IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); + } CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); SmallVector Hints; - if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly) + if ((!AT.matchesType(S.Context, IntendedTy) && !IsScopedEnum) || + ShouldNotPrintDirectly) Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); if (const CStyleCastExpr *CCast = dyn_cast(E)) { Index: clang/test/FixIt/format.cpp =================================================================== --- /dev/null +++ clang/test/FixIt/format.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s 2>&1 | FileCheck %s + +extern "C" int printf(const char *, ...); + +namespace N { + enum class E { One }; +} + +void a() { + printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")" + + printf("%hd", N::E::One); + // CHECK: "static_cast(" + + printf("%hu", N::E::One); + // CHECK: "static_cast(" +}