From e02348117c5d2f5dc6b1a249c58a0293a71bb526 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 12 Mar 2025 13:41:44 +0100 Subject: [PATCH] QSimplex: scope iterators tighter in for loops This is in preparation of breaking QSimplexVariable up into QSimplexVariable (with protected dtor) and trivial subclass QConcreteSimplexVariable, to statically ensure that we're not deleting derived classes (AnchorData) through QSimplexVariable pointers (which is UB, and subject to a Coverity complaint). This is basically the same program we did for QBrushData, culminating in 3bbc9e29ef59683351cf35c19a8bd4a030615c64. This first step scopes iterators of for loops in the for-loop, and, as drive-bys, makes them use auto and the shorter cbegin()/cend(), so they fit into a single line, and fixes the extra {} around single-line bodies (only in touched lines). Pick-to: 6.8 6.5 Coverity-Id: 390828 Change-Id: I439e0a10ebb316a33e92c42c255ee209139d47a2 Reviewed-by: Mate Barany (cherry picked from commit 49809c6e41101a11b5a264c7e8b4d4ce05d3c04a) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/graphicsview/qsimplex_p.cpp | 18 ++++-------------- src/widgets/graphicsview/qsimplex_p.h | 6 ++---- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/widgets/graphicsview/qsimplex_p.cpp b/src/widgets/graphicsview/qsimplex_p.cpp index 948437bf68b..4586e0d008c 100644 --- a/src/widgets/graphicsview/qsimplex_p.cpp +++ b/src/widgets/graphicsview/qsimplex_p.cpp @@ -230,12 +230,8 @@ bool QSimplex::setConstraints(const QList &newConstraints) setValueAt(i, c->helper.first->index, 1.0); } - QHash::const_iterator iter; - for (iter = c->variables.constBegin(); - iter != c->variables.constEnd(); - ++iter) { + for (auto iter = c->variables.cbegin(); iter != c->variables.cend(); ++iter) setValueAt(i, iter.key()->index, iter.value()); - } setValueAt(i, columns - 1, c->constant); } @@ -490,10 +486,7 @@ qreal QSimplex::solver(SolverFactor factor) // Set new objective in the first row of the simplex matrix qreal resultOffset = 0; - QHash::const_iterator iter; - for (iter = objective->variables.constBegin(); - iter != objective->variables.constEnd(); - ++iter) { + for (auto iter = objective->variables.cbegin(); iter != objective->variables.cend(); ++iter) { // Check if the variable was removed in the simplification process. // If so, we save its offset to the objective function and skip adding @@ -594,8 +587,7 @@ bool QSimplex::simplifyConstraints(QList *constraints) } // Replace known values among their variables - QHash::const_iterator r; - for (r = results.constBegin(); r != results.constEnd(); ++r) { + for (auto r = results.cbegin(); r != results.cend(); ++r) { if (c->variables.contains(r.key())) { c->constant -= r.value() * c->variables.take(r.key()); modified = true; @@ -630,10 +622,8 @@ void QSimplexConstraint::invert() constant = -constant; ratio = Ratio(2 - ratio); - QHash::iterator iter; - for (iter = variables.begin(); iter != variables.end(); ++iter) { + for (auto iter = variables.begin(); iter != variables.end(); ++iter) iter.value() = -iter.value(); - } } QT_END_NAMESPACE diff --git a/src/widgets/graphicsview/qsimplex_p.h b/src/widgets/graphicsview/qsimplex_p.h index f3efa2eb191..c45e728ccf9 100644 --- a/src/widgets/graphicsview/qsimplex_p.h +++ b/src/widgets/graphicsview/qsimplex_p.h @@ -65,8 +65,7 @@ struct QSimplexConstraint bool isSatisfied() { qreal leftHandSide(0); - QHash::const_iterator iter; - for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) { + for (auto iter = variables.cbegin(); iter != variables.cend(); ++iter) { leftHandSide += iter.value() * iter.key()->result; } @@ -90,8 +89,7 @@ struct QSimplexConstraint QString result; result += QString::fromLatin1("-- QSimplexConstraint %1 --").arg(quintptr(this), 0, 16); - QHash::const_iterator iter; - for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) { + for (auto iter = variables.cbegin(); iter != variables.cend(); ++iter) { result += QString::fromLatin1(" %1 x %2").arg(iter.value()).arg(quintptr(iter.key()), 0, 16); }