Skip to content

Commit 5c24d3b

Browse files
committed
Merge #13249: Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations.
f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
2 parents bdfeb5d + f34c8c4 commit 5c24d3b

36 files changed

+77
-77
lines changed

src/bech32.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ uint32_t PolyMod(const data& v)
6262
// v, it corresponds to x^2 + v0*x + v1 mod g(x). As 1 mod g(x) = 1, that is the starting value
6363
// for `c`.
6464
uint32_t c = 1;
65-
for (auto v_i : v) {
65+
for (const auto v_i : v) {
6666
// We want to update `c` to correspond to a polynomial with one extra term. If the initial
6767
// value of `c` consists of the coefficients of c(x) = f(x) mod g(x), we modify it to
6868
// correspond to c'(x) = (f(x) * x + v_i) mod g(x), where v_i is the next input to
@@ -149,7 +149,7 @@ std::string Encode(const std::string& hrp, const data& values) {
149149
data combined = Cat(values, checksum);
150150
std::string ret = hrp + '1';
151151
ret.reserve(ret.size() + combined.size());
152-
for (auto c : combined) {
152+
for (const auto c : combined) {
153153
ret += CHARSET[c];
154154
}
155155
return ret;

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static void MutateTxAddOutMultiSig(CMutableTransaction& tx, const std::string& s
385385
CScript scriptPubKey = GetScriptForMultisig(required, pubkeys);
386386

387387
if (bSegWit) {
388-
for (CPubKey& pubkey : pubkeys) {
388+
for (const CPubKey& pubkey : pubkeys) {
389389
if (!pubkey.IsCompressed()) {
390390
throw std::runtime_error("Uncompressed pubkeys are not useable for SegWit outputs");
391391
}

src/cuckoocache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,15 @@ class cache
397397
std::array<uint32_t, 8> locs = compute_hashes(e);
398398
// Make sure we have not already inserted this element
399399
// If we have, make sure that it does not get deleted
400-
for (uint32_t loc : locs)
400+
for (const uint32_t loc : locs)
401401
if (table[loc] == e) {
402402
please_keep(loc);
403403
epoch_flags[loc] = last_epoch;
404404
return;
405405
}
406406
for (uint8_t depth = 0; depth < depth_limit; ++depth) {
407407
// First try to insert to an empty slot, if one exists
408-
for (uint32_t loc : locs) {
408+
for (const uint32_t loc : locs) {
409409
if (!collection_flags.bit_is_set(loc))
410410
continue;
411411
table[loc] = std::move(e);
@@ -467,7 +467,7 @@ class cache
467467
inline bool contains(const Element& e, const bool erase) const
468468
{
469469
std::array<uint32_t, 8> locs = compute_hashes(e);
470-
for (uint32_t loc : locs)
470+
for (const uint32_t loc : locs)
471471
if (table[loc] == e) {
472472
if (erase)
473473
allow_erase(loc);

src/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ CNode* CConnman::FindNode(const CService& addr)
344344
bool CConnman::CheckIncomingNonce(uint64_t nonce)
345345
{
346346
LOCK(cs_vNodes);
347-
for (CNode* pnode : vNodes) {
347+
for (const CNode* pnode : vNodes) {
348348
if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce)
349349
return false;
350350
}
@@ -1614,7 +1614,7 @@ void CConnman::ThreadDNSAddressSeed()
16141614

16151615
LOCK(cs_vNodes);
16161616
int nRelevant = 0;
1617-
for (auto pnode : vNodes) {
1617+
for (const CNode* pnode : vNodes) {
16181618
nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound;
16191619
}
16201620
if (nRelevant >= 2) {
@@ -1733,7 +1733,7 @@ int CConnman::GetExtraOutboundCount()
17331733
int nOutbound = 0;
17341734
{
17351735
LOCK(cs_vNodes);
1736-
for (CNode* pnode : vNodes) {
1736+
for (const CNode* pnode : vNodes) {
17371737
if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) {
17381738
++nOutbound;
17391739
}
@@ -1803,7 +1803,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18031803
std::set<std::vector<unsigned char> > setConnected;
18041804
{
18051805
LOCK(cs_vNodes);
1806-
for (CNode* pnode : vNodes) {
1806+
for (const CNode* pnode : vNodes) {
18071807
if (!pnode->fInbound && !pnode->m_manual_connection) {
18081808
// Netgroups for inbound and addnode peers are not excluded because our goal here
18091809
// is to not use multiple of our limited outbound slots on a single netgroup

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
882882
// Erase orphan transactions included or precluded by this block
883883
if (vOrphanErase.size()) {
884884
int nErased = 0;
885-
for (uint256 &orphanHash : vOrphanErase) {
885+
for (const uint256& orphanHash : vOrphanErase) {
886886
nErased += EraseOrphanTx(orphanHash);
887887
}
888888
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
@@ -2288,7 +2288,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22882288
}
22892289
}
22902290

2291-
for (uint256 hash : vEraseQueue)
2291+
for (const uint256& hash : vEraseQueue)
22922292
EraseOrphanTx(hash);
22932293
}
22942294
else if (fMissingInputs)

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void BitcoinApplication::requestShutdown()
416416

417417
#ifdef ENABLE_WALLET
418418
window->removeAllWallets();
419-
for (WalletModel *walletModel : m_wallet_models) {
419+
for (const WalletModel* walletModel : m_wallet_models) {
420420
delete walletModel;
421421
}
422422
m_wallet_models.clear();

src/qt/bitcoingui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ void UnitDisplayStatusBarControl::mousePressEvent(QMouseEvent *event)
12761276
void UnitDisplayStatusBarControl::createContextMenu()
12771277
{
12781278
menu = new QMenu(this);
1279-
for (BitcoinUnits::Unit u : BitcoinUnits::availableUnits())
1279+
for (const BitcoinUnits::Unit u : BitcoinUnits::availableUnits())
12801280
{
12811281
QAction *menuAction = new QAction(QString(BitcoinUnits::longName(u)), this);
12821282
menuAction->setData(QVariant(u));

src/qt/peertablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PeerTablePriv
6565
interfaces::Node::NodesStats nodes_stats;
6666
node.getNodesStats(nodes_stats);
6767
cachedNodeStats.reserve(nodes_stats.size());
68-
for (auto& node_stats : nodes_stats)
68+
for (const auto& node_stats : nodes_stats)
6969
{
7070
CNodeCombinedStats stats;
7171
stats.nodeStats = std::get<0>(node_stats);

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ void SendCoinsDialog::on_sendButton_clicked()
348348
questionString.append("<hr />");
349349
CAmount totalAmount = currentTransaction.getTotalTransactionAmount() + txFee;
350350
QStringList alternativeUnits;
351-
for (BitcoinUnits::Unit u : BitcoinUnits::availableUnits())
351+
for (const BitcoinUnits::Unit u : BitcoinUnits::availableUnits())
352352
{
353353
if(u != model->getOptionsModel()->getDisplayUnit())
354354
alternativeUnits.append(BitcoinUnits::formatHtmlWithUnit(u, totalAmount));

src/qt/splashscreen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void SplashScreen::unsubscribeFromCoreSignals()
200200
// Disconnect signals from client
201201
m_handler_init_message->disconnect();
202202
m_handler_show_progress->disconnect();
203-
for (auto& handler : m_connected_wallet_handlers) {
203+
for (const auto& handler : m_connected_wallet_handlers) {
204204
handler->disconnect();
205205
}
206206
m_connected_wallet_handlers.clear();

0 commit comments

Comments
 (0)