[PATCH] Avoid adding redundant parens.

Alexander Kornienko alexfh at google.com
Wed Jul 16 06:47:34 PDT 2014


Closed by commit rL213149 (authored by @alexfh).

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D4534

Files:
  clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
  clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp

Index: clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp
@@ -17,7 +17,7 @@
 
   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;
@@ -33,6 +33,20 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged. Use const_cast. {{.*}}
   // 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));
+
+  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);
+
+  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));
+
+
   int b1 = (int)b;
   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
   // CHECK-FIXES: int b1 = static_cast<int>(b);
Index: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -56,6 +56,7 @@
   }
   return SourceType.getUnqualifiedType() == DestType.getUnqualifiedType();
 }
+
 void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
 
@@ -85,15 +86,18 @@
     diag_builder << ("Use " + CastType + ".").str();
     if (ParenRange.getBegin().isMacroID() || ParenRange.getEnd().isMacroID())
       return;
-    diag_builder << FixItHint::CreateReplacement(
-                        ParenRange,
-                        (CastType + "<" + DestTypeString + ">(").str())
-                 << FixItHint::CreateInsertion(
-                        Lexer::getLocForEndOfToken(
-                            CastExpr->getSubExprAsWritten()->getLocEnd(), 0,
-                            *Result.SourceManager,
-                            Result.Context->getLangOpts()),
-                        ")");
+
+    const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
+    std::string CastText = (CastType + "<" + DestTypeString + ">").str();
+    if (!isa<ParenExpr>(SubExpr)) {
+      CastText.push_back('(');
+      diag_builder << FixItHint::CreateInsertion(
+          Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0,
+                                     *Result.SourceManager,
+                                     Result.Context->getLangOpts()),
+          ")");
+    }
+    diag_builder << FixItHint::CreateReplacement(ParenRange, CastText);
   };
   // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
   switch (CastExpr->getCastKind()) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4534.11503.patch
Type: text/x-patch
Size: 3649 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140716/50a02a0b/attachment.bin>


More information about the cfe-commits mailing list