Skip to content

Commit 9eaef10

Browse files
author
MacroFake
committed
Merge bitcoin/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
2 parents d480586 + ae7ae36 commit 9eaef10

28 files changed

+43
-39
lines changed

src/.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ bugprone-argument-comment,
44
misc-unused-using-decls,
55
modernize-use-default-member-init,
66
modernize-use-nullptr,
7+
performance-for-range-copy,
8+
performance-unnecessary-copy-initialization,
79
readability-redundant-declaration,
810
readability-redundant-string-init,
911
'
@@ -12,6 +14,8 @@ bugprone-argument-comment,
1214
misc-unused-using-decls,
1315
modernize-use-default-member-init,
1416
modernize-use-nullptr,
17+
performance-for-range-copy,
18+
performance-unnecessary-copy-initialization,
1519
readability-redundant-declaration,
1620
readability-redundant-string-init,
1721
'

src/bitcoin-cli.cpp

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

912912
UniValue balances(UniValue::VOBJ);
913913
for (const UniValue& wallet : wallets.getValues()) {
914-
const std::string wallet_name = wallet.get_str();
914+
const std::string& wallet_name = wallet.get_str();
915915
const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name);
916916
const UniValue& balance = find_value(getbalances, "result")["mine"]["trusted"];
917917
balances.pushKV(wallet_name, balance);

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
596596
UniValue prevtxsObj = registers["prevtxs"];
597597
{
598598
for (unsigned int previdx = 0; previdx < prevtxsObj.size(); previdx++) {
599-
UniValue prevOut = prevtxsObj[previdx];
599+
const UniValue& prevOut = prevtxsObj[previdx];
600600
if (!prevOut.isObject())
601601
throw std::runtime_error("expected prevtxs internal object");
602602

src/blockfilter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const std::set<BlockFilterType>& AllBlockFilterTypes()
169169

170170
static std::once_flag flag;
171171
std::call_once(flag, []() {
172-
for (auto entry : g_filter_types) {
172+
for (const auto& entry : g_filter_types) {
173173
types.insert(entry.first);
174174
}
175175
});
@@ -185,7 +185,7 @@ const std::string& ListBlockFilterTypes()
185185
std::call_once(flag, []() {
186186
std::stringstream ret;
187187
bool first = true;
188-
for (auto entry : g_filter_types) {
188+
for (const auto& entry : g_filter_types) {
189189
if (!first) ret << ", ";
190190
ret << entry.second;
191191
first = false;

src/coins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) cons
296296
try {
297297
return CCoinsViewBacked::GetCoin(outpoint, coin);
298298
} catch(const std::runtime_error& e) {
299-
for (auto f : m_err_callbacks) {
299+
for (const auto& f : m_err_callbacks) {
300300
f();
301301
}
302302
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
@@ -265,7 +265,7 @@ int ParseSighashString(const UniValue& sighash)
265265
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
266266
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
267267
};
268-
std::string strHashType = sighash.get_str();
268+
const std::string& strHashType = sighash.get_str();
269269
const auto& it = map_sighash_values.find(strHashType);
270270
if (it != map_sighash_values.end()) {
271271
hash_type = it->second;

src/external_signer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalS
2828
if (!result.isArray()) {
2929
throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));
3030
}
31-
for (UniValue signer : result.getValues()) {
31+
for (const UniValue& signer : result.getValues()) {
3232
// Check for error
3333
const UniValue& error = find_value(signer, "error");
3434
if (!error.isNull()) {

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10461046
static bool LockDataDirectory(bool probeOnly)
10471047
{
10481048
// Make sure only a single Bitcoin process is using the data directory.
1049-
fs::path datadir = gArgs.GetDataDirNet();
1049+
const fs::path& datadir = gArgs.GetDataDirNet();
10501050
if (!DirIsWritable(datadir)) {
10511051
return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), fs::PathToString(datadir)));
10521052
}

src/logging.cpp

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

366366
if (m_log_threadnames && m_started_new_line) {
367-
const auto threadname = util::ThreadGetInternalName();
367+
const auto& threadname = util::ThreadGetInternalName();
368368
str_prefixed.insert(0, "[" + (threadname.empty() ? "unknown" : threadname) + "] ");
369369
}
370370

src/netbase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
387387
return error("Error sending to proxy");
388388
}
389389
uint8_t pchRet1[2];
390-
if ((recvr = InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
390+
if (InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
391391
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
392392
return false;
393393
}
@@ -410,7 +410,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
410410
}
411411
LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
412412
uint8_t pchRetA[2];
413-
if ((recvr = InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
413+
if (InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
414414
return error("Error reading proxy authentication response");
415415
}
416416
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
@@ -476,7 +476,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
476476
if (recvr != IntrRecvError::OK) {
477477
return error("Error reading from proxy");
478478
}
479-
if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
479+
if (InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
480480
return error("Error reading from proxy");
481481
}
482482
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);

0 commit comments

Comments
 (0)