Avoid optional::value() and expected::value()

They declare throwing std::bad_(optional|expected)_access,
which some static analyzers do not like in some situations.
And we do not handle these (and always check beforehand) anyway.
Use operator* and operator-> instead.

Change-Id: Ife8c54ff7e872019451163cb4eea7d2629c659ad
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Eike Ziller
2024-09-04 16:11:55 +02:00
parent 452d65b727
commit e92a8402a8
8 changed files with 23 additions and 22 deletions

View File

@@ -1376,7 +1376,7 @@ static QList<PluginSpec *> createCppPluginsFromArchive(const FilePath &path)
it.next(); it.next();
expected_str<PluginSpec *> spec = readCppPluginSpec(FilePath::fromUserInput(it.filePath())); expected_str<PluginSpec *> spec = readCppPluginSpec(FilePath::fromUserInput(it.filePath()));
if (spec) if (spec)
results.push_back(spec.value()); results.push_back(*spec);
} }
return results; return results;
} }

View File

@@ -73,7 +73,7 @@ expected_str<void> FileAccess::deployAndInit(
return make_unexpected( return make_unexpected(
QString("Could not determine OS on remote host: %1").arg(unameOs.error())); QString("Could not determine OS on remote host: %1").arg(unameOs.error()));
} }
Utils::expected_str<OsType> osType = osTypeFromString(unameOs.value()); Utils::expected_str<OsType> osType = osTypeFromString(*unameOs);
if (!osType) if (!osType)
return make_unexpected(osType.error()); return make_unexpected(osType.error());
@@ -85,7 +85,7 @@ expected_str<void> FileAccess::deployAndInit(
QString("Could not determine architecture on remote host: %1").arg(unameArch.error())); QString("Could not determine architecture on remote host: %1").arg(unameArch.error()));
} }
const Utils::expected_str<OsArch> osArch = osArchFromString(unameArch.value()); const Utils::expected_str<OsArch> osArch = osArchFromString(*unameArch);
if (!osArch) if (!osArch)
return make_unexpected(osArch.error()); return make_unexpected(osArch.error());

View File

@@ -91,7 +91,7 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
} }
if (testResult.result() == ResultType::TestStart && m_showDuration && testResult.duration()) { if (testResult.result() == ResultType::TestStart && m_showDuration && testResult.duration()) {
const QString txt = testResult.duration().value() + " ms"; const QString txt = *testResult.duration() + " ms";
QPen tmp = painter->pen(); QPen tmp = painter->pen();
painter->setPen(opt.palette.mid().color()); painter->setPen(opt.palette.mid().color());
painter->setClipRect(positions.durationArea()); painter->setClipRect(positions.durationArea());

View File

@@ -806,7 +806,7 @@ Group issueTableRecipe(const IssueListSearch &search, const IssueTableHandler &h
if (query.isEmpty()) if (query.isEmpty())
return {}; // TODO: Call handler with unexpected? return {}; // TODO: Call handler with unexpected?
const QUrl url = constructUrl(dd->m_currentProjectInfo.value().name, "issues", query); const QUrl url = constructUrl(dd->m_currentProjectInfo->name, "issues", query);
return fetchDataRecipe<Dto::IssueTableDto>(url, handler); return fetchDataRecipe<Dto::IssueTableDto>(url, handler);
} }
@@ -818,7 +818,7 @@ Group lineMarkerRecipe(const FilePath &filePath, const LineMarkerHandler &handle
const QString fileName = QString::fromUtf8(QUrl::toPercentEncoding(filePath.path())); const QString fileName = QString::fromUtf8(QUrl::toPercentEncoding(filePath.path()));
const QUrlQuery query({{"filename", fileName}, {"version", *dd->m_analysisVersion}}); const QUrlQuery query({{"filename", fileName}, {"version", *dd->m_analysisVersion}});
const QUrl url = constructUrl(dd->m_currentProjectInfo.value().name, "files", query); const QUrl url = constructUrl(dd->m_currentProjectInfo->name, "files", query);
return fetchDataRecipe<Dto::FileViewDto>(url, handler); return fetchDataRecipe<Dto::FileViewDto>(url, handler);
} }
@@ -830,7 +830,7 @@ Group fileSourceRecipe(const FilePath &filePath, const std::function<void(const
const QString fileName = QString::fromUtf8(QUrl::toPercentEncoding(filePath.path())); const QString fileName = QString::fromUtf8(QUrl::toPercentEncoding(filePath.path()));
const QUrlQuery query({{"filename", fileName}, {"version", *dd->m_analysisVersion}}); const QUrlQuery query({{"filename", fileName}, {"version", *dd->m_analysisVersion}});
const QUrl url = constructUrl(dd->m_currentProjectInfo.value().name, "sourcecode", query); const QUrl url = constructUrl(dd->m_currentProjectInfo->name, "sourcecode", query);
return fetchPlainTextRecipe(url, handler); return fetchPlainTextRecipe(url, handler);
} }
@@ -839,9 +839,10 @@ Group issueHtmlRecipe(const QString &issueId, const HtmlHandler &handler)
QTC_ASSERT(dd->m_currentProjectInfo, return {}); // TODO: Call handler with unexpected? QTC_ASSERT(dd->m_currentProjectInfo, return {}); // TODO: Call handler with unexpected?
QTC_ASSERT(dd->m_analysisVersion, return {}); // TODO: Call handler with unexpected? QTC_ASSERT(dd->m_analysisVersion, return {}); // TODO: Call handler with unexpected?
const QUrl url = constructUrl(dd->m_currentProjectInfo.value().name, const QUrl url = constructUrl(
QString("issues/" + issueId + "/properties/"), dd->m_currentProjectInfo->name,
{{"version", *dd->m_analysisVersion}}); QString("issues/" + issueId + "/properties/"),
{{"version", *dd->m_analysisVersion}});
return fetchHtmlRecipe(url, handler); return fetchHtmlRecipe(url, handler);
} }
@@ -894,7 +895,7 @@ void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
Group tableInfoRecipe(const QString &prefix, const TableInfoHandler &handler) Group tableInfoRecipe(const QString &prefix, const TableInfoHandler &handler)
{ {
const QUrlQuery query({{"kind", prefix}}); const QUrlQuery query({{"kind", prefix}});
const QUrl url = constructUrl(dd->m_currentProjectInfo.value().name, "issues_meta", query); const QUrl url = constructUrl(dd->m_currentProjectInfo->name, "issues_meta", query);
return fetchDataRecipe<Dto::TableInfoDto>(url, handler); return fetchDataRecipe<Dto::TableInfoDto>(url, handler);
} }

