Skip to content

Commit a8a0a0f

Browse files
committed
refactor: enable readability-container-contains clang-tidy rule
Replace the last few instances of `.count() != 0` and `.count() == 0` patterns with the more expressive C++20 `.contains()` method: * `std::set<std::string>` in `getblocktemplate` RPC; * `std::map<std::string, ...>` in `transaction_tests`; * other bare `std::unordered_set` and `std::map` count calls. With no remaining violations, enable the `readability-container-contains` clang-tidy check to prevent future regressions. Also fixes #34101 by reverting boost::multi_index::contains calls available in Boost 1.74.0 only
1 parent 09a1fa1 commit a8a0a0f

File tree

7 files changed

+13
-12
lines changed

7 files changed

+13
-12
lines changed

src/.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ performance-*,
2525
-performance-noexcept-move-constructor,
2626
-performance-unnecessary-value-param,
2727
readability-const-return-type,
28+
readability-container-contains,
2829
readability-redundant-declaration,
2930
readability-redundant-string-init,
3031
'

src/qt/transactiondesc.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
124124
{
125125
strHTML += "<b>" + tr("Source") + ":</b> " + tr("Generated") + "<br>";
126126
}
127-
else if (wtx.value_map.count("from") && !wtx.value_map["from"].empty())
127+
else if (wtx.value_map.contains("from") && !wtx.value_map["from"].empty())
128128
{
129129
// Online transaction
130130
strHTML += "<b>" + tr("From") + ":</b> " + GUIUtil::HtmlEscape(wtx.value_map["from"]) + "<br>";
@@ -157,7 +157,7 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
157157
//
158158
// To
159159
//
160-
if (wtx.value_map.count("to") && !wtx.value_map["to"].empty())
160+
if (wtx.value_map.contains("to") && !wtx.value_map["to"].empty())
161161
{
162162
// Online transaction
163163
std::string strAddress = wtx.value_map["to"];
@@ -212,7 +212,7 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
212212
if (toSelf && all_from_me)
213213
continue;
214214

215-
if (!wtx.value_map.count("to") || wtx.value_map["to"].empty())
215+
if (!wtx.value_map.contains("to") || wtx.value_map["to"].empty())
216216
{
217217
// Offline transaction
218218
CTxDestination address;
@@ -273,9 +273,9 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
273273
//
274274
// Message
275275
//
276-
if (wtx.value_map.count("message") && !wtx.value_map["message"].empty())
276+
if (wtx.value_map.contains("message") && !wtx.value_map["message"].empty())
277277
strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["message"], true) + "<br>";
278-
if (wtx.value_map.count("comment") && !wtx.value_map["comment"].empty())
278+
if (wtx.value_map.contains("comment") && !wtx.value_map["comment"].empty())
279279
strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["comment"], true) + "<br>";
280280

281281
strHTML += "<b>" + tr("Transaction ID") + ":</b> " + rec->getTxHash() + "<br>";

src/rpc/mining.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,12 +846,12 @@ static RPCHelpMan getblocktemplate()
846846
const Consensus::Params& consensusParams = chainman.GetParams().GetConsensus();
847847

848848
// GBT must be called with 'signet' set in the rules for signet chains
849-
if (consensusParams.signet_blocks && setClientRules.count("signet") != 1) {
849+
if (consensusParams.signet_blocks && !setClientRules.contains("signet")) {
850850
throw JSONRPCError(RPC_INVALID_PARAMETER, "getblocktemplate must be called with the signet rule set (call with {\"rules\": [\"segwit\", \"signet\"]})");
851851
}
852852

853853
// GBT must be called with 'segwit' set in the rules
854-
if (setClientRules.count("segwit") != 1) {
854+
if (!setClientRules.contains("segwit")) {
855855
throw JSONRPCError(RPC_INVALID_PARAMETER, "getblocktemplate must be called with the segwit rule set (call with {\"rules\": [\"segwit\"]})");
856856
}
857857

src/test/transaction_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ script_verify_flags ParseScriptFlags(std::string strFlags)
6060
std::vector<std::string> words = SplitString(strFlags, ',');
6161
for (const std::string& word : words)
6262
{
63-
if (!mapFlagNames.count(word)) {
63+
if (!mapFlagNames.contains(word)) {
6464
BOOST_ERROR("Bad test: unknown verification flag '" << word << "'");
6565
continue;
6666
}
@@ -90,7 +90,7 @@ bool CheckTxScripts(const CTransaction& tx, const std::map<COutPoint, CScript>&
9090
ScriptError err = expect_valid ? SCRIPT_ERR_UNKNOWN_ERROR : SCRIPT_ERR_OK;
9191
for (unsigned int i = 0; i < tx.vin.size() && tx_valid; ++i) {
9292
const CTxIn input = tx.vin[i];
93-
const CAmount amount = map_prevout_values.count(input.prevout) ? map_prevout_values.at(input.prevout) : 0;
93+
const CAmount amount = map_prevout_values.contains(input.prevout) ? map_prevout_values.at(input.prevout) : 0;
9494
try {
9595
tx_valid = VerifyScript(input.scriptSig, map_prevout_scriptPubKeys.at(input.prevout),
9696
&input.scriptWitness, flags, TransactionSignatureChecker(&tx, i, amount, txdata, MissingDataBehavior::ASSERT_FAIL), &err);

src/txmempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ util::Result<std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>>> CTxMemPool::
988988
CTxMemPool::ChangeSet::TxHandle CTxMemPool::ChangeSet::StageAddition(const CTransactionRef& tx, const CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
989989
{
990990
LOCK(m_pool->cs);
991-
Assume(!m_to_add.contains(tx->GetHash()));
991+
Assume(m_to_add.find(tx->GetHash()) == m_to_add.end());
992992
Assume(!m_dependencies_processed);
993993

994994
// We need to process dependencies after adding a new transaction.

src/txrequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ class TxRequestTracker::Impl {
580580
// Bail out if we already have a CANDIDATE_BEST announcement for this (txhash, peer) combination. The case
581581
// where there is a non-CANDIDATE_BEST announcement already will be caught by the uniqueness property of the
582582
// ByPeer index when we try to emplace the new object below.
583-
if (m_index.get<ByPeer>().contains(ByPeerView{peer, true, gtxid.ToUint256()})) return;
583+
if (m_index.get<ByPeer>().count(ByPeerView{peer, true, gtxid.ToUint256()})) return;
584584

585585
// Try creating the announcement with CANDIDATE_DELAYED state (which will fail due to the uniqueness
586586
// of the ByPeer index if a non-CANDIDATE_BEST announcement already exists with the same txhash and peer).

src/wallet/test/fuzz/scriptpubkeyman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
125125
[&] {
126126
const CScript script{ConsumeScript(fuzzed_data_provider)};
127127
if (spk_manager->IsMine(script)) {
128-
assert(spk_manager->GetScriptPubKeys().count(script));
128+
assert(spk_manager->GetScriptPubKeys().contains(script));
129129
}
130130
},
131131
[&] {

0 commit comments

Comments
 (0)