From e9f726db844ec52c96b10eb5328e09208b49a0e7 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 11 Jan 2025 12:27:24 +0200 Subject: [PATCH] examples: check return value of QFile::open() calls QFile::open() is marked [[nodiscard]]. Change-Id: I0fa884b329a47c4ad59f40d66b5fbc38f3a5648e Reviewed-by: Friedemann Kleint --- examples/qtconcurrent/wordcount/main.cpp | 12 ++++++++++-- .../itemviews/editabletreemodel/mainwindow.cpp | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/qtconcurrent/wordcount/main.cpp b/examples/qtconcurrent/wordcount/main.cpp index b157aabe47c..4369f7995d6 100644 --- a/examples/qtconcurrent/wordcount/main.cpp +++ b/examples/qtconcurrent/wordcount/main.cpp @@ -20,7 +20,11 @@ WordCount singleThreadedWordCount(const QStringList &files) WordCount wordCount; for (const QString &file : files) { QFile f(file); - f.open(QIODevice::ReadOnly); + if (!f.open(QIODevice::ReadOnly)) { + qWarning().noquote() << "Failed to open file:" << QDir::toNativeSeparators(file) + << ":" << f.errorString(); + return {}; + } QTextStream textStream(&f); while (!textStream.atEnd()) { const auto words = textStream.readLine().split(' ', Qt::SkipEmptyParts); @@ -37,7 +41,11 @@ WordCount singleThreadedWordCount(const QStringList &files) WordCount countWords(const QString &file) { QFile f(file); - f.open(QIODevice::ReadOnly); + if (!f.open(QIODevice::ReadOnly)) { + qWarning().noquote() << "Failed to open file:" << QDir::toNativeSeparators(file) + << ":" << f.errorString(); + return {}; + } QTextStream textStream(&f); WordCount wordCount; diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp index 75d7eb7e73c..147434b9926 100644 --- a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp +++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp @@ -17,7 +17,10 @@ MainWindow::MainWindow(QWidget *parent) const QStringList headers({tr("Title"), tr("Description")}); QFile file(":/default.txt"_L1); - file.open(QIODevice::ReadOnly | QIODevice::Text); + const bool res = file.open(QIODevice::ReadOnly | QIODevice::Text); + Q_ASSERT_X(res, Q_FUNC_INFO, "Failed to open ':/default.txt'"); + if (!res) + return; auto *model = new TreeModel(headers, QString::fromUtf8(file.readAll()), this); file.close();