Skip to content

Commit cc88003

Browse files
DashCoreAutoGuixMarcoFalke
authored andcommitted
Merge bitcoin#23829: refactor: use braced init for integer literals instead of c style casts
f2fc03e refactor: use braced init for integer constants instead of c style casts (Pasta) Pull request description: See bitcoin#23810 for more context. This is broken out from that PR, as it is less breaking, and should be trivial to review and merge. EDIT: Long term, the intention is to remove all C-style casts, as they can dangerously introduce reinterpret_casts. This is one step which removes a number of trivially removable C-style casts ACKs for top commit: aureleoules: ACK f2fc03e Tree-SHA512: 2fd11b92c9147e3f970ec3e130e3b3dce70e707ff02950a8c697d4b111ddcbbfa16915393db20cfc8f384bc76f13241c9b994a187987fcecd16a61f8cc0af14c Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
1 parent b483600 commit cc88003

21 files changed

+44
-44
lines changed

src/bench/coin_selection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
9595
utxo_pool.clear();
9696
CAmount target = 0;
9797
for (int i = 0; i < utxos; ++i) {
98-
target += (CAmount)1 << (utxos+i);
99-
add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
100-
add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
98+
target += CAmount{1} << (utxos+i);
99+
add_coin(CAmount{1} << (utxos+i), 2*i, utxo_pool);
100+
add_coin((CAmount{1} << (utxos+i)) + (CAmount{1} << (utxos-1-i)), 2*i + 1, utxo_pool);
101101
}
102102
return target;
103103
}

src/crypto/siphash.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val)
119119
SIPROUND;
120120
SIPROUND;
121121
v0 ^= d;
122-
v3 ^= ((uint64_t)4) << 59;
122+
v3 ^= (uint64_t{4}) << 59;
123123
SIPROUND;
124124
SIPROUND;
125-
v0 ^= ((uint64_t)4) << 59;
125+
v0 ^= (uint64_t{4}) << 59;
126126
v2 ^= 0xFF;
127127
SIPROUND;
128128
SIPROUND;
@@ -159,7 +159,7 @@ uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint3
159159
SIPROUND;
160160
SIPROUND;
161161
v0 ^= d;
162-
d = (((uint64_t)36) << 56) | extra;
162+
d = ((uint64_t{36}) << 56) | extra;
163163
v3 ^= d;
164164
SIPROUND;
165165
SIPROUND;

src/cuckoocache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class cache
342342
collection_flags.setup(size);
343343
epoch_flags.resize(size);
344344
// Set to 45% as described above
345-
epoch_size = std::max((uint32_t)1, (45 * size) / 100);
345+
epoch_size = std::max(uint32_t{1}, (45 * size) / 100);
346346
// Initially set to wait for a whole epoch
347347
epoch_heuristic_counter = epoch_size;
348348
return size;

src/random.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class FastRandomContext
192192
return rand64() >> (64 - bits);
193193
} else {
194194
if (bitbuf_size < bits) FillBitBuffer();
195-
uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
195+
uint64_t ret = bitbuf & (~uint64_t{0} >> (64 - bits));
196196
bitbuf >>= bits;
197197
bitbuf_size -= bits;
198198
return ret;

src/script/interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ class CTransactionSignatureSerializer
14261426
// Serialize the nSequence
14271427
if (nInput != nIn && (fHashSingle || fHashNone))
14281428
// let the others update at will
1429-
::Serialize(s, (int)0);
1429+
::Serialize(s, int{0});
14301430
else
14311431
::Serialize(s, txTo.vin[nInput].nSequence);
14321432
}

