Index: clang-tidy/google/AvoidCStyleCastsCheck.cpp =================================================================== --- clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -82,7 +82,11 @@ if (!Result.Context->getLangOpts().CPlusPlus) return; - std::string DestTypeString = CastExpr->getTypeAsWritten().getAsString(); + StringRef DestTypeString = Lexer::getSourceText( + CharSourceRange::getTokenRange( + CastExpr->getLParenLoc().getLocWithOffset(1), + CastExpr->getRParenLoc().getLocWithOffset(-1)), + *Result.SourceManager, Result.Context->getLangOpts()); auto diag_builder = diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0"); @@ -117,7 +121,13 @@ ReplaceWithCast("const_cast"); return; } - if (SourceType->isBuiltinType() && DestType->isBuiltinType()) { + // FALLTHROUGH + case clang::CK_IntegralCast: + // Convert integral and no-op casts between builtin types and enums to + // static_cast. A cast from enum to integer may be unnecessary, but it's + // still retained. + if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) && + (DestType->isBuiltinType() || DestType->isEnumeralType())) { ReplaceWithCast("static_cast"); return; } Index: test/clang-tidy/avoid-c-style-casts.cpp =================================================================== --- test/clang-tidy/avoid-c-style-casts.cpp +++ test/clang-tidy/avoid-c-style-casts.cpp @@ -3,6 +3,7 @@ bool g() { return false; } +enum Enum { Enum1 }; struct X {}; struct Y : public X {}; @@ -13,39 +14,38 @@ char *pc = (char*)cpc; // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char *pc = const_cast(cpc); + // CHECK-FIXES: char *pc = const_cast(cpc); char *pc2 = (char*)(cpc + 33); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char *pc2 = const_cast(cpc + 33); + // CHECK-FIXES: char *pc2 = const_cast(cpc + 33); const char &crc = *cpc; char &rc = (char&)crc; // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char &rc = const_cast(crc); + // CHECK-FIXES: char &rc = const_cast(crc); char &rc2 = (char&)*cpc; // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char &rc2 = const_cast(*cpc); + // CHECK-FIXES: char &rc2 = const_cast(*cpc); char ** const* const* ppcpcpc; char ****ppppc = (char****)ppcpcpc; // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char ****ppppc = const_cast(ppcpcpc); + // CHECK-FIXES: char ****ppppc = const_cast(ppcpcpc); char ***pppc = (char***)*(ppcpcpc); // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char ***pppc = const_cast(*(ppcpcpc)); + // CHECK-FIXES: char ***pppc = const_cast(*(ppcpcpc)); char ***pppc2 = (char***)(*ppcpcpc); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged. Use const_cast. {{.*}} - // CHECK-FIXES: char ***pppc2 = const_cast(*ppcpcpc); + // CHECK-FIXES: char ***pppc2 = const_cast(*ppcpcpc); char *pc5 = (char*)(const char*)(cpv); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: C-style casts are discouraged. Use reinterpret_cast. {{.*}} - // CHECK-FIXES: char *pc5 = const_cast(reinterpret_cast(cpv)); - + // CHECK-FIXES: char *pc5 = const_cast(reinterpret_cast(cpv)); int b1 = (int)b; // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting] @@ -58,12 +58,20 @@ const char *pc3 = (const char*)cpv; // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: C-style casts are discouraged. Use reinterpret_cast. [google-readability-casting] - // CHECK-FIXES: const char *pc3 = reinterpret_cast(cpv); + // CHECK-FIXES: const char *pc3 = reinterpret_cast(cpv); char *pc4 = (char*)cpv; // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting] // CHECK-FIXES: char *pc4 = (char*)cpv; + b1 = (int)Enum1; + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting] + // CHECK-FIXES: b1 = static_cast(Enum1); + + Enum e = (Enum)b1; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting] + // CHECK-FIXES: Enum e = static_cast(b1); + // CHECK-MESSAGES-NOT: warning: int b2 = int(b); int b3 = static_cast(b);