Submitting as draft pull request: seeking Concept ACK:s.
Tautological run-time checks may be an indication of incorrect logic. Enabling compiler warnings for tautological checks can help identify such issues early in the development cycle.
If it is clear already at compile-time that a run-time check is tautological it is better to make it a compile-time check using static_assert
(if possible and assuming the check should be kept).
Compile-time checking is preferred over run-time checking as noted in the developer notes:
static_assert
is preferred overassert
where possible. Generally; compile-time checking is preferred over run-time checking.
Suggested changes in this PR:
- Commit 1. Make compiler warn about tautological comparisons (
-Wtautological-constant-in-range-compare
in Clang) - Commit 2. Get rid of current warnings by replacing the run-time tautological comparisons with equivalent compile-time assertions (
static_assert
)
Compiler output given -Wtautological-constant-in-range-compare
before this PR:
0$ make
1util/strencodings.cpp:304:11: warning: result of comparison 'long long' >= -9223372036854775808 is always true [-Wtautological-type-limit-compare]
2 n >= std::numeric_limits<int64_t>::min() &&
3 ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4util/strencodings.cpp:305:11: warning: result of comparison 'long long' <= 9223372036854775807 is always true [-Wtautological-type-limit-compare]
5 n <= std::numeric_limits<int64_t>::max();
6 ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7util/strencodings.cpp:338:11: warning: result of comparison 'unsigned long long' <= 18446744073709551615 is always true [-Wtautological-type-limit-compare]
8 n <= std::numeric_limits<uint64_t>::max();
9 ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10core_write.cpp:100:15: warning: result of comparison of 0 <= unsigned enum expression is always true [-Wtautological-unsigned-enum-zero-compare]
11 if (0 <= opcode && opcode <= OP_PUSHDATA4) {
12 ~ ^ ~~~~~~
13script/script.h:449:20: warning: result of comparison of unsigned enum expression < 0 is always false [-Wtautological-unsigned-enum-zero-compare]
14 if (opcode < 0 || opcode > 0xff)
15 ~~~~~~ ^ ~
16script/script.h:449:34: warning: result of comparison 'opcodetype' > 255 is always false [-Wtautological-type-limit-compare]
17 if (opcode < 0 || opcode > 0xff)
18 ~~~~~~ ^ ~~~~
19script/interpreter.cpp:343:28: warning: result of comparison of 0 <= unsigned enum expression is always true [-Wtautological-unsigned-enum-zero-compare]
20 if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4) {
21 ~ ^ ~~~~~~
22$
Compiler output given -Wtautological-constant-in-range-compare
after this PR:
0$ make
1$