mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-17 23:40:51 +02:00
Merge #14088: tests: Don't assert(...) with side effects
ca1a093127c11bb2aea10bf96c38dbfb40f8d170 Add regression test: Don't assert(...) with side effects (practicalswift) 4c3c9c38699360f93d3c52a01a90ff15ee5e1a62 Don't assert(...) with side effects (practicalswift) Pull request description: Don't `assert(...)` with side effects. From the developer notes: > **Assertions should not have side-effects** > > Rationale: Even though the source code is set to refuse to compile with assertions disabled, having side-effects in assertions is unexpected and makes the code harder to understand These assertions were introduced quite recently (in #14069 which was merged two days ago) and since this is a recurring thing (see #13534 – "Don't assert(foo()) where foo() has side effects" from May) I added a simple regression test for the most obvious common side effect. Tree-SHA512: be65db9d8d5d0f5752152ba73fe3fbb0531880f156d3cd7dfdf1752709979b63214e46ae64b1adbe1e09fa121278f4087f4ae49bff16cf8f5aec16ea6bde3650
This commit is contained in:
commit
709a15b0a6
@ -138,11 +138,13 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
|
|||||||
// the callbacks should run in exactly the order in which they were enqueued
|
// the callbacks should run in exactly the order in which they were enqueued
|
||||||
for (int i = 0; i < 100; ++i) {
|
for (int i = 0; i < 100; ++i) {
|
||||||
queue1.AddToProcessQueue([i, &counter1]() {
|
queue1.AddToProcessQueue([i, &counter1]() {
|
||||||
assert(i == counter1++);
|
bool expectation = i == counter1++;
|
||||||
|
assert(expectation);
|
||||||
});
|
});
|
||||||
|
|
||||||
queue2.AddToProcessQueue([i, &counter2]() {
|
queue2.AddToProcessQueue([i, &counter2]() {
|
||||||
assert(i == counter2++);
|
bool expectation = i == counter2++;
|
||||||
|
assert(expectation);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
test/lint/lint-assertions.sh
Executable file
23
test/lint/lint-assertions.sh
Executable file
@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# Copyright (c) 2018 The Bitcoin Core developers
|
||||||
|
# Distributed under the MIT software license, see the accompanying
|
||||||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
#
|
||||||
|
# Check for assertions with obvious side effects.
|
||||||
|
|
||||||
|
export LC_ALL=C
|
||||||
|
|
||||||
|
EXIT_CODE=0
|
||||||
|
|
||||||
|
# PRE31-C (SEI CERT C Coding Standard):
|
||||||
|
# "Assertions should not contain assignments, increment, or decrement operators."
|
||||||
|
OUTPUT=$(git grep -E '[^_]assert\(.*(\+\+|\-\-|[^=!<>]=[^=!<>]).*\);' -- "*.cpp" "*.h")
|
||||||
|
if [[ ${OUTPUT} != "" ]]; then
|
||||||
|
echo "Assertions should not have side effects:"
|
||||||
|
echo
|
||||||
|
echo "${OUTPUT}"
|
||||||
|
EXIT_CODE=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit ${EXIT_CODE}
|
Loading…
x
Reference in New Issue
Block a user