summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2022-11-23 07:37:33 +0100
committerStephan Bergmann <sbergman@redhat.com>2022-11-23 08:48:46 +0100
commit7255d21f812b7333c83b4548e2b5b102adb606a9 (patch)
treea1cb1917f41e474671731837cb1665d21d13b3dc /compilerplugins
parent27eb23369f9e250e71cfea8524a71f962e93b8fd (diff)
Fix loplugin:redundantcast check for same-type dynamic_cast
...when the target type is a reference type and the source and target type have different cv qualifiers. (And fix the fallout. And make the tests cover that somewhat more exhaustively; and while at it also test that the plugin can cope with dynamic_cast to void pointers, which is the only legitimate case where a dynamic_cast can involve types that are not (pointers or references to) non-class types.) Change-Id: Ia568ddb5dbc4a84c275db172791c345d95964857 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143133 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/redundantcast.cxx1
-rw-r--r--compilerplugins/clang/test/redundantcast.cxx24
2 files changed, 25 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx
index 984b5f003108..80485e0e9bd6 100644
--- a/compilerplugins/clang/redundantcast.cxx
+++ b/compilerplugins/clang/redundantcast.cxx
@@ -901,6 +901,7 @@ bool RedundantCast::VisitCXXDynamicCastExpr(CXXDynamicCastExpr const * expr) {
{
// casting from 'T&' to 'const T&' is redundant, so compare without the qualifiers
qt1 = qt1->getPointeeType().getUnqualifiedType();
+ qt2 = qt2.getUnqualifiedType();
if (qt1 == qt2)
{
report(
diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx
index 228ccfc25dc2..0c1087ab32af 100644
--- a/compilerplugins/clang/test/redundantcast.cxx
+++ b/compilerplugins/clang/test/redundantcast.cxx
@@ -356,13 +356,37 @@ void testDynamicCast() {
S2 * s2 = nullptr;
S3 * s3 = nullptr;
+ (void) dynamic_cast<void *>(s1);
+ (void) dynamic_cast<void const *>(s1);
(void) dynamic_cast<S2 *>(s1);
+ (void) dynamic_cast<S2 &>(*s1);
(void) dynamic_cast<S1 *>(s2); // expected-error {{redundant dynamic upcast from 'S2 *' to 'S1 *' [loplugin:redundantcast]}}
+ (void) dynamic_cast<S1 &>(*s2); // expected-error {{redundant dynamic upcast from 'S2' to 'S1 &' [loplugin:redundantcast]}}
(void) dynamic_cast<S2 *>(s2); // expected-error {{redundant dynamic cast from 'S2 *' to 'S2 *' [loplugin:redundantcast]}}
+ (void) dynamic_cast<S2 &>(*s2); // expected-error {{redundant dynamic cast from 'S2' to 'S2 &' [loplugin:redundantcast]}}
(void) dynamic_cast<S3 *>(s2);
+ (void) dynamic_cast<S3 &>(*s2);
(void) dynamic_cast<const S2 *>(s2); // expected-error {{redundant dynamic cast from 'S2 *' to 'const S2 *' [loplugin:redundantcast]}}
+ (void) dynamic_cast<const S2 &>(*s2); // expected-error {{redundant dynamic cast from 'S2' to 'const S2 &' [loplugin:redundantcast]}}
(void) dynamic_cast<S1 *>(s3); // expected-error {{redundant dynamic upcast from 'S3 *' to 'S1 *' [loplugin:redundantcast]}}
(void) dynamic_cast<S1&>(*s3); // expected-error {{redundant dynamic upcast from 'S3' to 'S1 &' [loplugin:redundantcast]}}
+
+ S1 const * c1 = nullptr;
+ S2 const * c2 = nullptr;
+ S3 const * c3 = nullptr;
+
+ (void) dynamic_cast<void const *>(c1);
+ (void) dynamic_cast<S2 const *>(c1);
+ (void) dynamic_cast<S2 const &>(*c1);
+ (void) dynamic_cast<S1 const *>(c2); // expected-error {{redundant dynamic upcast from 'const S2 *' to 'const S1 *' [loplugin:redundantcast]}}
+ (void) dynamic_cast<S1 const &>(*c2); // expected-error {{redundant dynamic upcast from 'const S2' to 'const S1 &' [loplugin:redundantcast]}}
+
+ (void) dynamic_cast<S2 const *>(c2); // expected-error {{redundant dynamic cast from 'const S2 *' to 'const S2 *' [loplugin:redundantcast]}}
+ (void) dynamic_cast<S2 const &>(*c2); // expected-error {{redundant dynamic cast from 'const S2' to 'const S2 &' [loplugin:redundantcast]}}
+ (void) dynamic_cast<S3 const *>(c2);
+ (void) dynamic_cast<S3 const &>(*c2);
+ (void) dynamic_cast<S1 const *>(c3); // expected-error {{redundant dynamic upcast from 'const S3 *' to 'const S1 *' [loplugin:redundantcast]}}
+ (void) dynamic_cast<S1 const&>(*c3); // expected-error {{redundant dynamic upcast from 'const S3' to 'const S1 &' [loplugin:redundantcast]}}
}
void overload(int);