Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Reduce nesting of else clauses after return. #86

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .ci-scripts/build-qtox-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ fi
cmake --build "$BUILD_DIR"

if [ ! -z "${TIDY+x}" ]; then
run-clang-tidy -quiet -fix -format -p "$BUILD_DIR" -header-filter=.* src/ audio/src/ audio/include test/src/ \
test/include util/src/ util/include/
run-clang-tidy -quiet -fix -format -p "$BUILD_DIR" \
audio/include \
audio/src/ \
src/ \
test/include \
test/src/ \
util/include/ \
util/src/
else
ctest -j"$(nproc)" --test-dir "$BUILD_DIR" --output-on-failure
fi
3 changes: 3 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Checks: "-*
, modernize-use-nullptr
, modernize-use-override
, readability-container-*
, readability-else-after-return
, readability-named-parameter
, readability-inconsistent-declaration-parameter-name
"
WarningsAsErrors: "*"
ExcludeHeaderFilterRegex: "/usr/.*"
HeaderFilterRegex: ".*"
19 changes: 9 additions & 10 deletions src/chatlog/chatwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ ChatLogIdx firstItemAfterDate(QDate date, const IChatLog& chatLog)
auto idxs = chatLog.getDateIdxs(date, 1);
if (!idxs.empty()) {
return idxs[0].idx;
} else {
return chatLog.getNextIdx();
}
return chatLog.getNextIdx();
}

