Take array-to-pointer decay into account in loplugin:redundantcast
Change-Id: I3c5bace180605d1a72a74feb1bf9f9b184a16d83 Reviewed-on: https://gerrit.libreoffice.org/63545 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
@@ -358,6 +358,28 @@ bool RedundantCast::VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) {
|
||||
<< expr->getSourceRange();
|
||||
return true;
|
||||
}
|
||||
if (auto const impl = dyn_cast<ImplicitCastExpr>(expr->getSubExpr())) {
|
||||
if (impl->getCastKind() == CK_ArrayToPointerDecay && impl->getType() == t2)
|
||||
//TODO: instead of exact QualType match, allow some variation?
|
||||
{
|
||||
auto const fn = handler.getMainFileName();
|
||||
if (!(loplugin::isSamePathname(
|
||||
fn, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx")
|
||||
|| loplugin::isSamePathname(
|
||||
fn, SRCDIR "/sal/qa/rtl/strings/test_ostring_stringliterals.cxx")
|
||||
|| loplugin::isSamePathname(
|
||||
fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx")
|
||||
|| loplugin::isSamePathname(
|
||||
fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_stringliterals.cxx")))
|
||||
{
|
||||
report(
|
||||
DiagnosticsEngine::Warning, "redundant static_cast from %0 to %1",
|
||||
expr->getExprLoc())
|
||||
<< expr->getSubExprAsWritten()->getType() << t2 << expr->getSourceRange();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
auto const t3 = expr->getType();
|
||||
auto const c1 = t1.getCanonicalType();
|
||||
auto const c3 = t3.getCanonicalType();
|
||||
@@ -471,6 +493,30 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
|
||||
if (ignoreLocation(expr)) {
|
||||
return true;
|
||||
}
|
||||
if (auto const sub = dyn_cast<ImplicitCastExpr>(expr->getSubExpr())) {
|
||||
if (sub->getCastKind() == CK_ArrayToPointerDecay && sub->getType() == expr->getType())
|
||||
//TODO: instead of exact QualType match, allow some variation?
|
||||
{
|
||||
if (loplugin::TypeCheck(sub->getType()).Pointer().Const().Char()) {
|
||||
if (auto const lit = dyn_cast<clang::StringLiteral>(expr->getSubExprAsWritten())) {
|
||||
if (lit->getKind() == clang::StringLiteral::UTF8) {
|
||||
// Don't warn about
|
||||
//
|
||||
// redundant_cast<char const *>(u8"...")
|
||||
//
|
||||
// in pre-C++2a code:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
report(
|
||||
DiagnosticsEngine::Warning, "redundant reinterpret_cast from %0 to %1",
|
||||
expr->getExprLoc())
|
||||
<< expr->getSubExprAsWritten()->getType() << expr->getTypeAsWritten()
|
||||
<< expr->getSourceRange();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (expr->getSubExpr()->getType()->isVoidPointerType()) {
|
||||
auto t = expr->getType()->getAs<clang::PointerType>();
|
||||
if (t == nullptr || !t->getPointeeType()->isObjectType()) {
|
||||
|
@@ -386,6 +386,12 @@ void testIntermediaryStaticCast() {
|
||||
d = int(d) + 1.0; // expected-error {{suspicious functional cast from 'double' to 'int', result is implicitly cast to 'double' [loplugin:redundantcast]}}
|
||||
};
|
||||
|
||||
void testArrayDecay() {
|
||||
(void) static_cast<char const *>(""); // expected-error {{redundant static_cast from 'const char [1]' to 'const char *' [loplugin:redundantcast]}}
|
||||
(void) reinterpret_cast<char const *>(""); // expected-error {{redundant reinterpret_cast from 'const char [1]' to 'const char *' [loplugin:redundantcast]}}
|
||||
(void) reinterpret_cast<char const *>(u8"");
|
||||
}
|
||||
|
||||
int main() {
|
||||
testConstCast();
|
||||
testStaticCast();
|
||||
@@ -395,6 +401,7 @@ int main() {
|
||||
testReinterpretConstCast();
|
||||
testDynamicCast();
|
||||
testIntermediaryStaticCast();
|
||||
testArrayDecay();
|
||||
}
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|
||||
|
Reference in New Issue
Block a user