Fix warnings about unused return values

Change-Id: I9682c6b5a0bba400050e91fe89b2883f7bb6465f
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2025-01-20 16:27:27 +01:00
parent 2dbd2fb674
commit ea7b16cb70
34 changed files with 84 additions and 74 deletions

View File

@@ -949,7 +949,8 @@ bool DesktopDeviceFileAccess::ensureExistingFile(const FilePath &filePath) const
QFile f(filePath.path());
if (f.exists())
return true;
f.open(QFile::WriteOnly);
if (!f.open(QFile::WriteOnly))
return false;
f.close();
return f.exists();
}

View File

@@ -558,7 +558,7 @@ MimeType MimeDatabasePrivate::mimeTypeForFileNameAndData(const QString &fileName
return matchOnContent(device);
QFile fallbackFile(fileName);
fallbackFile.open(QIODevice::ReadOnly); // error handling: matchOnContent() will check isOpen()
(void) fallbackFile.open(QIODevice::ReadOnly); // error handling: matchOnContent() will check isOpen()
return matchOnContent(&fallbackFile);
}

View File

@@ -608,7 +608,8 @@ bool AndroidBuildApkWidget::isOpenSslLibsIncluded()
Utils::FilePath projectPath = appProjectFilePath();
const QString searchStr = openSslIncludeFileContent(projectPath);
QFile projectFile(projectPath.toFSPathString());
projectFile.open(QIODevice::ReadOnly);
if (!projectFile.open(QIODevice::ReadOnly))
return false;
QTextStream textStream(&projectFile);
QString fileContent = textStream.readAll();
projectFile.close();

View File

