QCoreApplication::requestPermission: refactor "dead" code

An assert that checks in debug mode is morally equivalent to an
assumption/contract in release mode. Rechecking that an assumption is
true is pointless (it's true by definition), so the code for the case
the check fails is effectively "dead".

Right now Q_ASSERT(x) doesn't turn into Q_ASSUME(x) because some
compilers still have poor codegen (and basically emit a check, even in
release mode). With C++23's [[assume(x)]] we may reconsider that. As
soon as we do it, this code is no longer theoretically dead but
practically dead.

Refactor the code so that the check is done unconditionally, and
if it fails, we assert (in debug mode). In release, we protect users
from a broken backend (which may cause an endless loop of user code
re-requesting the same permission, so it's worth avoiding it).

Change-Id: If0e70e7d88a585ce16ec4838ba7be747652d8155
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2023-09-05 09:52:31 +02:00
parent 0bd1fc0060
commit ca8fefca85

View File

@ -2921,10 +2921,11 @@ void QCoreApplication::requestPermission(const QPermission &requestedPermission,
PermissionReceiver *receiver = new PermissionReceiver(std::move(slotObj), context); PermissionReceiver *receiver = new PermissionReceiver(std::move(slotObj), context);
QPermissions::Private::requestPermission(requestedPermission, [=](Qt::PermissionStatus status) { QPermissions::Private::requestPermission(requestedPermission, [=](Qt::PermissionStatus status) {
Q_ASSERT_X(status != Qt::PermissionStatus::Undetermined, "QPermission", if (status == Qt::PermissionStatus::Undetermined) {
"QCoreApplication::requestPermission() should never return Undetermined"); Q_ASSERT_X(false, "QPermission",
if (status == Qt::PermissionStatus::Undetermined) "Internal error: requestPermission() should never return Undetermined");
status = Qt::PermissionStatus::Denied; status = Qt::PermissionStatus::Denied;
}
if (QCoreApplication::self) { if (QCoreApplication::self) {
QPermission permission = requestedPermission; QPermission permission = requestedPermission;