From ea1b0b26b17ff50ce5eba4017c176590bc4023b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Pereira?= Date: Wed, 14 Feb 2018 19:51:36 +0000 Subject: [PATCH] Don't implicitly cast iterator to const_iterator It prevents detachments: To illustrate: QMap map; /* code compiles and works fine but find() returns the non-const QMap::iterator that detaches! */ QMap::const_iterator it = map.find("girish"); but also some subtle bugs: QHash wrong; if (wrong.find(1) == wrong.cend()) { qDebug() << "Not found"; } else { /* find() detached the container before cend() was called, so it prints "Found" */ qDebug() << "Found"; } QHash right; if (right.constFind(1) == right.cend()) { qDebug() << "Not found"; // This is correct now ! } else { qDebug() << "Found"; } Enforced by QT_STRICT_ITERATORS definition. --- src/base/scanfoldersmodel.cpp | 2 +- src/gui/torrentcontenttreeview.cpp | 2 +- src/src.pro | 1 + src/webui/api/appcontroller.cpp | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/base/scanfoldersmodel.cpp b/src/base/scanfoldersmodel.cpp index 6967c988e..c35918797 100644 --- a/src/base/scanfoldersmodel.cpp +++ b/src/base/scanfoldersmodel.cpp @@ -340,7 +340,7 @@ void ScanFoldersModel::makePersistent() void ScanFoldersModel::configure() { - QVariantHash dirs = Preferences::instance()->getScanDirs(); + const QVariantHash dirs = Preferences::instance()->getScanDirs(); for (QVariantHash::const_iterator i = dirs.begin(), e = dirs.end(); i != e; ++i) { if (i.value().type() == QVariant::Int) diff --git a/src/gui/torrentcontenttreeview.cpp b/src/gui/torrentcontenttreeview.cpp index 574ab7bf7..9e8f1b14f 100644 --- a/src/gui/torrentcontenttreeview.cpp +++ b/src/gui/torrentcontenttreeview.cpp @@ -66,7 +66,7 @@ void TorrentContentTreeView::keyPressEvent(QKeyEvent *event) Qt::CheckState state = (static_cast(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked); - QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME); + const QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME); for (QModelIndexList::const_iterator i = selection.begin(); i != selection.end(); ++i) { QModelIndex index = *i; diff --git a/src/src.pro b/src/src.pro index 48ee4857d..3b76fbafa 100644 --- a/src/src.pro +++ b/src/src.pro @@ -57,6 +57,7 @@ include(../version.pri) DEFINES += QT_NO_CAST_TO_ASCII # Efficient construction for QString & QByteArray (Qt >= 4.8) DEFINES += QT_USE_QSTRINGBUILDER +DEFINES += QT_STRICT_ITERATORS INCLUDEPATH += $$PWD diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 2b3035c61..87005bd06 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -88,7 +88,7 @@ void AppController::preferencesAction() data["temp_path"] = Utils::Fs::toNativePath(session->tempPath()); data["preallocate_all"] = session->isPreallocationEnabled(); data["incomplete_files_ext"] = session->isAppendExtensionEnabled(); - QVariantHash dirs = pref->getScanDirs(); + const QVariantHash dirs = pref->getScanDirs(); QVariantMap nativeDirs; for (QVariantHash::const_iterator i = dirs.begin(), e = dirs.end(); i != e; ++i) { if (i.value().type() == QVariant::Int) @@ -240,7 +240,7 @@ void AppController::setPreferencesAction() if (m.contains("incomplete_files_ext")) session->setAppendExtensionEnabled(m["incomplete_files_ext"].toBool()); if (m.contains("scan_dirs")) { - QVariantMap nativeDirs = m["scan_dirs"].toMap(); + const QVariantMap nativeDirs = m["scan_dirs"].toMap(); QVariantHash oldScanDirs = pref->getScanDirs(); QVariantHash scanDirs; ScanFoldersModel *model = ScanFoldersModel::instance();