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>
This commit is contained in:
Stephan Bergmann
2022-09-21 08:41:28 +02:00
parent c20afb30e7
commit 70f8bddf87
3 changed files with 27 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -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(); };

View File

@@ -195,7 +195,7 @@ public:
public:
FrameAreaWriteAccess(SwFrameAreaDefinition& rTarget) : SwRect(rTarget.getFrameArea()), mrTarget(rTarget) {}
~FrameAreaWriteAccess();
void setSwRect(const SwRect& rNew) { *reinterpret_cast< SwRect* >(this) = rNew; }
void setSwRect(const SwRect& rNew) { *static_cast< SwRect* >(this) = rNew; }
};
// same helper for FramePrintArea
@@ -210,7 +210,7 @@ public:
public:
FramePrintAreaWriteAccess(SwFrameAreaDefinition& rTarget) : SwRect(rTarget.getFramePrintArea()), mrTarget(rTarget) {}
~FramePrintAreaWriteAccess();
void setSwRect(const SwRect& rNew) { *reinterpret_cast< SwRect* >(this) = rNew; }
void setSwRect(const SwRect& rNew) { *static_cast< SwRect* >(this) = rNew; }
};
// RotateFlyFrame3 - Support for Transformations