mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-02 17:24:58 +02:00
Merge #13249: Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations.
f34c8c466a 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
This commit is contained in:
@@ -155,7 +155,7 @@ static void Correct_Queue_range(std::vector<size_t> range)
|
||||
}
|
||||
// Make vChecks here to save on malloc (this test can be slow...)
|
||||
std::vector<FakeCheckCheckCompletion> vChecks;
|
||||
for (auto i : range) {
|
||||
for (const size_t i : range) {
|
||||
size_t total = i;
|
||||
FakeCheckCheckCompletion::n_calls = 0;
|
||||
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
|
||||
@@ -253,7 +253,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
||||
}
|
||||
|
||||
for (auto times = 0; times < 10; ++times) {
|
||||
for (bool end_fails : {true, false}) {
|
||||
for (const bool end_fails : {true, false}) {
|
||||
CCheckQueueControl<FailingCheck> control(fail_queue.get());
|
||||
{
|
||||
std::vector<FailingCheck> vChecks;
|
||||
|
||||
@@ -736,7 +736,7 @@ static void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount mo
|
||||
template <typename... Args>
|
||||
static void CheckAddCoin(Args&&... args)
|
||||
{
|
||||
for (CAmount base_value : {ABSENT, PRUNED, VALUE1})
|
||||
for (const CAmount base_value : {ABSENT, PRUNED, VALUE1})
|
||||
CheckAddCoinBase(base_value, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
@@ -848,10 +848,10 @@ BOOST_AUTO_TEST_CASE(ccoins_write)
|
||||
// they would be too repetitive (the parent cache is never updated in these
|
||||
// cases). The loop below covers these cases and makes sure the parent cache
|
||||
// is always left unchanged.
|
||||
for (CAmount parent_value : {ABSENT, PRUNED, VALUE1})
|
||||
for (CAmount child_value : {ABSENT, PRUNED, VALUE2})
|
||||
for (char parent_flags : parent_value == ABSENT ? ABSENT_FLAGS : FLAGS)
|
||||
for (char child_flags : child_value == ABSENT ? ABSENT_FLAGS : CLEAN_FLAGS)
|
||||
for (const CAmount parent_value : {ABSENT, PRUNED, VALUE1})
|
||||
for (const CAmount child_value : {ABSENT, PRUNED, VALUE2})
|
||||
for (const char parent_flags : parent_value == ABSENT ? ABSENT_FLAGS : FLAGS)
|
||||
for (const char child_flags : child_value == ABSENT ? ABSENT_FLAGS : CLEAN_FLAGS)
|
||||
CheckWriteCoins(parent_value, child_value, parent_value, parent_flags, child_flags, parent_flags);
|
||||
}
|
||||
|
||||
|
||||
@@ -82,11 +82,11 @@ static double test_cache(size_t megabytes, double load)
|
||||
*/
|
||||
std::vector<uint256> hashes_insert_copy = hashes;
|
||||
/** Do the insert */
|
||||
for (uint256& h : hashes_insert_copy)
|
||||
for (const uint256& h : hashes_insert_copy)
|
||||
set.insert(h);
|
||||
/** Count the hits */
|
||||
uint32_t count = 0;
|
||||
for (uint256& h : hashes)
|
||||
for (const uint256& h : hashes)
|
||||
count += set.contains(h, false);
|
||||
double hit_rate = ((double)count) / ((double)n_insert);
|
||||
return hit_rate;
|
||||
@@ -323,7 +323,7 @@ static void test_cache_generations()
|
||||
reads.push_back(inserts[i]);
|
||||
for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
|
||||
reads.push_back(inserts[i]);
|
||||
for (auto h : inserts)
|
||||
for (const auto& h : inserts)
|
||||
c.insert(h);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
|
||||
BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
{
|
||||
// Perform tests both obfuscated and non-obfuscated.
|
||||
for (bool obfuscate : {false, true}) {
|
||||
for (const bool obfuscate : {false, true}) {
|
||||
fs::path ph = SetDataDir(std::string("dbwrapper").append(obfuscate ? "_true" : "_false"));
|
||||
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
|
||||
char key = 'k';
|
||||
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
BOOST_AUTO_TEST_CASE(dbwrapper_batch)
|
||||
{
|
||||
// Perform tests both obfuscated and non-obfuscated.
|
||||
for (bool obfuscate : {false, true}) {
|
||||
for (const bool obfuscate : {false, true}) {
|
||||
fs::path ph = SetDataDir(std::string("dbwrapper_batch").append(obfuscate ? "_true" : "_false"));
|
||||
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
|
||||
|
||||
@@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
|
||||
BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
|
||||
{
|
||||
// Perform tests both obfuscated and non-obfuscated.
|
||||
for (bool obfuscate : {false, true}) {
|
||||
for (const bool obfuscate : {false, true}) {
|
||||
fs::path ph = SetDataDir(std::string("dbwrapper_iterator").append(obfuscate ? "_true" : "_false"));
|
||||
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
|
||||
|
||||
@@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
|
||||
if (x & 1) BOOST_CHECK(dbw.Write(key, value));
|
||||
}
|
||||
|
||||
for (int seek_start : {0x00, 0x80}) {
|
||||
for (const int seek_start : {0x00, 0x80}) {
|
||||
it->Seek((uint8_t)seek_start);
|
||||
for (unsigned int x=seek_start; x<255; ++x) {
|
||||
uint8_t key;
|
||||
@@ -291,7 +291,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
|
||||
}
|
||||
|
||||
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
|
||||
for (int seek_start : {0, 5}) {
|
||||
for (const int seek_start : {0, 5}) {
|
||||
snprintf(buf, sizeof(buf), "%d", seek_start);
|
||||
StringContentsSerializer seek_key(buf);
|
||||
it->Seek(seek_key);
|
||||
|
||||
@@ -24,7 +24,7 @@ static void ResetArgs(const std::string& strArg)
|
||||
|
||||
// Convert to char*:
|
||||
std::vector<const char*> vecChar;
|
||||
for (std::string& s : vecArg)
|
||||
for (const std::string& s : vecArg)
|
||||
vecChar.push_back(s.c_str());
|
||||
|
||||
std::string error;
|
||||
|
||||
@@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(key_io_invalid)
|
||||
std::string exp_base58string = test[0].get_str();
|
||||
|
||||
// must be invalid as public and as private key
|
||||
for (auto chain : { CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::REGTEST }) {
|
||||
for (const auto& chain : { CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::REGTEST }) {
|
||||
SelectParams(chain);
|
||||
destination = DecodeDestination(exp_base58string);
|
||||
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey in mainnet:" + strTest);
|
||||
|
||||
@@ -146,7 +146,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
|
||||
BOOST_AUTO_TEST_CASE(findearliestatleast_edge_test)
|
||||
{
|
||||
std::list<CBlockIndex> blocks;
|
||||
for (unsigned int timeMax : {100, 100, 100, 200, 200, 200, 300, 300, 300}) {
|
||||
for (const unsigned int timeMax : {100, 100, 100, 200, 200, 200, 300, 300, 300}) {
|
||||
CBlockIndex* prev = blocks.empty() ? nullptr : &blocks.back();
|
||||
blocks.emplace_back();
|
||||
blocks.back().nHeight = prev ? prev->nHeight + 1 : 0;
|
||||
|
||||
@@ -29,7 +29,7 @@ void CConnmanTest::AddNode(CNode& node)
|
||||
void CConnmanTest::ClearNodes()
|
||||
{
|
||||
LOCK(g_connman->cs_vNodes);
|
||||
for (CNode* node : g_connman->vNodes) {
|
||||
for (const CNode* node : g_connman->vNodes) {
|
||||
delete node;
|
||||
}
|
||||
g_connman->vNodes.clear();
|
||||
|
||||
@@ -65,7 +65,7 @@ unsigned int ParseScriptFlags(std::string strFlags)
|
||||
std::vector<std::string> words;
|
||||
boost::algorithm::split(words, strFlags, boost::algorithm::is_any_of(","));
|
||||
|
||||
for (std::string word : words)
|
||||
for (const std::string& word : words)
|
||||
{
|
||||
if (!mapFlagNames.count(word))
|
||||
BOOST_ERROR("Bad test: unknown verification flag '" << word << "'");
|
||||
|
||||
@@ -245,7 +245,7 @@ BOOST_AUTO_TEST_CASE(util_GetBoolArg)
|
||||
testArgs.ParseParameters(7, (char**)argv_test, error);
|
||||
|
||||
// Each letter should be set.
|
||||
for (char opt : "abcdef")
|
||||
for (const char opt : "abcdef")
|
||||
BOOST_CHECK(testArgs.IsArgSet({'-', opt}) || !opt);
|
||||
|
||||
// Nothing else should be in the map
|
||||
@@ -394,7 +394,7 @@ BOOST_AUTO_TEST_CASE(util_ReadConfigStream)
|
||||
&& test_args.GetArg("-iii", "xxx") == "xxx"
|
||||
);
|
||||
|
||||
for (bool def : {false, true}) {
|
||||
for (const bool def : {false, true}) {
|
||||
BOOST_CHECK(test_args.GetBoolArg("-a", def)
|
||||
&& test_args.GetBoolArg("-b", def)
|
||||
&& !test_args.GetBoolArg("-ccc", def)
|
||||
|
||||
Reference in New Issue
Block a user