aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSergio Martins <smartins@kde.org>2019-05-27 14:11:02 +0100
committerSergio Martins <smartins@kde.org>2019-05-27 14:12:10 +0100
commit4918d8efba9b20b273a15ab2dd215a768ff884f9 (patch)
tree41436cd1461d2280f02b2310e114a519cdbc0618 /src
parent3e684e046eefff815d62eebd85d087a25944c22f (diff)
unneeded-cast: Improve warning message for unneeded qobject_cast
Instead of saying that no cast to base is needed, simply say that no qobject_cast is needed. Since a static_cast might still be needed. This is the case for the ternary operator. BUG: 407981
Diffstat (limited to 'src')
-rw-r--r--src/checks/manuallevel/unneeded-cast.cpp16
-rw-r--r--src/checks/manuallevel/unneeded-cast.h2
2 files changed, 14 insertions, 4 deletions
diff --git a/src/checks/manuallevel/unneeded-cast.cpp b/src/checks/manuallevel/unneeded-cast.cpp
index 46c90b98..1b7aae67 100644
--- a/src/checks/manuallevel/unneeded-cast.cpp
+++ b/src/checks/manuallevel/unneeded-cast.cpp
@@ -105,10 +105,10 @@ bool UnneededCast::handleQObjectCast(Stmt *stm)
if (!clazy::is_qobject_cast(stm, &castTo, &castFrom))
return false;
- return maybeWarn(stm, castFrom, castTo);
+ return maybeWarn(stm, castFrom, castTo, /*isQObjectCast=*/ true);
}
-bool UnneededCast::maybeWarn(Stmt *stmt, CXXRecordDecl *castFrom, CXXRecordDecl *castTo)
+bool UnneededCast::maybeWarn(Stmt *stmt, CXXRecordDecl *castFrom, CXXRecordDecl *castTo, bool isQObjectCast)
{
castFrom = castFrom->getCanonicalDecl();
castTo = castTo->getCanonicalDecl();
@@ -117,7 +117,17 @@ bool UnneededCast::maybeWarn(Stmt *stmt, CXXRecordDecl *castFrom, CXXRecordDecl
emitWarning(clazy::getLocStart(stmt), "Casting to itself");
return true;
} else if (clazy::derivesFrom(/*child=*/ castFrom, castTo)) {
- emitWarning(clazy::getLocStart(stmt), "explicitly casting to base is unnecessary");
+ if (isQObjectCast) {
+ const bool isTernaryOperator = clazy::getFirstParentOfType<ConditionalOperator>(m_context->parentMap, stmt) != nullptr;
+ if (isTernaryOperator) {
+ emitWarning(clazy::getLocStart(stmt), "use static_cast instead of qobject_cast");
+ } else {
+ emitWarning(clazy::getLocStart(stmt), "explicitly casting to base is unnecessary");
+ }
+ } else {
+ emitWarning(clazy::getLocStart(stmt), "explicitly casting to base is unnecessary");
+ }
+
return true;
}
diff --git a/src/checks/manuallevel/unneeded-cast.h b/src/checks/manuallevel/unneeded-cast.h
index 1e40b22c..53d6e4b3 100644
--- a/src/checks/manuallevel/unneeded-cast.h
+++ b/src/checks/manuallevel/unneeded-cast.h
@@ -53,7 +53,7 @@ public:
private:
bool handleNamedCast(clang::CXXNamedCastExpr *);
bool handleQObjectCast(clang::Stmt *);
- bool maybeWarn(clang::Stmt *, clang::CXXRecordDecl *from, clang::CXXRecordDecl *to);
+ bool maybeWarn(clang::Stmt *, clang::CXXRecordDecl *from, clang::CXXRecordDecl *to, bool isQObjectCast = false);
};
#endif