View File

@@ -142,7 +142,7 @@ static QIcon iconForSorted(std::optional<Qt::SortOrder> order)
if (!order) if (!order)
return unsorted; return unsorted;
return order.value() == Qt::AscendingOrder ? sortedAsc : sortedDesc; return *order == Qt::AscendingOrder ? sortedAsc : sortedDesc;
} }
static QIcon iconForFilter(bool isActive) static QIcon iconForFilter(bool isActive)
@@ -173,7 +173,7 @@ QList<QPair<int, Qt::SortOrder>> IssueHeaderView::currentSortColumns() const
{ {
QList<QPair<int, Qt::SortOrder>> result; QList<QPair<int, Qt::SortOrder>> result;
for (int i : m_currentSortIndexes) for (int i : m_currentSortIndexes)
result.append({i, m_columnInfoList.at(i).sortOrder.value()}); result.append({i, *m_columnInfoList.at(i).sortOrder});
return result; return result;
} }
@@ -183,7 +183,7 @@ QList<QPair<int, QString>> IssueHeaderView::currentFilterColumns() const
for (int i = 0, end = m_columnInfoList.size(); i < end; ++i) { for (int i = 0, end = m_columnInfoList.size(); i < end; ++i) {
const ColumnInfo ci = m_columnInfoList.at(i); const ColumnInfo ci = m_columnInfoList.at(i);
if (ci.filter.has_value()) if (ci.filter.has_value())
result.append({i, ci.filter.value()}); result.append({i, *ci.filter});
} }
return result; return result;
} }
@@ -225,7 +225,7 @@ void IssueHeaderView::mousePressEvent(QMouseEvent *event)
void IssueHeaderView::mouseReleaseEvent(QMouseEvent *event) void IssueHeaderView::mouseReleaseEvent(QMouseEvent *event)
{ {
bool dontSkip = !m_dragging && m_maybeToggle; bool dontSkip = !m_dragging && m_maybeToggle;
const int toggleMode = m_maybeToggle ? m_maybeToggle.value() : -1; const int toggleMode = m_maybeToggle ? *m_maybeToggle : -1;
bool withShift = m_withShift && event->modifiers() == Qt::ShiftModifier; bool withShift = m_withShift && event->modifiers() == Qt::ShiftModifier;
m_dragging = false; m_dragging = false;
m_maybeToggle.reset(); m_maybeToggle.reset();

View File

@@ -35,7 +35,7 @@ ClangFormatFile::ClangFormatFile(
0, 0,
"# yaml-language-server: " "# yaml-language-server: "
"$schema=https://json.schemastore.org/clang-format.json\n"); "$schema=https://json.schemastore.org/clang-format.json\n");
m_filePath.writeFileContents(fileContent.value()); m_filePath.writeFileContents(*fileContent);
} }
parseConfigurationFile(m_filePath, m_style); parseConfigurationFile(m_filePath, m_style);
return; return;

