Skip to content

Commit a992cba

Browse files
MacroFakeknst
authored andcommitted
Merge bitcoin#25707: refactor: Make const references to avoid unnecessarily copying objects and enable two clang-tidy checks
ae7ae36 tidy: Enable two clang-tidy checks (Aurèle Oulès) 081b0e5 refactor: Make const refs vars where applicable (Aurèle Oulès) Pull request description: I added const references to some variables to avoid unnecessarily copying objects. Also added two clang-tidy checks : [performance-for-range-copy](https://releases.llvm.org/11.1.0/tools/clang/tools/extra/docs/clang-tidy/checks/performance-for-range-copy.html) and [performance-unnecessary-copy-initialization](https://releases.llvm.org/12.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/performance-unnecessary-copy-initialization.html). ACKs for top commit: vasild: ACK ae7ae36 MarcoFalke: review ACK ae7ae36 Tree-SHA512: f6ac6b0cd0eee1e0c34d2f186484bc0f7ec6071451cccb33fa88a67d93d92b304e2fac378b88f087e94657745bca4e966dbc443759587400eb01b1f3061fde8c
1 parent 0514237 commit a992cba

25 files changed

+39
-35
lines changed

src/.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ bugprone-use-after-move,
55
misc-unused-using-decls,
66
modernize-use-default-member-init,
77
modernize-use-nullptr,
8+
performance-for-range-copy,
9+
performance-unnecessary-copy-initialization,
810
readability-const-return-type,
911
readability-redundant-declaration,
1012
readability-redundant-string-init,
@@ -15,6 +17,8 @@ bugprone-use-after-move,
1517
misc-unused-using-decls,
1618
modernize-use-default-member-init,
1719
modernize-use-nullptr,
20+
performance-for-range-copy,
21+
performance-unnecessary-copy-initialization,
1822
readability-redundant-declaration,
1923
readability-redundant-string-init,
2024
'

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ static void GetWalletBalances(UniValue& result)
948948

949949
UniValue balances(UniValue::VOBJ);
950950
for (const UniValue& wallet : wallets.getValues()) {
951-
const std::string wallet_name = wallet.get_str();
951+
const std::string& wallet_name = wallet.get_str();
952952
const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name);
953953
const UniValue& balance = getbalances.find_value("result")["mine"]["trusted"];
954954
balances.pushKV(wallet_name, balance);

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
555555
UniValue prevtxsObj = registers["prevtxs"];
556556
{
557557
for (unsigned int previdx = 0; previdx < prevtxsObj.size(); previdx++) {
558-
UniValue prevOut = prevtxsObj[previdx];
558+
const UniValue& prevOut = prevtxsObj[previdx];
559559
if (!prevOut.isObject())
560560
throw std::runtime_error("expected prevtxs internal object");
561561

src/blockfilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const std::set<BlockFilterType>& AllBlockFilterTypes()
170170

171171
static std::once_flag flag;
172172
std::call_once(flag, []() {
173-
for (auto entry : g_filter_types) {
173+
for (const auto& entry : g_filter_types) {
174174
types.insert(entry.first);
175175
}
176176
});

src/coins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) cons
356356
try {
357357
return CCoinsViewBacked::GetCoin(outpoint, coin);
358358
} catch(const std::runtime_error& e) {
359-
for (auto f : m_err_callbacks) {
359+
for (const auto& f : m_err_callbacks) {
360360
f();
361361
}
362362
LogPrintf("Error reading from database: %s\n", e.what());

src/core_read.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int ParseSighashString(const UniValue& sighash)
189189
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
190190
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
191191
};
192-
std::string strHashType = sighash.get_str();
192+
const std::string& strHashType = sighash.get_str();
193193
const auto& it = map_sighash_values.find(strHashType);
194194
if (it != map_sighash_values.end()) {
195195
hash_type = it->second;

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
14331433
static bool LockDataDirectory(bool probeOnly)
14341434
{
14351435
// Make sure only a single Dash Core process is using the data directory.
1436-
fs::path datadir = gArgs.GetDataDirNet();
1436+
const fs::path& datadir = gArgs.GetDataDirNet();
14371437
if (!DirIsWritable(datadir)) {
14381438
return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), fs::PathToString(datadir)));
14391439
}

src/logging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& loggi
462462
}
463463

464464
if (m_log_threadnames && m_started_new_line) {
465-
const auto threadname = util::ThreadGetInternalName();
465+
const auto& threadname = util::ThreadGetInternalName();
466466
// 16 chars total, "dash-" is 5 of them and another 1 is a NUL terminator
467467
str_prefixed.insert(0, "[" + strprintf("%10s", (threadname.empty() ? "unknown" : threadname)) + "] ");
468468
}

src/netbase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
390390
return error("Error sending to proxy");
391391
}
392392
uint8_t pchRet1[2];
393-
if ((recvr = InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
393+
if (InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
394394
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
395395
return false;
396396
}
@@ -413,7 +413,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
413413
}
414414
LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
415415
uint8_t pchRetA[2];
416-
if ((recvr = InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
416+
if (InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
417417
return error("Error reading proxy authentication response");
418418
}
419419
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
@@ -479,7 +479,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
479479
if (recvr != IntrRecvError::OK) {
480480
return error("Error reading from proxy");
481481
}
482-
if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
482+
if (InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
483483
return error("Error reading from proxy");
484484
}
485485
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);

src/node/blockstorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void CleanupBlockRevFiles()
448448
// Remove the rev files immediately and insert the blk file paths into an
449449
// ordered map keyed by block file index.
450450
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
451-
fs::path blocksdir = gArgs.GetBlocksDirPath();
451+
const fs::path& blocksdir = gArgs.GetBlocksDirPath();
452452
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
453453
const std::string path = fs::PathToString(it->path().filename());
454454
if (fs::is_regular_file(*it) &&

0 commit comments

Comments
 (0)