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 <richard.gustavsen@qt.io>
This commit is contained in:
Ahmad Samir 2023-05-29 00:55:59 +03:00
parent 794dbfe0a0
commit 3ee289e40d
7 changed files with 63 additions and 70 deletions

View File

@ -1106,11 +1106,10 @@ void QFileSystemModelPrivate::sortChildren(int column, const QModelIndex &parent
indexNode->visibleChildren.clear(); indexNode->visibleChildren.clear();
//No more dirty item we reset our internal dirty index //No more dirty item we reset our internal dirty index
indexNode->dirtyChildrenIndex = -1; indexNode->dirtyChildrenIndex = -1;
const int numValues = values.size(); indexNode->visibleChildren.reserve(values.size());
indexNode->visibleChildren.reserve(numValues); for (QFileSystemNode *node : std::as_const(values)) {
for (int i = 0; i < numValues; ++i) { indexNode->visibleChildren.append(node->fileName);
indexNode->visibleChildren.append(values.at(i)->fileName); node->isVisible = true;
values.at(i)->isVisible = true;
} }
if (!disableRecursiveSort) { if (!disableRecursiveSort) {
@ -1136,10 +1135,8 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order)
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
QModelIndexList oldList = persistentIndexList(); QModelIndexList oldList = persistentIndexList();
QList<QPair<QFileSystemModelPrivate::QFileSystemNode *, int>> oldNodes; QList<QPair<QFileSystemModelPrivate::QFileSystemNode *, int>> oldNodes;
const int nodeCount = oldList.size(); oldNodes.reserve(oldList.size());
oldNodes.reserve(nodeCount); for (const QModelIndex &oldNode : oldList) {
for (int i = 0; i < nodeCount; ++i) {
const QModelIndex &oldNode = oldList.at(i);
QPair<QFileSystemModelPrivate::QFileSystemNode*, int> pair(d->node(oldNode), oldNode.column()); QPair<QFileSystemModelPrivate::QFileSystemNode*, int> pair(d->node(oldNode), oldNode.column());
oldNodes.append(pair); oldNodes.append(pair);
} }
@ -1153,12 +1150,10 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order)
d->sortOrder = order; d->sortOrder = order;
QModelIndexList newList; QModelIndexList newList;
const int numOldNodes = oldNodes.size(); newList.reserve(oldNodes.size());
newList.reserve(numOldNodes); for (const auto &[node, col]: std::as_const(oldNodes))
for (int i = 0; i < numOldNodes; ++i) { newList.append(d->index(node, col));
const QPair<QFileSystemModelPrivate::QFileSystemNode*, int> &oldNode = oldNodes.at(i);
newList.append(d->index(oldNode.first, oldNode.second));
}
changePersistentIndexList(oldList, newList); changePersistentIndexList(oldList, newList);
emit layoutChanged(); emit layoutChanged();
} }

View File