View File

@@ -856,7 +856,7 @@ QStringList DockerDevicePrivate::createMountArgs() const
mounts.append({m, m}); mounts.append({m, m});
if (cmdBridgePath && cmdBridgePath->isSameDevice(settings().dockerBinaryPath())) if (cmdBridgePath && cmdBridgePath->isSameDevice(settings().dockerBinaryPath()))
mounts.append({cmdBridgePath.value(), FilePath("/tmp/_qtc_cmdbridge")}); mounts.append({*cmdBridgePath, FilePath("/tmp/_qtc_cmdbridge")});
for (const MountPair &mi : mounts) { for (const MountPair &mi : mounts) {
if (isValidMountInfo(mi)) if (isValidMountInfo(mi))
@@ -1363,7 +1363,7 @@ expected_str<Environment> DockerDevicePrivate::environment()
} }
QTC_ASSERT(m_cachedEnviroment, return {}); QTC_ASSERT(m_cachedEnviroment, return {});
return m_cachedEnviroment.value(); return *m_cachedEnviroment;
} }
void DockerDevicePrivate::shutdown() void DockerDevicePrivate::shutdown()

View File

@@ -237,7 +237,7 @@ void tst_PluginSpec::locationAndPath()
Utils::expected_str<PluginSpec *> ps = readCppPluginSpec( Utils::expected_str<PluginSpec *> ps = readCppPluginSpec(
PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test"))); PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test")));
QVERIFY(ps); QVERIFY(ps);
CppPluginSpec *spec = static_cast<CppPluginSpec *>(ps.value()); CppPluginSpec *spec = static_cast<CppPluginSpec *>(*ps);
QCOMPARE(spec->location(), PLUGIN_DIR_PATH / "testplugin"); QCOMPARE(spec->location(), PLUGIN_DIR_PATH / "testplugin");
QCOMPARE(spec->filePath(), PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test"))); QCOMPARE(spec->filePath(), PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test")));
} }
@@ -292,7 +292,7 @@ void tst_PluginSpec::loadLibrary()
PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test"))); PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test")));
QVERIFY(ps); QVERIFY(ps);
CppPluginSpec *spec = static_cast<CppPluginSpec *>(ps.value()); CppPluginSpec *spec = static_cast<CppPluginSpec *>(*ps);
QCOMPARE(spec->errorString(), QString()); QCOMPARE(spec->errorString(), QString());
QVERIFY(spec->resolveDependencies({})); QVERIFY(spec->resolveDependencies({}));
@@ -311,7 +311,7 @@ void tst_PluginSpec::initializePlugin()
Utils::expected_str<PluginSpec *> ps = readCppPluginSpec( Utils::expected_str<PluginSpec *> ps = readCppPluginSpec(
PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test"))); PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test")));
QVERIFY(ps); QVERIFY(ps);
CppPluginSpec *spec = static_cast<CppPluginSpec *>(ps.value()); CppPluginSpec *spec = static_cast<CppPluginSpec *>(*ps);
QVERIFY(spec->resolveDependencies({})); QVERIFY(spec->resolveDependencies({}));
QVERIFY2(spec->loadLibrary(), qPrintable(spec->errorString())); QVERIFY2(spec->loadLibrary(), qPrintable(spec->errorString()));
bool isInitialized; bool isInitialized;
@@ -335,7 +335,7 @@ void tst_PluginSpec::initializeExtensions()
Utils::expected_str<PluginSpec *> ps = readCppPluginSpec( Utils::expected_str<PluginSpec *> ps = readCppPluginSpec(
PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test"))); PLUGIN_DIR_PATH / "testplugin" / libraryName(QLatin1String("test")));
QVERIFY(ps); QVERIFY(ps);
CppPluginSpec *spec = static_cast<CppPluginSpec *>(ps.value()); CppPluginSpec *spec = static_cast<CppPluginSpec *>(*ps);
QVERIFY(spec->resolveDependencies({})); QVERIFY(spec->resolveDependencies({}));
QVERIFY2(spec->loadLibrary(), qPrintable(spec->errorString())); QVERIFY2(spec->loadLibrary(), qPrintable(spec->errorString()));
bool isExtensionsInitialized; bool isExtensionsInitialized;