compilerplugins/clang/redundantcast.cxx      |   18 ++++++++++++++++++
 compilerplugins/clang/test/redundantcast.cxx |    7 +++++++
 sw/source/core/inc/frame.hxx                 |    4 ++--
 3 files changed, 27 insertions(+), 2 deletions(-)

New commits:
commit 70f8bddf87537e5ca28758a1f59bda23c0963501
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Wed Sep 21 08:41:28 2022 +0200
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Wed Sep 21 15:09:26 2022 +0200

    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 <sberg...@redhat.com>

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(); };
diff --git a/sw/source/core/inc/frame.hxx b/sw/source/core/inc/frame.hxx
index 7cb9dffc27bc..e5b4c4c9bf5c 100644
--- a/sw/source/core/inc/frame.hxx
+++ b/sw/source/core/inc/frame.hxx
@@ -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

Reply via email to