chart2/source/view/axes/VCartesianCoordinateSystem.cxx | 2 - chart2/source/view/axes/VPolarCoordinateSystem.cxx | 2 - compilerplugins/clang/redundantcast.cxx | 21 +++++++++++++++++ compilerplugins/clang/test/redundantcast.cxx | 1 sfx2/inc/preventduplicateinteraction.hxx | 2 - sw/source/uibase/uno/SwXDocumentSettings.cxx | 14 +++++------ 6 files changed, 32 insertions(+), 10 deletions(-)
New commits: commit 6bde58fa51d3e546488471ee17760b7442533142 Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Mon Oct 17 10:57:29 2022 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Mon Oct 17 12:00:28 2022 +0200 loplugin:redundantcast look for redundant dynamic_cast<T&> Change-Id: If5a8e74b7ede80b782b584b4f62ec1b8713fd86d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/141445 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/chart2/source/view/axes/VCartesianCoordinateSystem.cxx b/chart2/source/view/axes/VCartesianCoordinateSystem.cxx index 9f765223bf4d..dbcf89775ab5 100644 --- a/chart2/source/view/axes/VCartesianCoordinateSystem.cxx +++ b/chart2/source/view/axes/VCartesianCoordinateSystem.cxx @@ -110,7 +110,7 @@ void VCartesianCoordinateSystem::createVAxisList( // note: using xChartDoc itself as XNumberFormatsSupplier would cause // a leak from VCartesianAxis due to cyclic reference uno::Reference<util::XNumberFormatsSupplier> const xNumberFormatsSupplier( - dynamic_cast<ChartModel&>(*xChartDoc).getNumberFormatsSupplier()); + xChartDoc->getNumberFormatsSupplier()); m_aAxisMap.clear(); diff --git a/chart2/source/view/axes/VPolarCoordinateSystem.cxx b/chart2/source/view/axes/VPolarCoordinateSystem.cxx index 600447dd19d5..235237e266f9 100644 --- a/chart2/source/view/axes/VPolarCoordinateSystem.cxx +++ b/chart2/source/view/axes/VPolarCoordinateSystem.cxx @@ -78,7 +78,7 @@ void VPolarCoordinateSystem::createVAxisList( // note: using xChartDoc itself as XNumberFormatsSupplier would cause // a leak from VPolarAxis due to cyclic reference uno::Reference<util::XNumberFormatsSupplier> const xNumberFormatsSupplier( - dynamic_cast<ChartModel&>(*xChartDoc).getNumberFormatsSupplier()); + xChartDoc->getNumberFormatsSupplier()); m_aAxisMap.clear(); sal_Int32 nDimensionCount = m_xCooSysModel->getDimension(); diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index 8d7de5365d89..984b5f003108 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -897,6 +897,27 @@ bool RedundantCast::VisitCXXDynamicCastExpr(CXXDynamicCastExpr const * expr) { return true; } } + else if (qt1->isReferenceType() && qt2->isRecordType()) + { + // casting from 'T&' to 'const T&' is redundant, so compare without the qualifiers + qt1 = qt1->getPointeeType().getUnqualifiedType(); + if (qt1 == qt2) + { + report( + DiagnosticsEngine::Warning, + "redundant dynamic cast from %0 to %1", expr->getExprLoc()) + << t2 << t1 << expr->getSourceRange(); + return true; + } + if (qt1->getAsCXXRecordDecl() && qt2->getAsCXXRecordDecl()->isDerivedFrom(qt1->getAsCXXRecordDecl())) + { + report( + DiagnosticsEngine::Warning, + "redundant dynamic upcast from %0 to %1", expr->getExprLoc()) + << t2 << t1 << expr->getSourceRange(); + return true; + } + } return true; } diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index da570ab7de8f..228ccfc25dc2 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -362,6 +362,7 @@ void testDynamicCast() { (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<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]}} } void overload(int); diff --git a/sfx2/inc/preventduplicateinteraction.hxx b/sfx2/inc/preventduplicateinteraction.hxx index 1798dd5f5bd4..b4015d0f7040 100644 --- a/sfx2/inc/preventduplicateinteraction.hxx +++ b/sfx2/inc/preventduplicateinteraction.hxx @@ -72,7 +72,7 @@ private: if (!m_xWin) return; SolarMutexGuard aSolarGuard; - closedialogs(dynamic_cast<SystemWindow&>(*m_xWin), false); + closedialogs(*m_xWin, false); } public: diff --git a/sw/source/uibase/uno/SwXDocumentSettings.cxx b/sw/source/uibase/uno/SwXDocumentSettings.cxx index e29a25a49c0c..f22afc97d38b 100644 --- a/sw/source/uibase/uno/SwXDocumentSettings.cxx +++ b/sw/source/uibase/uno/SwXDocumentSettings.cxx @@ -307,14 +307,14 @@ Any SAL_CALL SwXDocumentSettings::queryInterface( const Type& rType ) { return ::cppu::queryInterface(rType, // OWeakObject interfaces - &dynamic_cast<XInterface&>(dynamic_cast<OWeakObject&>(*this)), - &dynamic_cast<XWeak&>(*this), + static_cast<XInterface*>(static_cast<OWeakObject*>(this)), + static_cast<XWeak*>(this), // my own interfaces - &dynamic_cast<XPropertySet&>(*this), - &dynamic_cast<XPropertyState&>(*this), - &dynamic_cast<XMultiPropertySet&>(*this), - &dynamic_cast<XServiceInfo&>(*this), - &dynamic_cast<XTypeProvider&>(*this)); + static_cast<XPropertySet*>(this), + static_cast<XPropertyState*>(this), + static_cast<XMultiPropertySet*>(this), + static_cast<XServiceInfo*>(this), + static_cast<XTypeProvider*>(this)); } void SwXDocumentSettings::acquire () noexcept