diff options
| author | Alexander Kornienko <alexfh@google.com> | 2014-07-16 13:38:48 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2014-07-16 13:38:48 +0000 |
| commit | 5f9cceb6852e2bab45c30d58667995113b54a39c (patch) | |
| tree | 5c95cd72c2cbb6093a2a27effcb8e1e0c7c43f3c | |
| parent | 2c03f1507ab125664d73fbffdd51dd92398b9cdd (diff) | |
Avoid adding redundant parens.
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: cfe-commits, sbenza
Differential Revision: http://reviews.llvm.org/D4534
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@213149 91177308-0d34-0410-b5e6-96231b3b80d8
| -rw-r--r-- | clang-tidy/google/AvoidCStyleCastsCheck.cpp | 22 | ||||
| -rw-r--r-- | test/clang-tidy/avoid-c-style-casts.cpp | 16 |
2 files changed, 28 insertions, 10 deletions
diff --git a/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 7f8a965a..4b8bf140 100644 --- a/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -56,6 +56,7 @@ bool pointedTypesAreEqual(QualType SourceType, QualType DestType) { } return SourceType.getUnqualifiedType() == DestType.getUnqualifiedType(); } + void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast"); @@ -85,15 +86,18 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { 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()) { diff --git a/test/clang-tidy/avoid-c-style-casts.cpp b/test/clang-tidy/avoid-c-style-casts.cpp index a81c2624..9ae63207 100644 --- a/test/clang-tidy/avoid-c-style-casts.cpp +++ b/test/clang-tidy/avoid-c-style-casts.cpp @@ -17,7 +17,7 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) { 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 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) { // 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); |
