diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy')
| -rw-r--r-- | clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 093250d541f..20440fe618c 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -68,19 +68,30 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { if (CastExpr->getTypeAsWritten()->isVoidType()) return; - QualType SourceType = - CastExpr->getSubExprAsWritten()->getType().getCanonicalType(); - QualType DestType = CastExpr->getTypeAsWritten().getCanonicalType(); + QualType SourceType = CastExpr->getSubExprAsWritten()->getType(); + QualType DestType = CastExpr->getTypeAsWritten(); if (SourceType == DestType) { - diag(CastExpr->getLocStart(), "Redundant cast to the same type.") + diag(CastExpr->getLocStart(), "redundant cast to the same type") << FixItHint::CreateRemoval(ParenRange); return; } + SourceType = SourceType.getCanonicalType(); + DestType = DestType.getCanonicalType(); + if (SourceType == DestType) { + diag(CastExpr->getLocStart(), + "possibly redundant cast between typedefs of the same type"); + return; + } + // The rest of this check is only relevant to C++. if (!Result.Context->getLangOpts().CPlusPlus) return; + // Ignore code inside extern "C" {} blocks. + if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context) + .empty()) + return; // Leave type spelling exactly as it was (unlike // getTypeAsWritten().getAsString() which would spell enum types 'enum X'). @@ -94,7 +105,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0"); auto ReplaceWithCast = [&](StringRef CastType) { - diag_builder << ("Use " + CastType + ".").str(); + diag_builder << ("Use " + CastType).str(); const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts(); std::string CastText = (CastType + "<" + DestTypeString + ">").str(); @@ -108,6 +119,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { } diag_builder << FixItHint::CreateReplacement(ParenRange, CastText); }; + // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics. switch (CastExpr->getCastKind()) { case CK_NoOp: @@ -145,7 +157,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { break; } - diag_builder << "Use static_cast/const_cast/reinterpret_cast."; + diag_builder << "Use static_cast/const_cast/reinterpret_cast"; } } // namespace readability |

