qt6-bb10/tests/auto/corelib
Marc Mutz 9f31f579ec Sequential erase/_if: don't apply predicate twice to element
The code was trying to avoid a detach in the case no element needed to
be removed, by first running find_if() on const_iterators, and then,
after converting its result to (mutable) iterators, start the
remove_if() algorithm where find_if() left off.

But this applies the predicate to the element found by find_if() (if
any) _twice_: first just before we exit the first find_if() and then
just as we enter remove_if(), which will start by running find_if()
again, with the result of the initial find_if as 'first'.

Apart from being needlessly inefficient, this violates the
specification of Uniform Erasure, which defines sequential erase_if()
as being equivalent to remove_if() + container erase(), with the
former being specified to apply the predicate exactly once per
element.

Fix by writing the remove_if() part by hand.

Instead of doing the dance with the loop invariant documentation
twice, simply implement erase() via erase_if() (complicated a bit by
the weird passing of predicates by lvalue reference instead of by
value, as would be idiomatic). This exposes users to:

[ChangeLog][QtCore][Potentially Source-Incompatible Changes] A fix in
the implementation of the erase-like algorithms of sequential Qt
container may re-enable signed/unsigned comparison warnings previously
suppressed by having occurred in std library code. To fix, cast the
value to look for such that it has the same signedness as the
container's elements.

... but the issue would be the same had we inlined std::remove()
instead of passing a lambda to sequential_erase_if(), so it's nothing
we can, nor should, work around.

[ChangeLog][QtCore][Containers] Fixed a bug in the implementation of
most sequential Qt container's erase-like algorithms (member
removeAll()/removeIf() and free erase()/erase_if()) where the equality
operator or the predicate, respectively, was applied to the first
matching element twice. Each element is now tested exactly once.

Pick-to: 6.3 6.2
Change-Id: Ib6d24b01b40866c125406f1cd6042d4cd083ea0d
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2022-02-02 04:16:59 +00:00
..
animation tst_qanimationgroup: fix memleaks 2022-01-05 08:22:00 +01:00
global Fix tst_qfloat16 run time error for INTEGRITY (tested on sa8115) 2022-01-30 00:02:38 +00:00
io Blacklist tst_QFileSystemWatcher::signalsEmittedAfterFileMoved on all Windows versions 2022-01-31 17:39:33 +01:00
itemmodels tst_QItemModel: fix memleaks 2022-01-05 08:22:00 +01:00
kernel tst_QMetaType: remove call to pthread_yield() 2022-01-19 20:57:19 -08:00
mimetypes Remove unused .qrc files 2022-01-17 23:17:01 +01:00
platform Android: Add runOnMainAndroidThread() under QNativeInterface 2021-05-26 23:24:11 +00:00
plugin QFactoryLoader: add setExtraSearchPath() (for QPA plugins' use) 2022-01-20 15:53:48 -08:00
serialization JSON: Further improve the duplicate handling in the parser 2022-01-21 00:53:49 +01:00
text QByteArray: fix isUpper/isLower 2022-01-25 15:29:20 +01:00
thread Restore missing Qt definitions 2022-01-31 17:40:43 +01:00
time Use Q_INT64_C() to express qint64 constants 2022-01-21 21:45:11 +01:00
tools Sequential erase/_if: don't apply predicate twice to element 2022-02-02 04:16:59 +00:00
CMakeLists.txt Add tests for QAndroidApplication's sdkVersion and activity 2021-05-13 01:41:36 +03:00