[clang-tools-extra] r296755 - [clang-tidy] google-readability-casting: detect redundant casts with top-level const

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 2 07:47:28 PST 2017


Author: alexfh
Date: Thu Mar  2 09:47:28 2017
New Revision: 296755

URL: http://llvm.org/viewvc/llvm-project?rev=296755&view=rev
Log:
[clang-tidy] google-readability-casting: detect redundant casts with top-level const

Modified:
    clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp

Modified: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=296755&r1=296754&r2=296755&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp Thu Mar  2 09:47:28 2017
@@ -75,12 +75,12 @@ void AvoidCStyleCastsCheck::check(const
            T->isMemberFunctionPointerType();
   };
 
-  const QualType DestTypeAsWritten = CastExpr->getTypeAsWritten();
-  const QualType SourceTypeAsWritten = CastExpr->getSubExprAsWritten()->getType();
-  const QualType SourceType =
-      SourceTypeAsWritten.getCanonicalType().getUnqualifiedType();
-  const QualType DestType =
-      DestTypeAsWritten.getCanonicalType().getUnqualifiedType();
+  const QualType DestTypeAsWritten =
+      CastExpr->getTypeAsWritten().getUnqualifiedType();
+  const QualType SourceTypeAsWritten =
+      CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
+  const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
+  const QualType DestType = DestTypeAsWritten.getCanonicalType();
 
   bool FnToFnCast =
       isFunction(SourceTypeAsWritten) && isFunction(DestTypeAsWritten);

Modified: clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp?rev=296755&r1=296754&r2=296755&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp Thu Mar  2 09:47:28 2017
@@ -103,6 +103,19 @@ void f(int a, double b, const char *cpc,
   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast [
   // CHECK-FIXES: Enum e = static_cast<Enum>(b1);
 
+  e = (Enum)Enum1;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
+  // CHECK-FIXES: {{^}}  e = Enum1;
+
+  e = (Enum)e;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
+  // CHECK-FIXES: {{^}}  e = e;
+
+  static const int kZero = 0;
+  (int)kZero;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type
+  // CHECK-FIXES: {{^}}  kZero;
+
   int b2 = int(b);
   int b3 = static_cast<double>(b);
   int b4 = b;




More information about the cfe-commits mailing list