Don't implicitly cast iterator to const_iterator

It prevents detachments:
To illustrate:

QMap<QString, QString> map;
/* code compiles and works fine but find() returns the non-const
   QMap::iterator that detaches!
*/
QMap<QString, QString>::const_iterator it = map.find("girish");

but also some subtle bugs:

QHash<int, int> 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<int, int> right;
if (right.constFind(1) == right.cend()) {
    qDebug() << "Not found"; // This is correct now !
} else {
    qDebug() << "Found";
}

Enforced by QT_STRICT_ITERATORS definition.
This commit is contained in:
Luís Pereira 2018-02-14 19:51:36 +00:00
parent 98a1c111b9
commit ea1b0b26b1
4 changed files with 5 additions and 4 deletions

View file

@ -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)