/**
Expand Down Expand Up @@ -181,14 +180,13 @@ ChatLogIdx clampedAdd(ChatLogIdx idx, int val, IChatLog& chatLog)
}

return idx - std::abs(val);
} else {
auto distToEnd = chatLog.getNextIdx() - idx;
if (static_cast<size_t>(val) > distToEnd) {
return chatLog.getNextIdx();
}

return idx + val;
}
auto distToEnd = chatLog.getNextIdx() - idx;
if (static_cast<size_t>(val) > distToEnd) {
return chatLog.getNextIdx();
}

return idx + val;
}

} // namespace
Expand Down Expand Up @@ -654,7 +652,8 @@ QString ChatWidget::getSelectedText() const
{
if (selectionMode == SelectionMode::Precise) {
return selClickedRow->content[selClickedCol]->getSelectedText();
} else if (selectionMode == SelectionMode::Multi) {
}
if (selectionMode == SelectionMode::Multi) {
// build a nicely formatted message
QString out;

Expand Down
16 changes: 7 additions & 9 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,10 +1054,9 @@ ConferenceId Core::getConferencePersistentId(uint32_t conferenceNumber) const
std::vector<uint8_t> idBuff(tox_conference_id_size());
if (tox_conference_get_id(tox.get(), conferenceNumber, idBuff.data())) {
return ConferenceId{idBuff.data()};
} else {
qCritical() << "Failed to get conference ID of conference" << conferenceNumber;
return {};
}
qCritical() << "Failed to get conference ID of conference" << conferenceNumber;
return {};
}

/**
Expand Down Expand Up @@ -1232,10 +1231,10 @@ int Core::createConference(uint8_t type)
emit saveRequest();
emit emptyConferenceCreated(conferenceId, getConferencePersistentId(conferenceId));
return conferenceId;
} else {
return std::numeric_limits<uint32_t>::max();
}
} else if (type == TOX_CONFERENCE_TYPE_AV) {
return std::numeric_limits<uint32_t>::max();
}
if (type == TOX_CONFERENCE_TYPE_AV) {
// unlike tox_conference_new, toxav_add_av_groupchat does not have an error enum, so -1
// conference number is our only indication of an error
int conferenceId = toxav_add_av_groupchat(tox.get(), CoreAV::conferenceCallCallback, this);
Expand All @@ -1246,10 +1245,9 @@ int Core::createConference(uint8_t type)
qCritical() << "Unknown error creating AV conference";
}
return conferenceId;
} else {
qWarning() << "createConference: Unknown type" << type;
return -1;
}
qWarning() << "createConference: Unknown type" << type;
return -1;
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/core/coreav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,13 @@ bool CoreAV::answerCall(uint32_t friendNum, bool video)
if (toxav_answer(toxav.get(), friendNum, audioSettings.getAudioBitrate(), videoBitrate, &err)) {
it->second->setActive(true);
return true;
} else {
qWarning() << "Failed to answer call with error" << err;
Toxav_Err_Call_Control controlErr;
toxav_call_control(toxav.get(), friendNum, TOXAV_CALL_CONTROL_CANCEL, &controlErr);
PARSE_ERR(controlErr);
calls.erase(it);
return false;
}
qWarning() << "Failed to answer call with error" << err;
Toxav_Err_Call_Control controlErr;
toxav_call_control(toxav.get(), friendNum, TOXAV_CALL_CONTROL_CANCEL, &controlErr);
PARSE_ERR(controlErr);
calls.erase(it);
return false;
}

bool CoreAV::startCall(uint32_t friendNum, bool video)
Expand Down
3 changes: 1 addition & 2 deletions src/core/toxid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ ToxPk ToxId::getPublicKey() const
const auto pkBytes = toxId.left(ToxPk::size);
if (pkBytes.isEmpty()) {
return ToxPk{};
} else {
return ToxPk{pkBytes};
}
return ToxPk{pkBytes};
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/core/toxpk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ ToxPk::ToxPk(const QString& pk)
: ChatId([&pk]() {
if (pk.length() == numHexChars) {
return QByteArray::fromHex(pk.toLatin1());
} else {
assert(!"ToxPk constructed with invalid length string");
return QByteArray(); // invalid pk string
}
assert(!"ToxPk constructed with invalid length string");
return QByteArray(); // invalid pk string
}())
{
}
Expand Down
6 changes: 3 additions & 3 deletions src/friendlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ QString FriendList::decideNickname(const ToxPk& friendPk, const QString& origNam
Friend* f = FriendList::findFriend(friendPk);
if (f != nullptr) {
return f->getDisplayedName();
} else if (!origName.isEmpty()) {
}
if (!origName.isEmpty()) {
return origName;
} else {
return friendPk.toString();
}
return friendPk.toString();
}
6 changes: 3 additions & 3 deletions src/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ bool IPC::isCurrentOwner()
const bool isOwner = isCurrentOwnerNoLock();
globalMemory.unlock();
return isOwner;
} else {
qWarning() << "isCurrentOwner failed to lock, returning false";
return false;
}
qWarning() << "isCurrentOwner failed to lock, returning false";
return false;

#endif
}

Expand Down
6 changes: 2 additions & 4 deletions src/model/chathistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ ChatLogIdx ChatHistory::getFirstIdx() const
{
if (canUseHistory()) {
return ChatLogIdx(0);
} else {
return sessionChatLog.getFirstIdx();
}
return sessionChatLog.getFirstIdx();
}

ChatLogIdx ChatHistory::getNextIdx() const
Expand All @@ -189,9 +188,8 @@ std::vector<IChatLog::DateChatLogIdxPair> ChatHistory::getDateIdxs(const QDate&

// Do not re-search in the session chat log. If we have history the query to the history should have been sufficient
return ret;
} else {
return sessionChatLog.getDateIdxs(startDate, maxDates);
}
return sessionChatLog.getDateIdxs(startDate, maxDates);
}

void ChatHistory::addSystemMessage(const SystemMessage& message)
Expand Down
15 changes: 6 additions & 9 deletions src/model/notificationgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ QString generateSingleChatTitle(const QHash<T, size_t> numNotifications, T conta
return QObject::tr("%1 message(s) from %2")
.arg(numNotifications[contact])
.arg(contact->getDisplayedName());
} else {
return contact->getDisplayedName();
}
return contact->getDisplayedName();
}

QString generateTitle(const QHash<const Friend*, size_t>& friendNotifications,
Expand All @@ -52,9 +51,8 @@ QString generateTitle(const QHash<const Friend*, size_t>& friendNotifications,
if (numChats > 1) {
return generateMultiChatTitle(numChats,
getNumMessages(friendNotifications, conferenceNotifications));
} else {
return generateSingleChatTitle(friendNotifications, f);
}
return generateSingleChatTitle(friendNotifications, f);
}

QString generateTitle(const QHash<const Friend*, size_t>& friendNotifications,
Expand All @@ -65,9 +63,8 @@ QString generateTitle(const QHash<const Friend*, size_t>& friendNotifications,
if (numChats > 1) {
return generateMultiChatTitle(numChats,
getNumMessages(friendNotifications, conferenceNotifications));
} else {
return generateSingleChatTitle(conferenceNotifications, c);
}
return generateSingleChatTitle(conferenceNotifications, c);
}

QString generateContent(const QHash<const Friend*, size_t>& friendNotifications,
Expand Down Expand Up @@ -107,15 +104,15 @@ QString generateContent(const QHash<const Friend*, size_t>& friendNotifications,
}

return ret;
} else if (conferenceNotifications.size() == 1) {
}
if (conferenceNotifications.size() == 1) {
auto it = conferenceNotifications.begin();
if (it == conferenceNotifications.end()) {
qFatal("Concurrency error: conference notifications got cleared while reading");
}
return it.key()->getPeerList()[sender] + ": " + lastMessage;
} else {
return lastMessage;
}
return lastMessage;
}

QPixmap getSenderAvatar(Profile* profile, const ToxPk& sender)
Expand Down
3 changes: 1 addition & 2 deletions src/model/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ QString getIconPath(Status status, bool event)
const QString statusSuffix = getAssetSuffix(status);
if (status == Status::Blocked) {
return ":/img/status/" + statusSuffix + ".svg";
} else {
return ":/img/status/" + statusSuffix + eventSuffix + ".svg";
}
return ":/img/status/" + statusSuffix + eventSuffix + ".svg";
}

bool isOnline(Status status)
Expand Down
3 changes: 1 addition & 2 deletions src/net/bootstrapnodeupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,8 @@ QList<DhtServer> BootstrapNodeUpdater::getBootstrapNodes() const
auto userFilePath = paths.getUserNodesFilePath();
if (QFile::exists(userFilePath)) {
return loadNodesFile(userFilePath);
} else {
return loadNodesFile(builtinNodesFile);
}
return loadNodesFile(builtinNodesFile);
}

void BootstrapNodeUpdater::requestBootstrapNodes()
Expand Down
4 changes: 2 additions & 2 deletions src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ void Nexus::setProfile(Profile* p)
emit profileLoadFailed();
// Warnings are issued during respective createNew/load calls
return;
} else {
emit profileLoaded();
}
emit profileLoaded();


emit currentProfileChanged(p);
}
Expand Down
21 changes: 10 additions & 11 deletions src/persistence/db/rawdatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,11 @@ bool RawDatabase::openEncryptedDatabaseAtLatestSupportedVersion(const QString& h
qInfo() << "Opened database with SQLCipher" << toString(highestSupportedVersion)
<< "parameters";
return true;
} else {
return updateSavedCipherParameters(hexKey, highestSupportedVersion);
}
} else {
qCritical() << "Failed to set latest supported SQLCipher params!";
return false;
return updateSavedCipherParameters(hexKey, highestSupportedVersion);
}
qCritical() << "Failed to set latest supported SQLCipher params!";
return false;
}

bool RawDatabase::testUsable()
Expand Down Expand Up @@ -891,17 +889,18 @@ QVariant RawDatabase::extractData(sqlite3_stmt* stmt, int col)
int type = sqlite3_column_type(stmt, col);
if (type == SQLITE_INTEGER) {
return sqlite3_column_int64(stmt, col);
} else if (type == SQLITE_TEXT) {
}
if (type == SQLITE_TEXT) {
const char* str = reinterpret_cast<const char*>(sqlite3_column_text(stmt, col));
int len = sqlite3_column_bytes(stmt, col);
return QString::fromUtf8(str, len);
} else if (type == SQLITE_NULL) {
}
if (type == SQLITE_NULL) {
return QVariant{};
} else {
const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(stmt, col));
int len = sqlite3_column_bytes(stmt, col);
return QByteArray(data, len);
}
const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(stmt, col));
int len = sqlite3_column_bytes(stmt, col);
return QByteArray(data, len);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/persistence/db/upgrades/dbupgrader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ DuplicateAlias getDuplicateAliasRows(RawDatabase& db, RowId goodPeerRow, RowId b

if (hasGoodEntry) {
return {goodAliasRow, badAliasRows};
} else {
return {};
}
return {};
}

void mergeAndDeleteAlias(QVector<RawDatabase::Query>& upgradeQueries, RowId goodAlias,
Expand Down Expand Up @@ -231,7 +230,8 @@ bool DbUpgrader::dbSchemaUpgrade(std::shared_ptr<RawDatabase>& db, IMessageBoxMa
<< "). Please upgrade qTox";
// We don't know what future versions have done, we have to disable db access until we re-upgrade
return false;
} else if (databaseSchemaVersion == SCHEMA_VERSION) {
}
if (databaseSchemaVersion == SCHEMA_VERSION) {
// No work to do
return true;
}
Expand Down
17 changes: 8 additions & 9 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,15 @@ RawDatabase::Query History::generateFileFinished(RowId id, bool success, const Q
.arg(id.get()),
QVector<QByteArray>{filePath.toUtf8(), fileHash},
};
} else {
return {
QStringLiteral( //
"UPDATE file_transfers "
"SET file_state = %1 "
"WHERE id = %2")
.arg(file_state)
.arg(id.get()),
};
}
return {
QStringLiteral( //
"UPDATE file_transfers "
"SET file_state = %1 "
"WHERE id = %2")
.arg(file_state)
.arg(id.get()),
};
}

void History::addNewFileMessage(const ChatId& chatId, const QByteArray& fileId,
Expand Down
3 changes: 1 addition & 2 deletions src/persistence/profilelocker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,5 @@ QString ProfileLocker::getCurLockName()
{
if (lockfile)
return curLockName;
else
return {};
return {};
}
Loading
Loading