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();
//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<QPair<QFileSystemModelPrivate::QFileSystemNode *, int>> 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<QFileSystemModelPrivate::QFileSystemNode*, int> 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<QFileSystemModelPrivate::QFileSystemNode*, int> &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();
}

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
{
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;
}

View File

@ -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<QShapedPixmapWindow*>(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> 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;

View File

@ -211,8 +211,9 @@ void QUrlModel::addUrls(const QList<QUrl> &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<QModelIndex> idxs = selectionModel()->selectedIndexes();
QList<QPersistentModelIndex> 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<QModelIndex> idxs = selectionModel()->selectedIndexes();
// Create a list of QPersistentModelIndex as the removeRow() calls below could
// invalidate the indexes in "idxs"
const QList<QPersistentModelIndex> persIndexes(idxs.cbegin(), idxs.cend());
for (const QPersistentModelIndex &persistent : persIndexes) {
if (!persistent.data(QUrlModel::UrlRole).toUrl().path().isEmpty())
model()->removeRow(persistent.row());
}
}

View File

@ -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<QWizard::WizardButton, QWizard::NButtons> array(buttonsCustomLayout.size());
for (int i = 0; i < buttonsCustomLayout.size(); ++i)
array[i] = buttonsCustomLayout.at(i);
setButtonLayout(array.constData(), array.size());
QVarLengthArray<QWizard::WizardButton, QWizard::NButtons> 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<QWizardField> &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<QWizardField> &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)

View File

@ -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<QWidget*>(object))
widget->removeAction(q);
#if QT_CONFIG(graphicsview)