From e3762c5f6e46b408f4a06319734bbcb6e4fcf111 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 12 Feb 2025 14:38:28 +1000 Subject: [PATCH] Avoid coverity uncaught exception warnings The warning is a false positive, since we were already checking that the optional has a value before accessing it. But we can avoid the exception altogether (and gain some performance) by using the std::optional dereference operator, which DOES not throw and which relies on the previous has_value check to avoid undefined behavior. --- src/core/elevation/qgsabstractprofilesurfacegenerator.cpp | 2 +- src/core/labeling/qgstextlabelfeature.h | 2 +- src/core/pointcloud/qgspointclouddataprovider.cpp | 2 +- src/core/settings/qgssettingsproxy.h | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/elevation/qgsabstractprofilesurfacegenerator.cpp b/src/core/elevation/qgsabstractprofilesurfacegenerator.cpp index 9ec3a9f9b0b..d23e2f33ddc 100644 --- a/src/core/elevation/qgsabstractprofilesurfacegenerator.cpp +++ b/src/core/elevation/qgsabstractprofilesurfacegenerator.cpp @@ -232,7 +232,7 @@ QVector QgsAbstractProfileSurfaceResults::identify( c prevElevation = it.value(); } if ( result.has_value() ) - return {result.value()}; + return {*result}; else return {}; } diff --git a/src/core/labeling/qgstextlabelfeature.h b/src/core/labeling/qgstextlabelfeature.h index e5fb5ffab4d..88337cead4d 100644 --- a/src/core/labeling/qgstextlabelfeature.h +++ b/src/core/labeling/qgstextlabelfeature.h @@ -83,7 +83,7 @@ class CORE_EXPORT QgsTextLabelFeature : public QgsLabelFeature * \see setTextMetrics() * \since QGIS 3.20 */ - const QgsPrecalculatedTextMetrics *textMetrics() const { return mTextMetrics.has_value() ? &mTextMetrics.value() : nullptr; } + const QgsPrecalculatedTextMetrics *textMetrics() const { return mTextMetrics.has_value() ? &( *mTextMetrics ) : nullptr; } /** * Sets additional text \a metrics required for curved label placement. diff --git a/src/core/pointcloud/qgspointclouddataprovider.cpp b/src/core/pointcloud/qgspointclouddataprovider.cpp index ddcba70b738..08bcc8ab7d9 100644 --- a/src/core/pointcloud/qgspointclouddataprovider.cpp +++ b/src/core/pointcloud/qgspointclouddataprovider.cpp @@ -264,7 +264,7 @@ struct MapIndexedPointCloudNode // here we check the flag set in header to determine if we need to // parse the time as GPS week time or GPS adjusted standard time // however often times the flag is set wrong, so we determine if the value is bigger than the maximum amount of seconds in week then it has to be adjusted standard time - if ( copcTimeFlag.value() || pointAttr[QStringLiteral( "GpsTime" )].toDouble() > numberOfSecsInWeek ) + if ( *copcTimeFlag || pointAttr[QStringLiteral( "GpsTime" )].toDouble() > numberOfSecsInWeek ) { const QString utcTime = gpsBaseTime.addSecs( static_cast( pointAttr[QStringLiteral( "GpsTime" )].toDouble() + 1e9 ) ).toString( Qt::ISODate ); pointAttr[ QStringLiteral( "GpsTime (raw)" )] = pointAttr[QStringLiteral( "GpsTime" )]; diff --git a/src/core/settings/qgssettingsproxy.h b/src/core/settings/qgssettingsproxy.h index 32ef08dfbfc..32c68871b40 100644 --- a/src/core/settings/qgssettingsproxy.h +++ b/src/core/settings/qgssettingsproxy.h @@ -48,7 +48,7 @@ class CORE_EXPORT QgsSettingsProxy */ QgsSettings *operator->() { - return mOwnedSettings.has_value() ? &( mOwnedSettings.value() ) : mNonOwnedSettings; + return mOwnedSettings.has_value() ? &( *mOwnedSettings ) : mNonOwnedSettings; } /** @@ -56,7 +56,7 @@ class CORE_EXPORT QgsSettingsProxy */ QgsSettings &operator* () { - return mOwnedSettings.has_value() ? mOwnedSettings.value() : *mNonOwnedSettings; + return mOwnedSettings.has_value() ? *mOwnedSettings : *mNonOwnedSettings; } private: