Skip to content

Commit f13e03c

Browse files
author
MarcoFalke
committed
Merge #20584: Declare de facto const reference variables/member functions as const
31b136e Don't declare de facto const reference variables as non-const (practicalswift) 1c65c07 Don't declare de facto const member functions as non-const (practicalswift) Pull request description: _Meta: This is the second and final part of the `const` refactoring series (part one: #20581). **I promise: no more refactoring PRs from me in a while! :)** I'll now go back to focusing on fuzzing/hardening!_ Changes in this PR: * Don't declare de facto const member functions as non-const * Don't declare de facto const reference variables as non-const Awards for finding candidates for the above changes go to: * `clang-tidy`'s [`readability-make-member-function-const`](https://clang.llvm.org/extra/clang-tidy/checks/readability-make-member-function-const.html) check ([list of `clang-tidy` checks](https://clang.llvm.org/extra/clang-tidy/checks/list.html)) * `cppcheck`'s `constVariable` check ([list of `cppcheck` checks](https://sourceforge.net/p/cppcheck/wiki/ListOfChecks/)) See #18920 for instructions on how to analyse Bitcoin Core using Clang Static Analysis, `clang-tidy` and `cppcheck`. ACKs for top commit: ajtowns: ACK 31b136e jonatack: ACK 31b136e theStack: ACK 31b136e ❄️ Tree-SHA512: f58f8f00744219426874379e9f3e9331132b9b48e954d24f3a85cbb858fdcc98009ed42ef7e7b4619ae8af9fc240a6d8bfc1c438db2e97b0ecd722a80dcfeffe
2 parents 3b6d1b6 + 31b136e commit f13e03c

17 files changed

+27
-27
lines changed

src/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
617617
return CAddrInfo();
618618
}
619619

620-
CAddrInfo& newInfo = mapInfo[id_new];
620+
const CAddrInfo& newInfo = mapInfo[id_new];
621621

622622
// which tried bucket to move the entry to
623623
int tried_bucket = newInfo.GetTriedBucket(nKey, m_asmap);

src/logging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
174174
return false;
175175
}
176176

177-
std::vector<LogCategory> BCLog::Logger::LogCategoriesList()
177+
std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
178178
{
179179
std::vector<LogCategory> ret;
180180
for (const CLogCategoryDesc& category_desc : LogCategories) {

src/logging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ namespace BCLog {
135135

136136
bool WillLogCategory(LogFlags category) const;
137137
/** Returns a vector of the log categories */
138-
std::vector<LogCategory> LogCategoriesList();
138+
std::vector<LogCategory> LogCategoriesList() const;
139139
/** Returns a string with the log categories */
140-
std::string LogCategoriesString()
140+
std::string LogCategoriesString() const
141141
{
142142
return Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; });
143143
};

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
213213
// - transaction finality (locktime)
214214
// - premature witness (in case segwit transactions are added to mempool before
215215
// segwit activation)
216-
bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
216+
bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package) const
217217
{
218218
for (CTxMemPool::txiter it : package) {
219219
if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class BlockAssembler
185185
* locktime, premature-witness, serialized size (if necessary)
186186
* These checks should always succeed, and they're here
187187
* only as an extra check in case of suboptimal node configuration */
188-
bool TestPackageTransactions(const CTxMemPool::setEntries& package);
188+
bool TestPackageTransactions(const CTxMemPool::setEntries& package) const;
189189
/** Return true if given transaction from mapTx has already been evaluated,
190190
* or if the transaction's cached data in mapTx is incorrect. */
191191
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set& mapModifiedTx, CTxMemPool::setEntries& failedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ void CConnman::NotifyNumConnectionsChanged()
12061206
}
12071207
}
12081208

1209-
void CConnman::InactivityCheck(CNode *pnode)
1209+
void CConnman::InactivityCheck(CNode *pnode) const
12101210
{
12111211
int64_t nTime = GetSystemTimeInSeconds();
12121212
if (nTime - pnode->nTimeConnected > m_peer_connect_timeout)

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class CConnman
420420
void AcceptConnection(const ListenSocket& hListenSocket);
421421
void DisconnectNodes();
422422
void NotifyNumConnectionsChanged();
423-
void InactivityCheck(CNode *pnode);
423+
void InactivityCheck(CNode *pnode) const;
424424
bool GenerateSelectSet(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
425425
void SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
426426
void SocketHandler();

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ NodeContext& EnsureNodeContext(const util::Ref& context)
6666

6767
CTxMemPool& EnsureMemPool(const util::Ref& context)
6868
{
69-
NodeContext& node = EnsureNodeContext(context);
69+
const NodeContext& node = EnsureNodeContext(context);
7070
if (!node.mempool) {
7171
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
7272
}
@@ -75,7 +75,7 @@ CTxMemPool& EnsureMemPool(const util::Ref& context)
7575

7676
ChainstateManager& EnsureChainman(const util::Ref& context)
7777
{
78-
NodeContext& node = EnsureNodeContext(context);
78+
const NodeContext& node = EnsureNodeContext(context);
7979
if (!node.chainman) {
8080
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
8181
}

src/script/interpreter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ class ConditionStack {
305305
uint32_t m_first_false_pos = NO_FALSE;
306306

307307
public:
308-
bool empty() { return m_stack_size == 0; }
309-
bool all_true() { return m_first_false_pos == NO_FALSE; }
308+
bool empty() const { return m_stack_size == 0; }
309+
bool all_true() const { return m_first_false_pos == NO_FALSE; }
310310
void push_back(bool f)
311311
{
312312
if (m_first_false_pos == NO_FALSE && !f) {

src/script/sign.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, C
388388
bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
389389
{
390390
assert(nIn < txTo.vin.size());
391-
CTxIn& txin = txTo.vin[nIn];
391+
const CTxIn& txin = txTo.vin[nIn];
392392
assert(txin.prevout.n < txFrom.vout.size());
393393
const CTxOut& txout = txFrom.vout[txin.prevout.n];
394394

src/test/checkqueue_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static const unsigned int QUEUE_BATCH_SIZE = 128;
2626
static const int SCRIPT_CHECK_THREADS = 3;
2727

2828
struct FakeCheck {
29-
bool operator()()
29+
bool operator()() const
3030
{
3131
return true;
3232
}
@@ -47,7 +47,7 @@ struct FailingCheck {
4747
bool fails;
4848
FailingCheck(bool _fails) : fails(_fails){};
4949
FailingCheck() : fails(true){};
50-
bool operator()()
50+
bool operator()() const
5151
{
5252
return !fails;
5353
}
@@ -76,7 +76,7 @@ struct UniqueCheck {
7676
struct MemoryCheck {
7777
static std::atomic<size_t> fake_allocated_memory;
7878
bool b {false};
79-
bool operator()()
79+
bool operator()() const
8080
{
8181
return true;
8282
}
@@ -107,7 +107,7 @@ struct FrozenCleanupCheck {
107107
// Freezing can't be the default initialized behavior given how the queue
108108
// swaps in default initialized Checks.
109109
bool should_freeze {false};
110-
bool operator()()
110+
bool operator()() const
111111
{
112112
return true;
113113
}

src/test/util/setup_common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ TestChain100Setup::~TestChain100Setup()
239239
gArgs.ForceSetArg("-segwitheight", "0");
240240
}
241241

242-
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx)
242+
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const
243243
{
244244
return FromTx(MakeTransactionRef(tx));
245245
}
246246

247-
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx)
247+
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const
248248
{
249249
return CTxMemPoolEntry(tx, nFee, nTime, nHeight,
250250
spendsCoinbase, sigOpCost, lp);

src/test/util/setup_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ struct TestMemPoolEntryHelper
145145
nFee(0), nTime(0), nHeight(1),
146146
spendsCoinbase(false), sigOpCost(4) { }
147147

148-
CTxMemPoolEntry FromTx(const CMutableTransaction& tx);
149-
CTxMemPoolEntry FromTx(const CTransactionRef& tx);
148+
CTxMemPoolEntry FromTx(const CMutableTransaction& tx) const;
149+
CTxMemPoolEntry FromTx(const CTransactionRef& tx) const;
150150

151151
// Change the default value
152152
TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
637637
LockPoints lp;
638638
m_view.SetBackend(m_viewmempool);
639639

640-
CCoinsViewCache& coins_cache = ::ChainstateActive().CoinsTip();
640+
const CCoinsViewCache& coins_cache = ::ChainstateActive().CoinsTip();
641641
// do all inputs exist?
642642
for (const CTxIn& txin : tx.vin) {
643643
if (!coins_cache.HaveCoinInCache(txin.prevout)) {

src/wallet/feebumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ Result CommitTransaction(CWallet& wallet, const uint256& txid, CMutableTransacti
256256
errors.push_back(Untranslated("Invalid or non-wallet transaction id"));
257257
return Result::MISC_ERROR;
258258
}
259-
CWalletTx& oldWtx = it->second;
259+
const CWalletTx& oldWtx = it->second;
260260

261261
// make sure the transaction still has no descendants and hasn't been mined in the meantime
262262
Result result = PreconditionChecks(wallet, oldWtx, errors);

src/wallet/wallet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void CWallet::AddToSpends(const uint256& wtxid)
570570
{
571571
auto it = mapWallet.find(wtxid);
572572
assert(it != mapWallet.end());
573-
CWalletTx& thisTx = it->second;
573+
const CWalletTx& thisTx = it->second;
574574
if (thisTx.IsCoinBase()) // Coinbases don't spend anything!
575575
return;
576576

@@ -1054,7 +1054,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
10541054
// Can't mark abandoned if confirmed or in mempool
10551055
auto it = mapWallet.find(hashTx);
10561056
assert(it != mapWallet.end());
1057-
CWalletTx& origtx = it->second;
1057+
const CWalletTx& origtx = it->second;
10581058
if (origtx.GetDepthInMainChain() != 0 || origtx.InMempool()) {
10591059
return false;
10601060
}
@@ -2709,7 +2709,7 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uin
27092709
return locktime;
27102710
}
27112711

2712-
OutputType CWallet::TransactionChangeType(const Optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend)
2712+
OutputType CWallet::TransactionChangeType(const Optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend) const
27132713
{
27142714
// If -changetype is specified, always use that change type.
27152715
if (change_type) {

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
930930
Balance GetBalance(int min_depth = 0, bool avoid_reuse = true) const;
931931
CAmount GetAvailableBalance(const CCoinControl* coinControl = nullptr) const;
932932

933-
OutputType TransactionChangeType(const Optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend);
933+
OutputType TransactionChangeType(const Optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend) const;
934934

935935
/**
936936
* Insert additional inputs into the transaction by

0 commit comments

Comments
 (0)