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:
@@ -682,6 +682,24 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
|
|||||||
return true;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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) 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() {
|
void testDynamicCast() {
|
||||||
|
|
||||||
struct S1 { virtual ~S1(); };
|
struct S1 { virtual ~S1(); };
|
||||||
|
@@ -195,7 +195,7 @@ public:
|
|||||||
public:
|
public:
|
||||||
FrameAreaWriteAccess(SwFrameAreaDefinition& rTarget) : SwRect(rTarget.getFrameArea()), mrTarget(rTarget) {}
|
FrameAreaWriteAccess(SwFrameAreaDefinition& rTarget) : SwRect(rTarget.getFrameArea()), mrTarget(rTarget) {}
|
||||||
~FrameAreaWriteAccess();
|
~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
|
// same helper for FramePrintArea
|
||||||
@@ -210,7 +210,7 @@ public:
|
|||||||
public:
|
public:
|
||||||
FramePrintAreaWriteAccess(SwFrameAreaDefinition& rTarget) : SwRect(rTarget.getFramePrintArea()), mrTarget(rTarget) {}
|
FramePrintAreaWriteAccess(SwFrameAreaDefinition& rTarget) : SwRect(rTarget.getFramePrintArea()), mrTarget(rTarget) {}
|
||||||
~FramePrintAreaWriteAccess();
|
~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
|
// RotateFlyFrame3 - Support for Transformations
|
||||||
|
Reference in New Issue
Block a user