diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2015-04-13 12:35:57 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2015-04-13 12:36:36 +0200 |
commit | 373a9b9bb62c50072c06e28fd932afb6e71ba687 (patch) | |
tree | dc6b72474110a4311d7ab57d42dc4bda5c221e4b /compilerplugins | |
parent | 3c4fd27392bd153e043eeb60907eeded48c60cf2 (diff) |
loplugin:redundantcast: redundant const_cast followed by implicit upcast
Change-Id: I58297ba336d96358eb0683684bbd763870ef56cb
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/redundantcast.cxx | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index ee731e2938fc..3abf66a853b3 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -156,6 +156,48 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) { } } break; + case CK_DerivedToBase: + case CK_UncheckedDerivedToBase: + if (expr->getType()->isPointerType()) { + Expr const * e = expr->getSubExpr()->IgnoreParenImpCasts(); + while (isa<CXXConstCastExpr>(e)) { + auto cc = dyn_cast<CXXConstCastExpr>(e); + if (expr->getType()->getAs<PointerType>()->getPointeeType() + .isAtLeastAsQualifiedAs( + cc->getSubExpr()->getType() + ->getAs<PointerType>()->getPointeeType())) + { + report( + DiagnosticsEngine::Warning, + ("redundant const_cast from %0 to %1, result is" + " ultimately implictly cast to %2"), + cc->getExprLoc()) + << cc->getSubExprAsWritten()->getType() << cc->getType() + << expr->getType() << expr->getSourceRange(); + } + e = cc->getSubExpr()->IgnoreParenImpCasts(); + } + } else if (expr->getType()->isReferenceType()) { + Expr const * e = expr->getSubExpr()->IgnoreParenImpCasts(); + while (isa<CXXConstCastExpr>(e)) { + auto cc = dyn_cast<CXXConstCastExpr>(e); + if (expr->getType()->getAs<ReferenceType>()->getPointeeType() + .isAtLeastAsQualifiedAs( + cc->getSubExpr()->getType() + ->getAs<ReferenceType>()->getPointeeType())) + { + report( + DiagnosticsEngine::Warning, + ("redundant const_cast from %0 to %1, result is" + " ultimately implictly cast to %2"), + cc->getExprLoc()) + << cc->getSubExprAsWritten()->getType() << cc->getType() + << expr->getType() << expr->getSourceRange(); + } + e = cc->getSubExpr()->IgnoreParenImpCasts(); + } + } + break; default: break; } |