From 051d7137eaf23bcd00cdea4d9b6d355f5aa48aa2 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 5 Nov 2024 11:55:55 +0800 Subject: [PATCH] Use proper macro for unreachable switch cases Those are the `default` cases which are not expected to hit (nor reachable) normally. When the code is compiled with release mode and it reaches `Q_UNREACHABLE()`, it becomes undefined behavior. So it rely on the developers to catch the errors in debug mode. The upside of this is that the `switch` statement will be more optimized than not using it. This also means the statements after `Q_UNREACHABLE()` isn't important. It allow anything to preserve the intention of the code. This macro is preferred over C++23 `std::unreachable` because it will automatically insert a `Q_ASSERT(false)` with it. PR #21752. --- src/base/bittorrent/bandwidthscheduler.cpp | 2 +- src/base/bittorrent/sessionimpl.cpp | 5 +++-- src/base/http/connection.cpp | 2 +- src/base/net/dnsupdater.cpp | 4 ++-- src/base/torrentfilter.cpp | 6 ++++-- src/gui/advancedsettings.cpp | 2 +- src/gui/torrentcontentmodelitem.cpp | 12 ++++++++---- src/gui/transferlistmodel.cpp | 6 ++++-- src/webui/api/synccontroller.cpp | 2 +- src/webui/webapplication.cpp | 3 ++- 10 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/base/bittorrent/bandwidthscheduler.cpp b/src/base/bittorrent/bandwidthscheduler.cpp index bb8d1d0a8..5463cc36e 100644 --- a/src/base/bittorrent/bandwidthscheduler.cpp +++ b/src/base/bittorrent/bandwidthscheduler.cpp @@ -102,7 +102,7 @@ bool BandwidthScheduler::isTimeForAlternative() const alternative = !alternative; break; default: - Q_ASSERT(false); + Q_UNREACHABLE(); break; } } diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 7ccaa6a89..6d201bccc 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -296,14 +296,15 @@ namespace { switch (mode) { - default: - Q_ASSERT(false); case MoveStorageMode::FailIfExist: return lt::move_flags_t::fail_if_exist; case MoveStorageMode::KeepExistingFiles: return lt::move_flags_t::dont_replace; case MoveStorageMode::Overwrite: return lt::move_flags_t::always_replace_files; + default: + Q_UNREACHABLE(); + break; } } } diff --git a/src/base/http/connection.cpp b/src/base/http/connection.cpp index 32a7ce840..55309a778 100644 --- a/src/base/http/connection.cpp +++ b/src/base/http/connection.cpp @@ -160,7 +160,7 @@ void Connection::read() break; default: - Q_ASSERT(false); + Q_UNREACHABLE(); return; } } diff --git a/src/base/net/dnsupdater.cpp b/src/base/net/dnsupdater.cpp index 4aa385668..6399fc5a2 100644 --- a/src/base/net/dnsupdater.cpp +++ b/src/base/net/dnsupdater.cpp @@ -153,7 +153,7 @@ QString DNSUpdater::getUpdateUrl() const break; default: qWarning() << "Unrecognized Dynamic DNS service!"; - Q_ASSERT(false); + Q_UNREACHABLE(); break; } url.setPath(u"/nic/update"_s); @@ -305,7 +305,7 @@ QUrl DNSUpdater::getRegistrationUrl(const DNS::Service service) case DNS::Service::NoIP: return {u"https://www.noip.com/remote-access"_s}; default: - Q_ASSERT(false); + Q_UNREACHABLE(); break; } return {}; diff --git a/src/base/torrentfilter.cpp b/src/base/torrentfilter.cpp index 89d9a97e9..e1a6243b1 100644 --- a/src/base/torrentfilter.cpp +++ b/src/base/torrentfilter.cpp @@ -205,9 +205,11 @@ bool TorrentFilter::matchState(const BitTorrent::Torrent *const torrent) const case Errored: return torrent->isErrored(); default: - Q_ASSERT(false); - return false; + Q_UNREACHABLE(); + break; } + + return false; } bool TorrentFilter::matchHash(const BitTorrent::Torrent *const torrent) const diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index a698bbcb4..27da9beba 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -410,7 +410,7 @@ void AdvancedSettings::updateInterfaceAddressCombo() case QAbstractSocket::IPv6Protocol: return Utils::Net::canonicalIPv6Addr(address).toString(); default: - Q_ASSERT(false); + Q_UNREACHABLE(); break; } return {}; diff --git a/src/gui/torrentcontentmodelitem.cpp b/src/gui/torrentcontentmodelitem.cpp index d226d0852..ee5245c96 100644 --- a/src/gui/torrentcontentmodelitem.cpp +++ b/src/gui/torrentcontentmodelitem.cpp @@ -140,9 +140,11 @@ QString TorrentContentModelItem::displayData(const int column) const return (value + u'%'); } default: - Q_ASSERT(false); - return {}; + Q_UNREACHABLE(); + break; } + + return {}; } QVariant TorrentContentModelItem::underlyingData(const int column) const @@ -165,9 +167,11 @@ QVariant TorrentContentModelItem::underlyingData(const int column) const case COL_AVAILABILITY: return availability(); default: - Q_ASSERT(false); - return {}; + Q_UNREACHABLE(); + break; } + + return {}; } int TorrentContentModelItem::row() const diff --git a/src/gui/transferlistmodel.cpp b/src/gui/transferlistmodel.cpp index 8263a3b6d..7c827c1a6 100644 --- a/src/gui/transferlistmodel.cpp +++ b/src/gui/transferlistmodel.cpp @@ -775,7 +775,9 @@ QIcon TransferListModel::getIconByState(const BitTorrent::TorrentState state) co case BitTorrent::TorrentState::Error: return m_errorIcon; default: - Q_ASSERT(false); - return m_errorIcon; + Q_UNREACHABLE(); + break; } + + return {}; } diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index 8794533b0..f493fe776 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -288,7 +288,7 @@ namespace } break; default: - Q_ASSERT(false); + Q_UNREACHABLE(); break; } } diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 5125c09ac..54d2b404f 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -396,7 +396,8 @@ void WebApplication::doProcessRequest() case APIErrorType::NotFound: throw NotFoundHTTPError(error.message()); default: - Q_ASSERT(false); + Q_UNREACHABLE(); + break; } } }