From 197cb2f311dbf50bc811bcf107af6e1d98e33ffb Mon Sep 17 00:00:00 2001 From: barton26 Date: Thu, 21 Apr 2022 15:36:37 -0400 Subject: [PATCH 1/5] qt, refactor: Use enum type as switch argument in AddressTableModel --- src/qt/addresstablemodel.cpp | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index ac49bb6a08..7bb0d5d1b1 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -178,42 +178,35 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const AddressTableEntry *rec = static_cast(index.internalPointer()); - if(role == Qt::DisplayRole || role == Qt::EditRole) - { - switch(index.column()) - { + const auto column = static_cast(index.column()); + if (role == Qt::DisplayRole || role == Qt::EditRole) { + switch (column) { case Label: - if(rec->label.isEmpty() && role == Qt::DisplayRole) - { + if (rec->label.isEmpty() && role == Qt::DisplayRole) { return tr("(no label)"); - } - else - { + } else { return rec->label; } case Address: return rec->address; - } - } - else if (role == Qt::FontRole) - { + } // no default case, so the compiler can warn about missing cases + assert(false); + } else if (role == Qt::FontRole) { QFont font; if(index.column() == Address) { font = GUIUtil::bitcoinAddressFont(); } return font; - } - else if (role == TypeRole) - { + } else if (role == TypeRole) { switch(rec->type) { case AddressTableEntry::Sending: return Send; case AddressTableEntry::Receiving: return Receive; - default: break; - } + } // no default case, so the compiler can warn about missing cases + assert(false); } return QVariant(); } From 9aca504a1a422a2d1940ccd56cb2c72f3bd58608 Mon Sep 17 00:00:00 2001 From: barton26 Date: Thu, 21 Apr 2022 15:40:26 -0400 Subject: [PATCH 2/5] qt, refactor: Use enum type as switch argument in BanTableModel --- src/qt/bantablemodel.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/qt/bantablemodel.cpp b/src/qt/bantablemodel.cpp index 4f2006ff32..a78ed24db6 100644 --- a/src/qt/bantablemodel.cpp +++ b/src/qt/bantablemodel.cpp @@ -25,15 +25,13 @@ bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan if (order == Qt::DescendingOrder) std::swap(pLeft, pRight); - switch(column) - { + switch (static_cast(column)) { case BanTableModel::Address: return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0; case BanTableModel::Bantime: return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil; - } - - return false; + } // no default case, so the compiler can warn about missing cases + assert(false); } // private implementation @@ -122,16 +120,17 @@ QVariant BanTableModel::data(const QModelIndex &index, int role) const CCombinedBan *rec = static_cast(index.internalPointer()); + const auto column = static_cast(index.column()); if (role == Qt::DisplayRole) { - switch(index.column()) - { + switch (column) { case Address: return QString::fromStdString(rec->subnet.ToString()); case Bantime: QDateTime date = QDateTime::fromMSecsSinceEpoch(0); date = date.addSecs(rec->banEntry.nBanUntil); return QLocale::system().toString(date, QLocale::LongFormat); - } + } // no default case, so the compiler can warn about missing cases + assert(false); } return QVariant(); From f9f4b9a82ecdf6ef1405268c9e5986e22332177f Mon Sep 17 00:00:00 2001 From: barton26 Date: Thu, 21 Apr 2022 15:44:37 -0400 Subject: [PATCH 3/5] qt, refactor: Use enum type as switch argument in PeerTableModel --- src/qt/peertablemodel.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/qt/peertablemodel.cpp b/src/qt/peertablemodel.cpp index 8513dc5814..82fe844342 100644 --- a/src/qt/peertablemodel.cpp +++ b/src/qt/peertablemodel.cpp @@ -23,8 +23,7 @@ bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombine if (order == Qt::DescendingOrder) std::swap(pLeft, pRight); - switch(column) - { + switch (static_cast(column)) { case PeerTableModel::NetNodeId: return pLeft->id < pRight->id; case PeerTableModel::Address: @@ -37,9 +36,8 @@ bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombine return pLeft->nSendBytes < pRight->nSendBytes; case PeerTableModel::Received: return pLeft->nRecvBytes < pRight->nRecvBytes; - } - - return false; + } // no default case, so the compiler can warn about missing cases + assert(false); } // private implementation @@ -158,9 +156,9 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const CNodeCombinedStats *rec = static_cast(index.internalPointer()); + const auto column = static_cast(index.column()); if (role == Qt::DisplayRole) { - switch(index.column()) - { + switch (column) { case NetNodeId: return (qint64)rec->nodeStats.id; case Address: @@ -179,16 +177,18 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const return GUIUtil::formatBytes(rec->nodeStats.nSendBytes); case Received: return GUIUtil::formatBytes(rec->nodeStats.nRecvBytes); - } + } // no default case, so the compiler can warn about missing cases + assert(false); } else if (role == Qt::TextAlignmentRole) { - switch (index.column()) { + switch (column) { case Ping: case Sent: case Received: return QVariant(Qt::AlignRight | Qt::AlignVCenter); default: return QVariant(); - } + } // no default case, so the compiler can warn about missing cases + assert(false); } return QVariant(); From a5c1b83d67e6426140ca4bdbef35e30088fe16fd Mon Sep 17 00:00:00 2001 From: barton26 Date: Thu, 21 Apr 2022 15:48:18 -0400 Subject: [PATCH 4/5] qt, refactor: Use enum type as switch argument in TransactionTableModel --- src/qt/transactiontablemodel.cpp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp index 551755304d..7a58dff1ee 100644 --- a/src/qt/transactiontablemodel.cpp +++ b/src/qt/transactiontablemodel.cpp @@ -605,20 +605,22 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const return QVariant(); TransactionRecord *rec = static_cast(index.internalPointer()); - switch(role) - { + const auto column = static_cast(index.column()); + switch (role) { case Qt::DecorationRole: - switch(index.column()) - { + switch (column) { case Status: return txStatusDecoration(rec); + case Date: return {}; + case Type: return {}; case ToAddress: return txAddressDecoration(rec); - } - break; + case Amount: return {}; + } // no default case, so the compiler can warn about missing cases + assert(false); case Qt::DisplayRole: - switch(index.column()) - { + switch (column) { + case Status: return {}; case Date: return formatTxDate(rec); case Type: @@ -627,12 +629,11 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const return formatTxToAddress(rec, false); case Amount: return formatTxAmount(rec); - } - break; + } // no default case, so the compiler can warn about missing cases + assert(false); case Qt::EditRole: // Edit role is used for sorting, so return the unformatted values - switch(index.column()) - { + switch (column) { case Status: return QString::fromStdString(rec->status.sortKey); case Date: @@ -643,8 +644,8 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const return formatTxToAddress(rec, true); case Amount: return rec->credit + rec->debit; - } - break; + } // no default case, so the compiler can warn about missing cases + assert(false); case Qt::ToolTipRole: return formatTooltip(rec); case Qt::TextAlignmentRole: From 00f8ef8fdbe6965e84e110a44b640ec5452cf84c Mon Sep 17 00:00:00 2001 From: barton26 Date: Fri, 22 Apr 2022 16:56:13 -0400 Subject: [PATCH 5/5] qt, refactor: Use enum type as switch argument in PollTableModel --- src/qt/voting/polltablemodel.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/qt/voting/polltablemodel.cpp b/src/qt/voting/polltablemodel.cpp index 9fb10ddd96..487eaf1685 100644 --- a/src/qt/voting/polltablemodel.cpp +++ b/src/qt/voting/polltablemodel.cpp @@ -72,8 +72,8 @@ class PollTableDataModel : public QAbstractTableModel return QString::number(row->m_vote_percent_AVW, 'f', 4); case PollTableModel::TopAnswer: return row->m_top_answer; - } - break; + } // no default case, so the compiler can warn about missing cases + assert(false); case Qt::TextAlignmentRole: switch (index.column()) { @@ -102,8 +102,8 @@ class PollTableDataModel : public QAbstractTableModel return QVariant::fromValue(row->m_vote_percent_AVW); case PollTableModel::TopAnswer: return row->m_top_answer; - } - break; + } // no default case, so the compiler can warn about missing cases + assert(false); } return QVariant();