@@ -54,7 +54,7 @@ void CMakeModificationFile::read()
void CMakeModificationFile::write() const
{
QFile out(nativePath());
out.open(QIODevice::WriteOnly | QIODevice::Text);
QTC_CHECK(out.open(QIODevice::WriteOnly | QIODevice::Text));
QTextStream outStream(&out);
for (QString &line : defaultModificationFile()) {

View File

@@ -161,7 +161,7 @@ void CocoCMakeSettings::writeToolchainFile(const QString &internalPath)
const Utils::FilePath projectDirectory = buildConfig()->project()->projectDirectory();
QFile internalFile{internalPath};
internalFile.open(QIODeviceBase::ReadOnly);
QTC_CHECK(internalFile.open(QIODeviceBase::ReadOnly));
const QByteArray internalContent = internalFile.readAll();
const QString fileName = Utils::FilePath::fromString(internalPath).fileName();
@@ -170,7 +170,7 @@ void CocoCMakeSettings::writeToolchainFile(const QString &internalPath)
if (toolchainPath.exists()) {
QFile currentFile{toolchainNative};
currentFile.open(QIODeviceBase::ReadOnly);
QTC_CHECK(currentFile.open(QIODeviceBase::ReadOnly));
QByteArray currentContent = currentFile.readAll();
if (internalContent == currentContent)
@@ -181,7 +181,7 @@ void CocoCMakeSettings::writeToolchainFile(const QString &internalPath)
logSilently(Tr::tr("Write file %1").arg(maybeQuote(toolchainNative)));
QFile out{toolchainNative};
out.open(QIODeviceBase::WriteOnly);
QTC_CHECK(out.open(QIODeviceBase::WriteOnly));
out.write(internalContent);
out.close();
}

View File

@@ -49,7 +49,7 @@ QStringList ModificationFile::defaultModificationFile() const
QStringList ModificationFile::contentOf(const Utils::FilePath &filePath) const
{
QFile resource(filePath.nativePath());
resource.open(QIODevice::ReadOnly | QIODevice::Text);
QTC_CHECK(resource.open(QIODevice::ReadOnly | QIODevice::Text));
QTextStream inStream(&resource);
QStringList result;

View File

@@ -63,7 +63,7 @@ void QMakeFeatureFile::read()
void QMakeFeatureFile::write() const
{
QFile out(nativePath());
out.open(QIODevice::WriteOnly | QIODevice::Text);
QTC_CHECK(out.open(QIODevice::WriteOnly | QIODevice::Text));
QTextStream outStream(&out);
for (QString &line : defaultModificationFile()) {

View File

@@ -4,19 +4,16 @@
#include "compileroptionsbuilder_test.h"
#include "compileroptionsbuilder.h"
#include "projectinfo.h"
#include "projectpart.h"
#include <projectexplorer/headerpath.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/temporarydirectory.h>
#include <QtTest>
#include <memory>
using namespace ProjectExplorer;
namespace CppEditor::Internal {
@@ -28,7 +25,7 @@ public:
const ProjectPart &finalize()
{
QFile pchFile(pchFileNativePath());
pchFile.open(QIODevice::WriteOnly);
QTC_CHECK(pchFile.open(QIODevice::WriteOnly));
RawProjectPart rpp;
rpp.setPreCompiledHeaders({pchFileNativePath()});
rpp.setMacros({Macro{"projectFoo", "projectBar"}});

View File

@@ -272,13 +272,12 @@ namespace CppEditor::Internal {
static inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }
static void createTempFile(const FilePath &filePath)
static bool createTempFile(const FilePath &filePath)
{
QString fileName = filePath.toUrlishString();
QFile file(fileName);
QDir(QFileInfo(fileName).absolutePath()).mkpath(_("."));
file.open(QFile::WriteOnly);
file.close();
return file.open(QFile::WriteOnly);
}
static QString baseTestDir()
@@ -309,8 +308,8 @@ void HeaderSourceTest::test()
const QDir path = QDir(temporaryDir.path() + QLatin1Char('/') + _(QTest::currentDataTag()));
const FilePath sourcePath = FilePath::fromString(path.absoluteFilePath(sourceFileName));
const FilePath headerPath = FilePath::fromString(path.absoluteFilePath(headerFileName));
createTempFile(sourcePath);
createTempFile(headerPath);
QVERIFY2(createTempFile(sourcePath), qPrintable(sourcePath.toUserOutput()));
QVERIFY2(createTempFile(headerPath), qPrintable(headerPath.toUserOutput()));
bool wasHeader;
clearHeaderSourceCache();

View File

@@ -113,7 +113,7 @@ void DebuggerRunTool::startCoreFileSetupIfNeededAndContinueStartup()
{
TemporaryFile tmp("tmpcore-XXXXXX");
tmp.open();
QTC_CHECK(tmp.open());
d->m_tempCoreFilePath = FilePath::fromString(tmp.fileName());
}
@@ -139,7 +139,7 @@ void DebuggerRunTool::startCoreFileSetupIfNeededAndContinueStartup()
if (coreFile.endsWith(".gz")) {
d->m_tempCoreFile.setFileName(d->m_tempCoreFilePath.path());
d->m_tempCoreFile.open(QFile::WriteOnly);
QTC_CHECK(d->m_tempCoreFile.open(QFile::WriteOnly));
connect(&d->m_coreUnpackProcess, &Process::readyReadStandardOutput, this, [this] {
d->m_tempCoreFile.write(d->m_coreUnpackProcess.readAllRawStandardOutput());
});

View File

@@ -2855,7 +2855,7 @@ static void handleShowModuleSymbols(const DebuggerResponse &response,
if (response.resultClass == ResultDone) {
Symbols symbols;
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QTC_CHECK(file.open(QIODevice::ReadOnly));
// Object file /opt/dev/qt/lib/libQtNetworkMyns.so.4:
// [ 0] A 0x16bd64 _DYNAMIC moc_qudpsocket.cpp
// [12] S 0xe94680 _ZN4myns5QFileC1Ev section .plt myns::QFile::QFile()

View File

@@ -6345,7 +6345,8 @@ bool FakeVimHandler::Private::handleExWriteCommand(const ExCommand &cmd)
}
// Check result by reading back.
QFile file3(fileName);
file3.open(QIODevice::ReadOnly);
if (!file3.open(QIODevice::ReadOnly))
return false;
QByteArray ba = file3.readAll();
showMessage(MessageInfo, Tr::tr("\"%1\" %2 %3L, %4C written.")
.arg(fileName).arg(exists ? QString(" ") : Tr::tr(" [New] "))
@@ -6375,7 +6376,8 @@ bool FakeVimHandler::Private::handleExReadCommand(const ExCommand &cmd)
m_currentFileName = replaceTildeWithHome(cmd.args);
QFile file(m_currentFileName);
file.open(QIODevice::ReadOnly);
if (!file.open(QIODevice::ReadOnly))
return false;
QTextStream ts(&file);
QString data = ts.readAll();
insertText(data);

View File

@@ -84,7 +84,7 @@ StdIOClientInterface::StdIOClientInterface()
: m_logFile("lspclient.XXXXXX.log")
{
m_logFile.setAutoRemove(false);
m_logFile.open();
QTC_CHECK(m_logFile.open());
}
StdIOClientInterface::~StdIOClientInterface()

View File

@@ -206,7 +206,10 @@ static Group installRecipe(
{
QTemporaryFile tempFile(QDir::tempPath() + "/XXXXXX" + ext);
tempFile.setAutoRemove(false);
tempFile.open();
if (!tempFile.open()) {
emitResult(Tr::tr("Cannot open temporary file."));
return SetupResult::StopWithError;
}
(*storage).setFileName(tempFile.fileName());
}

View File

@@ -165,7 +165,7 @@ public:
m_readCallback = {};
QFile f(":/lua/scripts/ilua.lua");
f.open(QIODevice::ReadOnly);
QTC_CHECK(f.open(QIODevice::ReadOnly));
const auto ilua = QString::fromUtf8(f.readAll());
m_luaState = runScript(ilua, "ilua.lua", [this](sol::state &lua) {
lua["print"] = [this](sol::variadic_args va) {

View File

@@ -74,7 +74,8 @@ QString McuPackageXmlVersionDetector::parseVersion(const FilePath &packagePath)
const auto files = QDir(packagePath.toUrlishString(), m_filePattern).entryInfoList();
for (const auto &xmlFile : files) {
QFile sdkXmlFile = QFile(xmlFile.absoluteFilePath());
sdkXmlFile.open(QFile::OpenModeFlag::ReadOnly);
if (!sdkXmlFile.open(QFile::OpenModeFlag::ReadOnly))
return {};
QXmlStreamReader xmlReader(&sdkXmlFile);
while (xmlReader.readNext()) {
if (xmlReader.name() == m_versionElement) {

View File

@@ -47,11 +47,8 @@ template<typename T>
inline std::optional<T> load(const QString &jsonFile)
{
QFile js(jsonFile);
js.open(QIODevice::ReadOnly | QIODevice::Text);
if (js.isOpen()) {
auto data = js.readAll();
return load<T>(QJsonDocument::fromJson(data));
}
if (js.open(QIODevice::ReadOnly | QIODevice::Text))
return load<T>(QJsonDocument::fromJson(js.readAll()));
return std::nullopt;
}

View File

@@ -82,7 +82,7 @@ private slots:
{
// With unconfigured project
QTemporaryFile introFile;
introFile.open();
QVERIFY(introFile.open());
const auto tool = findMeson();
QVERIFY(tool.has_value());
const MesonToolWrapper meson("name", *tool);

View File

@@ -245,7 +245,7 @@ QVariantMap JsonWizardFactory::loadDefaultValues(const QString &fileName)
+ "\n");
if (current.pathAppended(fileName).exists()) {
QFile configFile(current.pathAppended(fileName).toUrlishString());
configFile.open(QIODevice::ReadOnly);
QTC_CHECK(configFile.open(QIODevice::ReadOnly));
QJsonParseError error;
const QByteArray fileData = configFile.readAll();
const QJsonDocument json = QJsonDocument::fromJson(fileData, &error);

View File

@@ -183,7 +183,7 @@ void QmlProfilerDetailsRewriterTest::seedRewriter()
lPaths, m_modelManager, false);
QFile file(filename);
file.open(QFile::ReadOnly | QFile::Text);
QTC_CHECK(file.open(QFile::ReadOnly | QFile::Text));
const QString content = QString::fromUtf8(file.readAll());
file.close();

View File

@@ -267,7 +267,7 @@ void CMakeGenerator::readQmlDir(const Utils::FilePath &filePath, NodePtr &node)
node->type = Node::Type::Module;
QFile f(filePath.toUrlishString());
f.open(QIODevice::ReadOnly);
QTC_CHECK(f.open(QIODevice::ReadOnly));
QTextStream stream(&f);
Utils::FilePath dir = filePath.parentDir();

View File

@@ -101,7 +101,8 @@ CMakeWriter::Version CMakeWriter::versionFromIgnoreFile(const Utils::FilePath &p
QString CMakeWriter::readTemplate(const QString &templatePath)
{
QFile templatefile(templatePath);
templatefile.open(QIODevice::ReadOnly | QIODevice::Text);
if (!templatefile.open(QIODevice::ReadOnly | QIODevice::Text))
return {};
QTextStream stream(&templatefile);
QString content = stream.readAll();
templatefile.close();

View File

@@ -65,8 +65,7 @@ bool QmlProjectFileGenerator::execute()
.arg(contentEntry, imageEntry, jsEntry, assetEntry, importPaths);
QFile file(m_targetFile.toUrlishString());
file.open(QIODevice::WriteOnly);
if (!file.isOpen())
if (!file.open(QIODevice::WriteOnly))
return false;
file.reset();

View File

@@ -46,7 +46,7 @@ public:
setWindowTitle(Tr::tr("Record Screen"));
StyleHelper::setPanelWidget(this);
m_recordFile.open();
QTC_CHECK(m_recordFile.open());
m_recordWidget = new RecordWidget(FilePath::fromString(m_recordFile.fileName()));
m_cropAndTrimStatusWidget = new CropAndTrimWidget;

View File

@@ -592,7 +592,7 @@ void SquishTools::setupAndStartRecorder()
args << "--suitedir" << m_suitePath.toUserOutput();
Utils::TemporaryFile tmp("squishsnippetfile-XXXXXX"); // quick and dirty
tmp.open();
QTC_CHECK(tmp.open());
m_currentRecorderSnippetFile = Utils::FilePath::fromUserInput(tmp.fileName());
args << "--outfile" << m_currentRecorderSnippetFile.toUserOutput();
tmp.close();

View File

@@ -383,7 +383,8 @@ static expected_str<void> loadXFCE4ColorScheme(const FilePath &path)
arr->replace(';', ',');
QTemporaryFile f;
f.open();
if (!f.open())
return make_unexpected(f.errorString());
f.write(*arr);
f.close();

View File

@@ -189,7 +189,7 @@ void tst_CppSelectionChanger::initTestCase()
// Read cpp file contents into QTextDocument and CppEditor::Document::Ptr.
QString fileName(SRCDIR "/testCppFile.cpp");
QFile file(fileName);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text), qPrintable(fileName));
QTextStream s(&file);
cppFileString = s.readAll();
file.close();

View File

@@ -63,7 +63,7 @@ static bool generateEnvironmentSettings(Utils::Environment &env,
QString tempOutFile;
QTemporaryFile* pVarsTempFile = new QTemporaryFile(QDir::tempPath() + "/XXXXXX.txt");
pVarsTempFile->setAutoRemove(false);
pVarsTempFile->open();
QTC_CHECK(pVarsTempFile->open());
pVarsTempFile->close();
tempOutFile = pVarsTempFile->fileName();
delete pVarsTempFile;
@@ -1372,7 +1372,7 @@ void tst_Dumpers::cleanup()
{
if (!t->buildTemp.autoRemove()) {
QFile logger(t->buildPath + "/input.txt");
logger.open(QIODevice::ReadWrite);
QTC_CHECK(logger.open(QIODevice::ReadWrite));
logger.write(t->input.toUtf8());
}
delete t;
@@ -1839,7 +1839,7 @@ void tst_Dumpers::dumper()
} else if (m_debuggerEngine == LldbEngine) {
QFile fullLldb(t->buildPath + "/lldbcommand.txt");
fullLldb.setPermissions(QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ReadOther);
fullLldb.open(QIODevice::WriteOnly);
QVERIFY2(fullLldb.open(QIODevice::WriteOnly), qPrintable(fullLldb.fileName()));
fullLldb.write((exe + ' ' + args.join(' ') + '\n').toUtf8());
#ifdef Q_OS_WIN
@@ -1894,7 +1894,7 @@ void tst_Dumpers::dumper()
if (keepTemp()) {
QFile logger(t->buildPath + "/output.txt");
logger.open(QIODevice::ReadWrite);
QVERIFY2(logger.open(QIODevice::ReadWrite), qPrintable(logger.fileName()));
logger.write("=== STDOUT ===\n");
logger.write(output);
logger.write("\n=== STDERR ===\n");

View File

@@ -1508,7 +1508,7 @@ void tst_Json::fromJsonErrors()
void tst_Json::fromBinary()
{
QFile file(testDataDir + QLatin1String("/test.json"));
file.open(QFile::ReadOnly);
QVERIFY2(file.open(QFile::ReadOnly), qPrintable(file.fileName()));
std::string testJson = file.readAll().data();
JsonDocument doc = JsonDocument::fromJson(testJson);
@@ -1524,7 +1524,7 @@ void tst_Json::fromBinary()
// b1file.close();
QFile bfile(testDataDir + QLatin1String("/test.bjson"));
bfile.open(QFile::ReadOnly);
QVERIFY2(bfile.open(QFile::ReadOnly), qPrintable(bfile.fileName()));
std::string binary = bfile.readAll().toStdString();
JsonDocument bdoc = JsonDocument::fromBinaryData(binary);
@@ -1722,7 +1722,7 @@ void tst_Json::parseDuplicateKeys()
void tst_Json::testParser()
{
QFile file(testDataDir + QLatin1String("/test.json"));
file.open(QFile::ReadOnly);
QVERIFY2(file.open(QFile::ReadOnly), qPrintable(file.fileName()));
std::string testJson = file.readAll().data();
JsonDocument doc = JsonDocument::fromJson(testJson);
@@ -1828,7 +1828,7 @@ void tst_Json::validation()
QFile file2(testDataDir + QLatin1String("/test3.json"));
file2.open(QFile::ReadOnly);
QVERIFY2(file2.open(QFile::ReadOnly), qPrintable(file2.fileName()));
testJson = file2.readAll().data();
QVERIFY(!testJson.empty());
@@ -2207,7 +2207,7 @@ void tst_Json::arrayEquals()
void tst_Json::bom()
{
QFile file(testDataDir + QLatin1String("/bom.json"));
file.open(QFile::ReadOnly);
QVERIFY2(file.open(QFile::ReadOnly), qPrintable(file.fileName()));
std::string json = file.readAll().data();
// Import json document into a JsonDocument

View File

@@ -107,7 +107,7 @@ void tst_Check::test()
Snapshot snapshot = mm->snapshot();
Document::MutablePtr doc = Document::create(pathPath, Dialect::Qml);
QFile file(doc->fileName().toUrlishString());
file.open(QFile::ReadOnly | QFile::Text);
QVERIFY2(file.open(QFile::ReadOnly | QFile::Text), qPrintable(file.fileName()));
doc->setSource(QString::fromUtf8(file.readAll()));
file.close();
doc->parse();

View File

@@ -26,6 +26,8 @@
#include <extensionsystem/pluginmanager.h>
#include <utils/filepath.h>
#include <optional>
using namespace QmlJS;
using namespace QmlJS::AST;
using namespace QmlJS::StaticAnalysis;
@@ -49,9 +51,10 @@ struct TestData
const int staticMessages;
};
static TestData testData(const QString &path) {
static std::optional<TestData> testData(const QString &path) {
QFile file(path);
file.open(QFile::ReadOnly | QFile::Text);
if (!file.open(QFile::ReadOnly | QFile::Text))
return {};
const QString content = QString::fromUtf8(file.readAll());
file.close();
@@ -129,10 +132,11 @@ void tst_Dependencies::test()
ModelManagerInterface::importScan(ModelManagerInterface::workingCopy(), lPaths,
ModelManagerInterface::instance(), false);
ModelManagerInterface::instance()->test_joinAllThreads();
TestData data = testData(filename);
Document::MutablePtr doc = data.doc;
int nExpectedSemanticMessages = data.semanticMessages;
int nExpectedStaticMessages = data.staticMessages;
const auto data = testData(filename);
QVERIFY(data);
Document::MutablePtr doc = data->doc;
int nExpectedSemanticMessages = data->semanticMessages;
int nExpectedStaticMessages = data->staticMessages;
QVERIFY(!doc->source().isEmpty());
Snapshot snapshot = modelManager->snapshot();

View File

@@ -68,7 +68,7 @@ void tst_Reformatter::test()
Document::MutablePtr doc = Document::create(fPath, ModelManagerInterface::guessLanguageOfFile(fPath));
QFile file(doc->fileName().toUrlishString());
file.open(QFile::ReadOnly | QFile::Text);
QVERIFY2(file.open(QFile::ReadOnly | QFile::Text), qPrintable(file.fileName()));
QString source = QString::fromUtf8(file.readAll());
doc->setSource(source);
file.close();

View File

@@ -135,12 +135,15 @@ private:
QString exeExt;
};
static void touch(const QDir &dir, const QString &filename, bool fill, bool executable = false)
static bool touch(const QDir &dir, const QString &filename, bool fill, bool executable = false)
{
QFile file(dir.absoluteFilePath(filename));
file.open(QIODevice::WriteOnly);
if (executable)
file.setPermissions(file.permissions() | QFileDevice::ExeUser);
if (!file.open(QIODevice::WriteOnly))
return false;
if (executable) {
if (!file.setPermissions(file.permissions() | QFileDevice::ExeUser))
return false;
}
if (fill) {
QRandomGenerator *random = QRandomGenerator::global();
@@ -148,6 +151,7 @@ static void touch(const QDir &dir, const QString &filename, bool fill, bool exec
file.write(QString::number(random->generate(), 16).toUtf8());
}
file.close();
return true;
}
void tst_filepath::initTestCase()
@@ -160,13 +164,13 @@ void tst_filepath::initTestCase()
dir.mkpath("a/x/y/z");
dir.mkpath("a/b/x/y/z");
dir.mkpath("x/y/z");
touch(dir, "a/b/c/d/file1.txt", false);
touch(dir, "a/x/y/z/file2.txt", false);
touch(dir, "a/file3.txt", false);
touch(dir, "x/y/file4.txt", false);
QVERIFY(touch(dir, "a/b/c/d/file1.txt", false));
QVERIFY(touch(dir, "a/x/y/z/file2.txt", false));
QVERIFY(touch(dir, "a/file3.txt", false));
QVERIFY(touch(dir, "x/y/file4.txt", false));
// initialize test for tst_filepath::asyncLocalCopy()
touch(dir, "x/y/fileToCopy.txt", true);
QVERIFY(touch(dir, "x/y/fileToCopy.txt", true));
// initialize test for tst_filepath::searchIn()
#ifdef Q_OS_WIN
@@ -175,8 +179,8 @@ void tst_filepath::initTestCase()
dir.mkpath("s/1");
dir.mkpath("s/2");
touch(dir, "s/1/testexe" + exeExt, false, true);
touch(dir, "s/2/testexe" + exeExt, false, true);
QVERIFY(touch(dir, "s/1/testexe" + exeExt, false, true));
QVERIFY(touch(dir, "s/2/testexe" + exeExt, false, true));
}
void tst_filepath::searchInWithFilter()
@@ -1550,7 +1554,7 @@ void tst_filepath::isSameFile_data()
<< false;
QDir dir(tempDir.path());
touch(dir, "target-file", false);
QVERIFY(touch(dir, "target-file", false));
QFile file(dir.absoluteFilePath("target-file"));
if (file.link(dir.absoluteFilePath("source-file"))) {

View File

@@ -120,7 +120,7 @@ void tst_CodeSize::cleanup()
{
if (!t->buildTemp.autoRemove()) {
QFile logger(t->buildPath + QLatin1String("/input.txt"));
logger.open(QIODevice::ReadWrite);
QVERIFY2(logger.open(QIODevice::ReadWrite), qPrintable(logger.fileName()));
logger.write(t->input);
}
delete t;