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.
This commit is contained in:
Chocobo1 2024-11-05 11:55:55 +08:00 committed by GitHub
parent b462a2bf0c
commit 051d7137ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 27 additions and 17 deletions

View file

@ -102,7 +102,7 @@ bool BandwidthScheduler::isTimeForAlternative() const
alternative = !alternative; alternative = !alternative;
break; break;
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
break; break;
} }
} }

View file

@ -296,14 +296,15 @@ namespace
{ {
switch (mode) switch (mode)
{ {
default:
Q_ASSERT(false);
case MoveStorageMode::FailIfExist: case MoveStorageMode::FailIfExist:
return lt::move_flags_t::fail_if_exist; return lt::move_flags_t::fail_if_exist;
case MoveStorageMode::KeepExistingFiles: case MoveStorageMode::KeepExistingFiles:
return lt::move_flags_t::dont_replace; return lt::move_flags_t::dont_replace;
case MoveStorageMode::Overwrite: case MoveStorageMode::Overwrite:
return lt::move_flags_t::always_replace_files; return lt::move_flags_t::always_replace_files;
default:
Q_UNREACHABLE();
break;
} }
} }
} }

View file

@ -160,7 +160,7 @@ void Connection::read()
break; break;
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
return; return;
} }
} }

View file

@ -153,7 +153,7 @@ QString DNSUpdater::getUpdateUrl() const
break; break;
default: default:
qWarning() << "Unrecognized Dynamic DNS service!"; qWarning() << "Unrecognized Dynamic DNS service!";
Q_ASSERT(false); Q_UNREACHABLE();
break; break;
} }
url.setPath(u"/nic/update"_s); url.setPath(u"/nic/update"_s);
@ -305,7 +305,7 @@ QUrl DNSUpdater::getRegistrationUrl(const DNS::Service service)
case DNS::Service::NoIP: case DNS::Service::NoIP:
return {u"https://www.noip.com/remote-access"_s}; return {u"https://www.noip.com/remote-access"_s};
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
break; break;
} }
return {}; return {};

View file

@ -205,9 +205,11 @@ bool TorrentFilter::matchState(const BitTorrent::Torrent *const torrent) const
case Errored: case Errored:
return torrent->isErrored(); return torrent->isErrored();
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
return false; break;
} }
return false;
} }
bool TorrentFilter::matchHash(const BitTorrent::Torrent *const torrent) const bool TorrentFilter::matchHash(const BitTorrent::Torrent *const torrent) const

View file

@ -410,7 +410,7 @@ void AdvancedSettings::updateInterfaceAddressCombo()
case QAbstractSocket::IPv6Protocol: case QAbstractSocket::IPv6Protocol:
return Utils::Net::canonicalIPv6Addr(address).toString(); return Utils::Net::canonicalIPv6Addr(address).toString();
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
break; break;
} }
return {}; return {};

View file

@ -140,9 +140,11 @@ QString TorrentContentModelItem::displayData(const int column) const
return (value + u'%'); return (value + u'%');
} }
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
return {}; break;
} }
return {};
} }
QVariant TorrentContentModelItem::underlyingData(const int column) const QVariant TorrentContentModelItem::underlyingData(const int column) const
@ -165,9 +167,11 @@ QVariant TorrentContentModelItem::underlyingData(const int column) const
case COL_AVAILABILITY: case COL_AVAILABILITY:
return availability(); return availability();
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
return {}; break;
} }
return {};
} }
int TorrentContentModelItem::row() const int TorrentContentModelItem::row() const

View file

@ -775,7 +775,9 @@ QIcon TransferListModel::getIconByState(const BitTorrent::TorrentState state) co
case BitTorrent::TorrentState::Error: case BitTorrent::TorrentState::Error:
return m_errorIcon; return m_errorIcon;
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
return m_errorIcon; break;
} }
return {};
} }

View file

@ -288,7 +288,7 @@ namespace
} }
break; break;
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
break; break;
} }
} }

View file

@ -396,7 +396,8 @@ void WebApplication::doProcessRequest()
case APIErrorType::NotFound: case APIErrorType::NotFound:
throw NotFoundHTTPError(error.message()); throw NotFoundHTTPError(error.message());
default: default:
Q_ASSERT(false); Q_UNREACHABLE();
break;
} }
} }
} }