@ -54,8 +54,9 @@ QPixmap QPlatformScreen::grabWindow(WId window, int x, int y, int width, int hei
QWindow *QPlatformScreen::topLevelAt(const QPoint & pos) const QWindow *QPlatformScreen::topLevelAt(const QPoint & pos) const
{ {
const QWindowList list = QGuiApplication::topLevelWindows(); const QWindowList list = QGuiApplication::topLevelWindows();
for (int i = list.size()-1; i >= 0; --i) { const auto crend = list.crend();
QWindow *w = list[i]; for (auto it = list.crbegin(); it != crend; ++it) {
QWindow *w = *it;
if (w->isVisible() && QHighDpi::toNativePixels(w->geometry(), w).contains(pos)) if (w->isVisible() && QHighDpi::toNativePixels(w->geometry(), w).contains(pos))
return w; return w;
} }

View File

@ -34,9 +34,10 @@ Q_LOGGING_CATEGORY(lcDnd, "qt.gui.dnd")
static QWindow* topLevelAt(const QPoint &pos) static QWindow* topLevelAt(const QPoint &pos)
{ {
QWindowList list = QGuiApplication::topLevelWindows(); const QWindowList list = QGuiApplication::topLevelWindows();
for (int i = list.size()-1; i >= 0; --i) { const auto crend = list.crend();
QWindow *w = list.at(i); for (auto it = list.crbegin(); it != crend; ++it) {
QWindow *w = *it;
if (w->isVisible() && w->handle() && w->geometry().contains(pos) && !qobject_cast<QShapedPixmapWindow*>(w)) if (w->isVisible() && w->handle() && w->geometry().contains(pos) && !qobject_cast<QShapedPixmapWindow*>(w))
return w; return w;
} }

View File

@ -1240,12 +1240,10 @@ QStringList QFileDialogPrivate::addDefaultSuffixToFiles(const QStringList &files
QList<QUrl> QFileDialogPrivate::addDefaultSuffixToUrls(const QList<QUrl> &urlsToFix) const QList<QUrl> QFileDialogPrivate::addDefaultSuffixToUrls(const QList<QUrl> &urlsToFix) const
{ {
QList<QUrl> urls; QList<QUrl> urls;
const int numUrlsToFix = urlsToFix.size(); urls.reserve(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 // if the filename has no suffix, add the default suffix
const QString defaultSuffix = options->defaultSuffix(); const QString defaultSuffix = options->defaultSuffix();
for (QUrl url : urlsToFix) {
if (!defaultSuffix.isEmpty()) { if (!defaultSuffix.isEmpty()) {
const QString urlPath = url.path(); const QString urlPath = url.path();
const auto idx = urlPath.lastIndexOf(u'/'); const auto idx = urlPath.lastIndexOf(u'/');
@ -1353,11 +1351,10 @@ QStringList qt_strip_filters(const QStringList &filters)
#if QT_CONFIG(regularexpression) #if QT_CONFIG(regularexpression)
QStringList strippedFilters; QStringList strippedFilters;
static const QRegularExpression r(QString::fromLatin1(QPlatformFileDialogHelper::filterRegExp)); static const QRegularExpression r(QString::fromLatin1(QPlatformFileDialogHelper::filterRegExp));
const int numFilters = filters.size(); strippedFilters.reserve(filters.size());
strippedFilters.reserve(numFilters); for (const QString &filter : filters) {
for (int i = 0; i < numFilters; ++i) {
QString filterName; QString filterName;
auto match = r.match(filters[i]); auto match = r.match(filter);
if (match.hasMatch()) if (match.hasMatch())
filterName = match.captured(1); filterName = match.captured(1);
strippedFilters.append(filterName.simplified()); strippedFilters.append(filterName.simplified());
@ -1392,11 +1389,10 @@ void QFileDialog::setNameFilters(const QStringList &filters)
{ {
Q_D(QFileDialog); Q_D(QFileDialog);
QStringList cleanedFilters; QStringList cleanedFilters;
const int numFilters = filters.size(); cleanedFilters.reserve(filters.size());
cleanedFilters.reserve(numFilters); for (const QString &filter : filters)
for (int i = 0; i < numFilters; ++i) { cleanedFilters << filter.simplified();
cleanedFilters << filters[i].simplified();
}
d->options->setNameFilters(cleanedFilters); d->options->setNameFilters(cleanedFilters);
if (!d->usingWidgets()) if (!d->usingWidgets())
@ -3355,8 +3351,10 @@ void QFileDialogPrivate::navigate(HistoryItem &historyItem)
| QItemSelectionModel::Rows; | QItemSelectionModel::Rows;
selectionModel->select(historyItem.selection.constFirst(), selectionModel->select(historyItem.selection.constFirst(),
flags | QItemSelectionModel::Clear | QItemSelectionModel::Current); flags | QItemSelectionModel::Clear | QItemSelectionModel::Current);
for (int i = 1, size = historyItem.selection.size(); i < size; ++i) auto it = historyItem.selection.cbegin() + 1;
selectionModel->select(historyItem.selection.at(i), flags); const auto end = historyItem.selection.cend();
for (; it != end; ++it)
selectionModel->select(*it, flags);
view->scrollTo(historyItem.selection.constFirst()); view->scrollTo(historyItem.selection.constFirst());
} }
@ -3532,9 +3530,9 @@ void QFileDialogPrivate::_q_deleteCurrent()
if (model->isReadOnly()) if (model->isReadOnly())
return; return;
QModelIndexList list = qFileDialogUi->listView->selectionModel()->selectedRows(); const QModelIndexList list = qFileDialogUi->listView->selectionModel()->selectedRows();
for (int i = list.size() - 1; i >= 0; --i) { for (auto it = list.crbegin(), end = list.crend(); it != end; ++it) {
QPersistentModelIndex index = list.at(i); QPersistentModelIndex index = *it;
if (index == qFileDialogUi->listView->rootIndex()) if (index == qFileDialogUi->listView->rootIndex())
continue; continue;

View File

@ -211,8 +211,9 @@ void QUrlModel::addUrls(const QList<QUrl> &list, int row, bool move)
if (row == -1) if (row == -1)
row = rowCount(); row = rowCount();
row = qMin(row, rowCount()); row = qMin(row, rowCount());
for (int i = list.size() - 1; i >= 0; --i) { const auto rend = list.crend();
QUrl url = list.at(i); for (auto it = list.crbegin(); it != rend; ++it) {
QUrl url = *it;
if (!url.isValid() || url.scheme() != "file"_L1) if (!url.isValid() || url.scheme() != "file"_L1)
continue; continue;
//this makes sure the url is clean //this makes sure the url is clean
@ -312,13 +313,11 @@ void QUrlModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &botto
void QUrlModel::layoutChanged() void QUrlModel::layoutChanged()
{ {
QStringList paths; QStringList paths;
const int numPaths = watching.size(); paths.reserve(watching.size());
paths.reserve(numPaths); for (const WatchItem &item : std::as_const(watching))
for (int i = 0; i < numPaths; ++i) paths.append(item.path);
paths.append(watching.at(i).path);
watching.clear(); watching.clear();
for (int i = 0; i < numPaths; ++i) { for (const auto &path : paths) {
QString path = paths.at(i);
QModelIndex newIndex = fileSystemModel->index(path); QModelIndex newIndex = fileSystemModel->index(path);
watching.append({newIndex, path}); watching.append({newIndex, path});
if (newIndex.isValid()) if (newIndex.isValid())
@ -429,16 +428,13 @@ void QSidebar::showContextMenu(const QPoint &position)
*/ */
void QSidebar::removeEntry() void QSidebar::removeEntry()
{ {
QList<QModelIndex> idxs = selectionModel()->selectedIndexes(); const QList<QModelIndex> idxs = selectionModel()->selectedIndexes();
QList<QPersistentModelIndex> indexes; // Create a list of QPersistentModelIndex as the removeRow() calls below could
const int numIndexes = idxs.size(); // invalidate the indexes in "idxs"
indexes.reserve(numIndexes); const QList<QPersistentModelIndex> persIndexes(idxs.cbegin(), idxs.cend());
for (int i = 0; i < numIndexes; i++) for (const QPersistentModelIndex &persistent : persIndexes) {
indexes.append(idxs.at(i)); if (!persistent.data(QUrlModel::UrlRole).toUrl().path().isEmpty())
model()->removeRow(persistent.row());
for (int i = 0; i < numIndexes; ++i) {
if (!indexes.at(i).data(QUrlModel::UrlRole).toUrl().path().isEmpty())
model()->removeRow(indexes.at(i).row());
} }
} }

View File

@ -691,8 +691,9 @@ void QWizardPrivate::reset()
if (current != -1) { if (current != -1) {
q->currentPage()->hide(); q->currentPage()->hide();
cleanupPagesNotInHistory(); cleanupPagesNotInHistory();
for (int i = history.size() - 1; i >= 0; --i) const auto end = history.crend();
q->cleanupPage(history.at(i)); for (auto it = history.crbegin(); it != end; ++it)
q->cleanupPage(*it);
history.clear(); history.clear();
for (QWizardPage *page : std::as_const(pageMap)) for (QWizardPage *page : std::as_const(pageMap))
page->d_func()->initialized = false; page->d_func()->initialized = false;
@ -1420,10 +1421,9 @@ void QWizardPrivate::updateButtonTexts()
void QWizardPrivate::updateButtonLayout() void QWizardPrivate::updateButtonLayout()
{ {
if (buttonsHaveCustomLayout) { if (buttonsHaveCustomLayout) {
QVarLengthArray<QWizard::WizardButton, QWizard::NButtons> array(buttonsCustomLayout.size()); QVarLengthArray<QWizard::WizardButton, QWizard::NButtons> array{
for (int i = 0; i < buttonsCustomLayout.size(); ++i) buttonsCustomLayout.cbegin(), buttonsCustomLayout.cend()};
array[i] = buttonsCustomLayout.at(i); setButtonLayout(array.constData(), int(array.size()));
setButtonLayout(array.constData(), array.size());
} else { } else {
// Positions: // Positions:
// Help Stretch Custom1 Custom2 Custom3 Cancel Back Next Commit Finish Cancel Help // 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); page->setParent(d->pageFrame);
QList<QWizardField> &pendingFields = page->d_func()->pendingFields; QList<QWizardField> &pendingFields = page->d_func()->pendingFields;
for (int i = 0; i < pendingFields.size(); ++i) for (const auto &field : std::as_const(pendingFields))
d->addField(pendingFields.at(i)); d->addField(field);
pendingFields.clear(); pendingFields.clear();
connect(page, SIGNAL(completeChanged()), this, SLOT(_q_updateButtonStates())); connect(page, SIGNAL(completeChanged()), this, SLOT(_q_updateButtonStates()));
@ -3619,8 +3619,9 @@ bool QWizardPage::isComplete() const
return true; return true;
const QList<QWizardField> &wizardFields = d->wizard->d_func()->fields; const QList<QWizardField> &wizardFields = d->wizard->d_func()->fields;
for (int i = wizardFields.size() - 1; i >= 0; --i) { const auto end = wizardFields.crend();
const QWizardField &field = wizardFields.at(i); for (auto it = wizardFields.crbegin(); it != end; ++it) {
const QWizardField &field = *it;
if (field.page == this && field.mandatory) { if (field.page == this && field.mandatory) {
QVariant value = field.object->property(field.property); QVariant value = field.object->property(field.property);
if (value == field.initialValue) if (value == field.initialValue)

View File

@ -28,8 +28,9 @@ void QtWidgetsActionPrivate::destroy()
{ {
Q_Q(QAction); Q_Q(QAction);
const auto objects = associatedObjects; const auto objects = associatedObjects;
for (int i = objects.size()-1; i >= 0; --i) { const auto end = objects.crend();
QObject *object = objects.at(i); for (auto it = objects.crbegin(); it != end; ++it) {
QObject *object = *it;
if (QWidget *widget = qobject_cast<QWidget*>(object)) if (QWidget *widget = qobject_cast<QWidget*>(object))
widget->removeAction(q); widget->removeAction(q);
#if QT_CONFIG(graphicsview) #if QT_CONFIG(graphicsview)