summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/redundantcast.cxx
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-10-20 20:51:50 +0200
committerStephan Bergmann <sbergman@redhat.com>2017-10-21 15:44:11 +0200
commitead920a48aa8c35075fdb980b9d213ff1c580dd1 (patch)
treebb1628fd293f03b22920bc74e89657320da6442e /compilerplugins/clang/redundantcast.cxx
parenttdf#105000, related tdf#87538: Make new color names translatable (diff)
downloadcore-ead920a48aa8c35075fdb980b9d213ff1c580dd1.tar.gz
core-ead920a48aa8c35075fdb980b9d213ff1c580dd1.zip
loplugin:redundantcast handle dynamic_cast
Change-Id: I7855c76e820efce96778b1c19ec71dffcc4b4abb Reviewed-on: https://gerrit.libreoffice.org/43621 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins/clang/redundantcast.cxx')
-rw-r--r--compilerplugins/clang/redundantcast.cxx24
1 files changed, 24 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx
index 30914a460dc4..c28878e9e536 100644
--- a/compilerplugins/clang/redundantcast.cxx
+++ b/compilerplugins/clang/redundantcast.cxx
@@ -128,6 +128,8 @@ public:
bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr);
+ bool VisitCXXDynamicCastExpr(CXXDynamicCastExpr const * expr);
+
bool VisitCallExpr(CallExpr const * expr);
bool VisitCXXDeleteExpr(CXXDeleteExpr const * expr);
@@ -676,6 +678,28 @@ bool RedundantCast::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * exp
return true;
}
+bool RedundantCast::VisitCXXDynamicCastExpr(CXXDynamicCastExpr const * expr) {
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ // ignore dynamic_cast<T1>(static_cast<T1>(void*)), it's necessary
+ if (auto subStaticCast = dyn_cast<CXXStaticCastExpr>(expr->getSubExpr())) {
+ if (loplugin::TypeCheck(subStaticCast->getSubExpr()->getType()).Pointer().Void())
+ return true;
+ }
+ // so far this only deals with dynamic casting from T to T
+ auto const sub = compat::getSubExprAsWritten(expr);
+ auto const t1 = expr->getTypeAsWritten();
+ auto const t2 = sub->getType();
+ if (t1.getCanonicalType() != t2.getCanonicalType())
+ return true;
+ report(
+ DiagnosticsEngine::Warning,
+ "redundant dynamic cast from %0 to %1", expr->getExprLoc())
+ << t2 << t1 << expr->getSourceRange();
+ return true;
+}
+
bool RedundantCast::VisitCallExpr(CallExpr const * expr) {
if (ignoreLocation(expr)) {
return true;