summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2022-09-21 08:41:28 +0200
committerStephan Bergmann <sbergman@redhat.com>2022-09-21 15:09:26 +0200
commit70f8bddf87537e5ca28758a1f59bda23c0963501 (patch)
tree3311218ad652b421fa0ce7f4e521e085dbb77f8c /compilerplugins
parentc20afb30e724361b76257fec0f3f7b9b90f8d58c (diff)
Extend loplugin:redundantcast to some suspicious reinterpret_cast
Change-Id: I5534939bfbea67d216a17891722a683c53621d36 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/140303 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/redundantcast.cxx18
-rw-r--r--compilerplugins/clang/test/redundantcast.cxx7
2 files changed, 25 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx
index b95a742cb2a2..8d7de5365d89 100644
--- a/compilerplugins/clang/redundantcast.cxx
+++ b/compilerplugins/clang/redundantcast.cxx
@@ -682,6 +682,24 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
return true;
}
}
+ if (auto const t1 = expr->getSubExpr()->getType()->getAs<clang::PointerType>()) {
+ if (auto const t2 = expr->getType()->getAs<clang::PointerType>()) {
+ if (auto const d1 = t1->getPointeeCXXRecordDecl()) {
+ if (auto const d2 = t2->getPointeeCXXRecordDecl()) {
+ if (d1->hasDefinition() && d1->isDerivedFrom(d2)) {
+ report(
+ DiagnosticsEngine::Warning,
+ "suspicious reinterpret_cast from derived %0 to base %1, maybe this was"
+ " meant to be a static_cast",
+ expr->getExprLoc())
+ << expr->getSubExprAsWritten()->getType() << expr->getTypeAsWritten()
+ << expr->getSourceRange();
+ return true;
+ }
+ }
+ }
+ }
+ }
return true;
}
diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx
index 7a102cca5d0a..da570ab7de8f 100644
--- a/compilerplugins/clang/test/redundantcast.cxx
+++ b/compilerplugins/clang/test/redundantcast.cxx
@@ -339,6 +339,13 @@ void testReinterpretConstCast() {
(void) reinterpret_cast<std::size_t>((const_cast<int const *>(&n))); // expected-error-re {{redundant const_cast from 'int *' to 'const int *' within reinterpret_cast to fundamental type 'std::size_t' (aka 'unsigned {{.+}}') [loplugin:redundantcast]}}
}
+void testSuspiciousReinterpretCast() {
+ D * p;
+ // expected-error@+1 {{suspicious reinterpret_cast from derived 'D *' to base 'S *', maybe this was meant to be a static_cast [loplugin:redundantcast]}}
+ (void) reinterpret_cast<S *>(p);
+ (void) reinterpret_cast<sal_uIntPtr>(p); // expected no error
+}
+
void testDynamicCast() {
struct S1 { virtual ~S1(); };