From 3ee289e40d1a7d3582cb1a3a304e681d2085bb82 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 29 May 2023 00:55:59 +0300 Subject: [PATCH] Misc.: fix narrowing conversion warnings Using: - range-for and iterator-based loops - QList constructor that takes a pair of iterators Found by using -Wshorten-64-to-32 clang compiler flag, or adding that flag to the flags clangd uses, e.g. adding this to clangd's config file (see https://clangd.llvm.org/config): CompileFlags: Add: [-Wshorten-64-to-32] Pick-to: 6.6 6.5 Change-Id: I13ae65e09ab59a59f9e5c189ea27e4e16527df2d Reviewed-by: Richard Moe Gustavsen --- src/gui/itemmodels/qfilesystemmodel.cpp | 25 +++++++--------- src/gui/kernel/qplatformscreen.cpp | 5 ++-- src/gui/kernel/qsimpledrag.cpp | 7 +++-- src/widgets/dialogs/qfiledialog.cpp | 38 ++++++++++++------------- src/widgets/dialogs/qsidebar.cpp | 32 +++++++++------------ src/widgets/dialogs/qwizard.cpp | 21 +++++++------- src/widgets/kernel/qaction_widgets.cpp | 5 ++-- 7 files changed, 63 insertions(+), 70 deletions(-) diff --git a/src/gui/itemmodels/qfilesystemmodel.cpp b/src/gui/itemmodels/qfilesystemmodel.cpp index 9c5c21f303..31480bb52d 100644 --- a/src/gui/itemmodels/qfilesystemmodel.cpp +++ b/src/gui/itemmodels/qfilesystemmodel.cpp @@ -1106,11 +1106,10 @@ void QFileSystemModelPrivate::sortChildren(int column, const QModelIndex &parent indexNode->visibleChildren.clear(); //No more dirty item we reset our internal dirty index indexNode->dirtyChildrenIndex = -1; - const int numValues = values.size(); - indexNode->visibleChildren.reserve(numValues); - for (int i = 0; i < numValues; ++i) { - indexNode->visibleChildren.append(values.at(i)->fileName); - values.at(i)->isVisible = true; + indexNode->visibleChildren.reserve(values.size()); + for (QFileSystemNode *node : std::as_const(values)) { + indexNode->visibleChildren.append(node->fileName); + node->isVisible = true; } if (!disableRecursiveSort) { @@ -1136,10 +1135,8 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order) emit layoutAboutToBeChanged(); QModelIndexList oldList = persistentIndexList(); QList> oldNodes; - const int nodeCount = oldList.size(); - oldNodes.reserve(nodeCount); - for (int i = 0; i < nodeCount; ++i) { - const QModelIndex &oldNode = oldList.at(i); + oldNodes.reserve(oldList.size()); + for (const QModelIndex &oldNode : oldList) { QPair pair(d->node(oldNode), oldNode.column()); oldNodes.append(pair); } @@ -1153,12 +1150,10 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order) d->sortOrder = order; QModelIndexList newList; - const int numOldNodes = oldNodes.size(); - newList.reserve(numOldNodes); - for (int i = 0; i < numOldNodes; ++i) { - const QPair &oldNode = oldNodes.at(i); - newList.append(d->index(oldNode.first, oldNode.second)); - } + newList.reserve(oldNodes.size()); + for (const auto &[node, col]: std::as_const(oldNodes)) + newList.append(d->index(node, col)); + changePersistentIndexList(oldList, newList); emit layoutChanged(); } diff --git a/src/gui/kernel/qplatformscreen.cpp b/src/gui/kernel/qplatformscreen.cpp index 1b1631bf3f..0369a0b12a 100644 --- a/src/gui/kernel/qplatformscreen.cpp +++ b/src/gui/kernel/qplatformscreen.cpp @@ -54,8 +54,9 @@ QPixmap QPlatformScreen::grabWindow(WId window, int x, int y, int width, int hei QWindow *QPlatformScreen::topLevelAt(const QPoint & pos) const { const QWindowList list = QGuiApplication::topLevelWindows(); - for (int i = list.size()-1; i >= 0; --i) { - QWindow *w = list[i]; + const auto crend = list.crend(); + for (auto it = list.crbegin(); it != crend; ++it) { + QWindow *w = *it; if (w->isVisible() && QHighDpi::toNativePixels(w->geometry(), w).contains(pos)) return w; } diff --git a/src/gui/kernel/qsimpledrag.cpp b/src/gui/kernel/qsimpledrag.cpp index 77cbfd2b96..a90e8dd33c 100644 --- a/src/gui/kernel/qsimpledrag.cpp +++ b/src/gui/kernel/qsimpledrag.cpp @@ -34,9 +34,10 @@ Q_LOGGING_CATEGORY(lcDnd, "qt.gui.dnd") static QWindow* topLevelAt(const QPoint &pos) { - QWindowList list = QGuiApplication::topLevelWindows(); - for (int i = list.size()-1; i >= 0; --i) { - QWindow *w = list.at(i); + const QWindowList list = QGuiApplication::topLevelWindows(); + const auto crend = list.crend(); + for (auto it = list.crbegin(); it != crend; ++it) { + QWindow *w = *it; if (w->isVisible() && w->handle() && w->geometry().contains(pos) && !qobject_cast(w)) return w; } diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index ffec02d938..18395e928a 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1240,12 +1240,10 @@ QStringList QFileDialogPrivate::addDefaultSuffixToFiles(const QStringList &files QList QFileDialogPrivate::addDefaultSuffixToUrls(const QList &urlsToFix) const { QList urls; - const int numUrlsToFix = urlsToFix.size(); - urls.reserve(numUrlsToFix); - for (int i = 0; i < numUrlsToFix; ++i) { - QUrl url = urlsToFix.at(i); - // if the filename has no suffix, add the default suffix - const QString defaultSuffix = options->defaultSuffix(); + urls.reserve(urlsToFix.size()); + // if the filename has no suffix, add the default suffix + const QString defaultSuffix = options->defaultSuffix(); + for (QUrl url : urlsToFix) { if (!defaultSuffix.isEmpty()) { const QString urlPath = url.path(); const auto idx = urlPath.lastIndexOf(u'/'); @@ -1353,11 +1351,10 @@ QStringList qt_strip_filters(const QStringList &filters) #if QT_CONFIG(regularexpression) QStringList strippedFilters; static const QRegularExpression r(QString::fromLatin1(QPlatformFileDialogHelper::filterRegExp)); - const int numFilters = filters.size(); - strippedFilters.reserve(numFilters); - for (int i = 0; i < numFilters; ++i) { + strippedFilters.reserve(filters.size()); + for (const QString &filter : filters) { QString filterName; - auto match = r.match(filters[i]); + auto match = r.match(filter); if (match.hasMatch()) filterName = match.captured(1); strippedFilters.append(filterName.simplified()); @@ -1392,11 +1389,10 @@ void QFileDialog::setNameFilters(const QStringList &filters) { Q_D(QFileDialog); QStringList cleanedFilters; - const int numFilters = filters.size(); - cleanedFilters.reserve(numFilters); - for (int i = 0; i < numFilters; ++i) { - cleanedFilters << filters[i].simplified(); - } + cleanedFilters.reserve(filters.size()); + for (const QString &filter : filters) + cleanedFilters << filter.simplified(); + d->options->setNameFilters(cleanedFilters); if (!d->usingWidgets()) @@ -3355,8 +3351,10 @@ void QFileDialogPrivate::navigate(HistoryItem &historyItem) | QItemSelectionModel::Rows; selectionModel->select(historyItem.selection.constFirst(), flags | QItemSelectionModel::Clear | QItemSelectionModel::Current); - for (int i = 1, size = historyItem.selection.size(); i < size; ++i) - selectionModel->select(historyItem.selection.at(i), flags); + auto it = historyItem.selection.cbegin() + 1; + const auto end = historyItem.selection.cend(); + for (; it != end; ++it) + selectionModel->select(*it, flags); view->scrollTo(historyItem.selection.constFirst()); } @@ -3532,9 +3530,9 @@ void QFileDialogPrivate::_q_deleteCurrent() if (model->isReadOnly()) return; - QModelIndexList list = qFileDialogUi->listView->selectionModel()->selectedRows(); - for (int i = list.size() - 1; i >= 0; --i) { - QPersistentModelIndex index = list.at(i); + const QModelIndexList list = qFileDialogUi->listView->selectionModel()->selectedRows(); + for (auto it = list.crbegin(), end = list.crend(); it != end; ++it) { + QPersistentModelIndex index = *it; if (index == qFileDialogUi->listView->rootIndex()) continue; diff --git a/src/widgets/dialogs/qsidebar.cpp b/src/widgets/dialogs/qsidebar.cpp index b52c7e8c9e..75e43a2bed 100644 --- a/src/widgets/dialogs/qsidebar.cpp +++ b/src/widgets/dialogs/qsidebar.cpp @@ -211,8 +211,9 @@ void QUrlModel::addUrls(const QList &list, int row, bool move) if (row == -1) row = rowCount(); row = qMin(row, rowCount()); - for (int i = list.size() - 1; i >= 0; --i) { - QUrl url = list.at(i); + const auto rend = list.crend(); + for (auto it = list.crbegin(); it != rend; ++it) { + QUrl url = *it; if (!url.isValid() || url.scheme() != "file"_L1) continue; //this makes sure the url is clean @@ -312,13 +313,11 @@ void QUrlModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &botto void QUrlModel::layoutChanged() { QStringList paths; - const int numPaths = watching.size(); - paths.reserve(numPaths); - for (int i = 0; i < numPaths; ++i) - paths.append(watching.at(i).path); + paths.reserve(watching.size()); + for (const WatchItem &item : std::as_const(watching)) + paths.append(item.path); watching.clear(); - for (int i = 0; i < numPaths; ++i) { - QString path = paths.at(i); + for (const auto &path : paths) { QModelIndex newIndex = fileSystemModel->index(path); watching.append({newIndex, path}); if (newIndex.isValid()) @@ -429,16 +428,13 @@ void QSidebar::showContextMenu(const QPoint &position) */ void QSidebar::removeEntry() { - QList idxs = selectionModel()->selectedIndexes(); - QList indexes; - const int numIndexes = idxs.size(); - indexes.reserve(numIndexes); - for (int i = 0; i < numIndexes; i++) - indexes.append(idxs.at(i)); - - for (int i = 0; i < numIndexes; ++i) { - if (!indexes.at(i).data(QUrlModel::UrlRole).toUrl().path().isEmpty()) - model()->removeRow(indexes.at(i).row()); + const QList idxs = selectionModel()->selectedIndexes(); + // Create a list of QPersistentModelIndex as the removeRow() calls below could + // invalidate the indexes in "idxs" + const QList persIndexes(idxs.cbegin(), idxs.cend()); + for (const QPersistentModelIndex &persistent : persIndexes) { + if (!persistent.data(QUrlModel::UrlRole).toUrl().path().isEmpty()) + model()->removeRow(persistent.row()); } } diff --git a/src/widgets/dialogs/qwizard.cpp b/src/widgets/dialogs/qwizard.cpp index 27c0a12f71..8405f0a19a 100644 --- a/src/widgets/dialogs/qwizard.cpp +++ b/src/widgets/dialogs/qwizard.cpp @@ -691,8 +691,9 @@ void QWizardPrivate::reset() if (current != -1) { q->currentPage()->hide(); cleanupPagesNotInHistory(); - for (int i = history.size() - 1; i >= 0; --i) - q->cleanupPage(history.at(i)); + const auto end = history.crend(); + for (auto it = history.crbegin(); it != end; ++it) + q->cleanupPage(*it); history.clear(); for (QWizardPage *page : std::as_const(pageMap)) page->d_func()->initialized = false; @@ -1420,10 +1421,9 @@ void QWizardPrivate::updateButtonTexts() void QWizardPrivate::updateButtonLayout() { if (buttonsHaveCustomLayout) { - QVarLengthArray array(buttonsCustomLayout.size()); - for (int i = 0; i < buttonsCustomLayout.size(); ++i) - array[i] = buttonsCustomLayout.at(i); - setButtonLayout(array.constData(), array.size()); + QVarLengthArray array{ + buttonsCustomLayout.cbegin(), buttonsCustomLayout.cend()}; + setButtonLayout(array.constData(), int(array.size())); } else { // Positions: // Help Stretch Custom1 Custom2 Custom3 Cancel Back Next Commit Finish Cancel Help @@ -2188,8 +2188,8 @@ void QWizard::setPage(int theid, QWizardPage *page) page->setParent(d->pageFrame); QList &pendingFields = page->d_func()->pendingFields; - for (int i = 0; i < pendingFields.size(); ++i) - d->addField(pendingFields.at(i)); + for (const auto &field : std::as_const(pendingFields)) + d->addField(field); pendingFields.clear(); connect(page, SIGNAL(completeChanged()), this, SLOT(_q_updateButtonStates())); @@ -3619,8 +3619,9 @@ bool QWizardPage::isComplete() const return true; const QList &wizardFields = d->wizard->d_func()->fields; - for (int i = wizardFields.size() - 1; i >= 0; --i) { - const QWizardField &field = wizardFields.at(i); + const auto end = wizardFields.crend(); + for (auto it = wizardFields.crbegin(); it != end; ++it) { + const QWizardField &field = *it; if (field.page == this && field.mandatory) { QVariant value = field.object->property(field.property); if (value == field.initialValue) diff --git a/src/widgets/kernel/qaction_widgets.cpp b/src/widgets/kernel/qaction_widgets.cpp index 53b6e13b2d..ad282ab104 100644 --- a/src/widgets/kernel/qaction_widgets.cpp +++ b/src/widgets/kernel/qaction_widgets.cpp @@ -28,8 +28,9 @@ void QtWidgetsActionPrivate::destroy() { Q_Q(QAction); const auto objects = associatedObjects; - for (int i = objects.size()-1; i >= 0; --i) { - QObject *object = objects.at(i); + const auto end = objects.crend(); + for (auto it = objects.crbegin(); it != end; ++it) { + QObject *object = *it; if (QWidget *widget = qobject_cast(object)) widget->removeAction(q); #if QT_CONFIG(graphicsview)