src/test/checkqueue_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ static void Correct_Queue_range(std::vector<size_t> range)
193193
BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Zero)
194194
{
195195
std::vector<size_t> range;
196-
range.push_back((size_t)0);
196+
range.push_back(size_t{0});
197197
Correct_Queue_range(range);
198198
}
199199
/** Test that 1 check is correct
200200
*/
201201
BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_One)
202202
{
203203
std::vector<size_t> range;
204-
range.push_back((size_t)1);
204+
range.push_back(size_t{1});
205205
Correct_Queue_range(range);
206206
}
207207
/** Test that MAX check is correct

src/test/fuzz/versionbits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class TestConditionChecker : public AbstractThresholdConditionChecker
5656

5757
bool Condition(int32_t version) const
5858
{
59-
uint32_t mask = ((uint32_t)1) << m_bit;
59+
uint32_t mask = (uint32_t{1}) << m_bit;
6060
return (((version & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) && (version & mask) != 0);
6161
}
6262

src/test/merkle_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
5050
// For each of the lower bits in count that are 0, do 1 step. Each
5151
// corresponds to an inner value that existed before processing the
5252
// current leaf, and each needs a hash to combine it.
53-
for (level = 0; !(count & (((uint32_t)1) << level)); level++) {
53+
for (level = 0; !(count & ((uint32_t{1}) << level)); level++) {
5454
if (pbranch) {
5555
if (matchh) {
5656
pbranch->push_back(inner[level]);
@@ -74,12 +74,12 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
7474
int level = 0;
7575
// As long as bit number level in count is zero, skip it. It means there
7676
// is nothing left at this level.
77-
while (!(count & (((uint32_t)1) << level))) {
77+
while (!(count & ((uint32_t{1}) << level))) {
7878
level++;
7979
}
8080
uint256 h = inner[level];
8181
bool matchh = matchlevel == level;
82-
while (count != (((uint32_t)1) << level)) {
82+
while (count != ((uint32_t{1}) << level)) {
8383
// If we reach this point, h is an inner value that is not the top.
8484
// We combine it with itself (Bitcoin's special rule for odd levels in
8585
// the tree) to produce a higher level one.
@@ -89,10 +89,10 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
8989
h = Hash(h, h);
9090
// Increment count to the value it would have if two entries at this
9191
// level had existed.
92-
count += (((uint32_t)1) << level);
92+
count += ((uint32_t{1}) << level);
9393
level++;
9494
// And propagate the result upwards accordingly.
95-
while (!(count & (((uint32_t)1) << level))) {
95+
while (!(count & ((uint32_t{1}) << level))) {
9696
if (pbranch) {
9797
if (matchh) {
9898
pbranch->push_back(inner[level]);

src/test/random_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(fastrandom_randbits)
9595
for (int j = 0; j < 1000; ++j) {
9696
uint64_t rangebits = ctx1.randbits(bits);
9797
BOOST_CHECK_EQUAL(rangebits >> bits, 0U);
98-
uint64_t range = ((uint64_t)1) << bits | rangebits;
98+
uint64_t range = (uint64_t{1}) << bits | rangebits;
9999
uint64_t rand = ctx2.randrange(range);
100100
BOOST_CHECK(rand < range);
101101
}

src/test/serialize_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ BOOST_AUTO_TEST_CASE(varints_bitpatterns)
123123
CDataStream ss(SER_DISK, 0);
124124
ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
125125
ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
126-
ss << VARINT_MODE((int8_t)0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
126+
ss << VARINT_MODE(int8_t{0x7f}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
127127
ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
128-
ss << VARINT((uint8_t)0x80); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
128+
ss << VARINT(uint8_t{0x80}); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
129129
ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
130-
ss << VARINT_MODE((int16_t)0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
130+
ss << VARINT_MODE(int16_t{0x1234}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
131131
ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
132-
ss << VARINT((uint16_t)0xffff); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
132+
ss << VARINT(uint16_t{0xffff}); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
133133
ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
134-
ss << VARINT_MODE((int32_t)0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
134+
ss << VARINT_MODE(int32_t{0x123456}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
135135
ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
136-
ss << VARINT((uint32_t)0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
136+
ss << VARINT(uint32_t{0x80123456U}); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
137137
ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
138138
ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
139139
ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();

0 commit comments

Comments
 (0)