Hi klimek,

Convert c-style casts between integral end enum types to static_cast<>.

http://reviews.llvm.org/D5558

Files:
  clang-tidy/google/AvoidCStyleCastsCheck.cpp
  test/clang-tidy/avoid-c-style-casts.cpp
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<char *>(cpc);
+  // CHECK-FIXES: char *pc = const_cast<char*>(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<char *>(cpc + 33);
+  // CHECK-FIXES: char *pc2 = const_cast<char*>(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<char &>(crc);
+  // CHECK-FIXES: char &rc = const_cast<char&>(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<char &>(*cpc);
+  // CHECK-FIXES: char &rc2 = const_cast<char&>(*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<char ****>(ppcpcpc);
+  // CHECK-FIXES: char ****ppppc = const_cast<char****>(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<char ***>(*(ppcpcpc));
+  // CHECK-FIXES: char ***pppc = const_cast<char***>(*(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<char ***>(*ppcpcpc);
+  // CHECK-FIXES: char ***pppc2 = const_cast<char***>(*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<char *>(reinterpret_cast<const char *>(cpv));
-
+  // CHECK-FIXES: char *pc5 = const_cast<char*>(reinterpret_cast<const char*>(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<const char *>(cpv);
+  // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char*>(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<int>(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<Enum>(b1);
+
   // CHECK-MESSAGES-NOT: warning:
   int b2 = int(b);
   int b3 = static_cast<double>(b);
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to