Skip to content

Commit bf6253c

Browse files
MacroFakeknst
authored andcommitted
partial Merge bitcoin#25707: refactor: Make const references to avoid unnecessarily copying objects and enable two clang-tidy checks
BACKPORT NOTE: Somehow there's multiple warnings that are for backported code, mostly related to mempool, such as: rpc/mempool.cpp:549:33: error: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy,-warnings-as-errors] 549 | for (CTxMemPool::txiter descendantIt : setDescendants) { | ^ | const & I can't find a way to fix it without big diversity of codebase, so, make as partial temporary 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 2f53656 commit bf6253c

34 files changed

+57
-56
lines changed

src/.clang-tidy

Lines changed: 3 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,7 @@ bugprone-use-after-move,
1517
misc-unused-using-decls,
1618
modernize-use-default-member-init,
1719
modernize-use-nullptr,
20+
performance-unnecessary-copy-initialization,
1821
readability-redundant-declaration,
1922
readability-redundant-string-init,
2023
'

src/bench/load_external.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
2828
// block data) as a stream object.
2929
const fs::path blkfile{testing_setup.get()->m_path_root / "blk.dat"};
3030
CDataStream ss(SER_DISK, 0);
31-
auto params{Params()};
31+
const auto& params{Params()};
3232
ss << params.MessageStart();
3333
ss << static_cast<uint32_t>(benchmark::data::block813851.size());
3434
// We can't use the streaming serialization (ss << benchmark::data::block813851)

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/bls/bls_worker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ template <typename T>
1515
bool VerifyVectorHelper(Span<T> vec)
1616
{
1717
std::set<uint256> set;
18-
for (auto item : vec) {
18+
for (const auto& item : vec) {
1919
if (!item.IsValid())
2020
return false;
2121
// check duplicates

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/evo/creditpool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock(const gsl::n
9191
LogPrintf("%s: WARNING: No valid CbTx at height=%d\n", __func__, block_index->nHeight);
9292
return std::nullopt;
9393
}
94-
for (CTransactionRef tx : block.vtx) {
94+
for (const CTransactionRef& tx : block.vtx) {
9595
if (!tx->IsSpecialTxVersion() || tx->nType != TRANSACTION_ASSET_UNLOCK) continue;
9696

9797
CAmount unlocked{0};

src/evo/mnauth.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CActiveMasternode
3636
if (Params().NetworkIDString() != CBaseChainParams::MAIN && gArgs.IsArgSet("-pushversion")) {
3737
nOurNodeVersion = gArgs.GetIntArg("-pushversion", PROTOCOL_VERSION);
3838
}
39-
auto pk = mn_activeman.GetPubKey();
40-
const CBLSPublicKey pubKey(pk);
41-
const uint256 signHash{::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn(), nOurNodeVersion))};
39+
const uint256 signHash{::SerializeHash(std::make_tuple(mn_activeman.GetPubKey(), receivedMNAuthChallenge, peer.IsInboundConn(), nOurNodeVersion))};
4240

4341
mnauth.proRegTxHash = mn_activeman.GetProTxHash();
4442

0 commit comments

Comments
 (0)