Skip to content

Commit 31145a3

Browse files
committed
Merge #13480: Avoid copies in range-for loops and add a warning to detect them
d92204c build: add warning to detect hidden copies in range-for loops (Cory Fields) 466e16e cleanup: avoid hidden copies in range-for loops (Cory Fields) Pull request description: Following-up on #13241, which was itself a follow-up of #12169. See title. Fixing these would otherwise be a continuous process, adding the warning should keep them from cropping up. Note that the warning seems to be Clang-only for now. Tree-SHA512: ccfb769c3128b3f92c95715abcf21ee2496fe2aa384f80efead1529a28eeb56b98995b531b49a089f8142601389e63f7bb935963d724eacde4f5e1b4a024934b
2 parents dc53f7f + d92204c commit 31145a3

File tree

7 files changed

+10
-9
lines changed

7 files changed

+10
-9
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
301301
AX_CHECK_COMPILE_FLAG([-Wvla],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]])
302302
AX_CHECK_COMPILE_FLAG([-Wformat-security],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wformat-security"],,[[$CXXFLAG_WERROR]])
303303
AX_CHECK_COMPILE_FLAG([-Wthread-safety-analysis],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wthread-safety-analysis"],,[[$CXXFLAG_WERROR]])
304+
AX_CHECK_COMPILE_FLAG([-Wrange-loop-analysis],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wrange-loop-analysis"],,[[$CXXFLAG_WERROR]])
304305

305306
## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
306307
## unknown options if any other warning is produced. Test the -Wfoo case, and

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
209209
// segwit activation)
210210
bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
211211
{
212-
for (const CTxMemPool::txiter it : package) {
212+
for (CTxMemPool::txiter it : package) {
213213
if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
214214
return false;
215215
if (!fIncludeWitness && it->GetTx().HasWitness())
@@ -241,7 +241,7 @@ int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& already
241241
indexed_modified_transaction_set &mapModifiedTx)
242242
{
243243
int nDescendantsUpdated = 0;
244-
for (const CTxMemPool::txiter it : alreadyAdded) {
244+
for (CTxMemPool::txiter it : alreadyAdded) {
245245
CTxMemPool::setEntries descendants;
246246
mempool.CalculateDescendants(it, descendants);
247247
// Insert all descendants (not yet in block) into the modified set

src/qt/platformstyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void MakeSingleColorImage(QImage& img, const QColor& colorbase)
4646
QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase)
4747
{
4848
QIcon new_ico;
49-
for (const QSize sz : ico.availableSizes())
49+
for (const QSize& sz : ico.availableSizes())
5050
{
5151
QImage img(ico.pixmap(sz).toImage());
5252
MakeSingleColorImage(img, colorbase);

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash,
853853
ss << hash;
854854
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase ? 1u : 0u);
855855
stats.nTransactions++;
856-
for (const auto output : outputs) {
856+
for (const auto& output : outputs) {
857857
ss << VARINT(output.first + 1);
858858
ss << output.second.out.scriptPubKey;
859859
ss << VARINT(output.second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);

src/txmempool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendan
6969
setAllDescendants.insert(cit);
7070
stageEntries.erase(cit);
7171
const setEntries &setChildren = GetMemPoolChildren(cit);
72-
for (const txiter childEntry : setChildren) {
72+
for (txiter childEntry : setChildren) {
7373
cacheMap::iterator cacheIt = cachedDescendants.find(childEntry);
7474
if (cacheIt != cachedDescendants.end()) {
7575
// We've already calculated this one, just add the entries for this set
7676
// but don't traverse again.
77-
for (const txiter cacheEntry : cacheIt->second) {
77+
for (txiter cacheEntry : cacheIt->second) {
7878
setAllDescendants.insert(cacheEntry);
7979
}
8080
} else if (!setAllDescendants.count(childEntry)) {

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
651651
view.SetBackend(viewMemPool);
652652

653653
// do all inputs exist?
654-
for (const CTxIn txin : tx.vin) {
654+
for (const CTxIn& txin : tx.vin) {
655655
if (!pcoinsTip->HaveCoinInCache(txin.prevout)) {
656656
coins_to_uncache.push_back(txin.prevout);
657657
}
@@ -957,7 +957,7 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
957957
}
958958

959959
// Remove conflicting transactions from the mempool
960-
for (const CTxMemPool::txiter it : allConflicting)
960+
for (CTxMemPool::txiter it : allConflicting)
961961
{
962962
LogPrint(BCLog::MEMPOOL, "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
963963
it->GetTx().GetHash().ToString(),

src/wallet/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ bool WalletInit::Verify() const
200200
// Keep track of each wallet absolute path to detect duplicates.
201201
std::set<fs::path> wallet_paths;
202202

203-
for (const auto wallet_file : wallet_files) {
203+
for (const auto& wallet_file : wallet_files) {
204204
fs::path wallet_path = fs::absolute(wallet_file, GetWalletDir());
205205

206206
if (!wallet_paths.insert(wallet_path).second) {

0 commit comments

Comments
 (0)