Skip to content

Commit 709a15b

Browse files
committed
Merge #14088: tests: Don't assert(...) with side effects
ca1a093 Add regression test: Don't assert(...) with side effects (practicalswift) 4c3c9c3 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
2 parents bdbd654 + ca1a093 commit 709a15b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/test/scheduler_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,13 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
138138
// the callbacks should run in exactly the order in which they were enqueued
139139
for (int i = 0; i < 100; ++i) {
140140
queue1.AddToProcessQueue([i, &counter1]() {
141-
assert(i == counter1++);
141+
bool expectation = i == counter1++;
142+
assert(expectation);
142143
});
143144

144145
queue2.AddToProcessQueue([i, &counter2]() {
145-
assert(i == counter2++);
146+
bool expectation = i == counter2++;
147+
assert(expectation);
146148
});
147149
}
148150

test/lint/lint-assertions.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2018 The Bitcoin Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
#
7+
# Check for assertions with obvious side effects.
8+
9+
export LC_ALL=C
10+
11+
EXIT_CODE=0
12+
13+
# PRE31-C (SEI CERT C Coding Standard):
14+
# "Assertions should not contain assignments, increment, or decrement operators."
15+
OUTPUT=$(git grep -E '[^_]assert\(.*(\+\+|\-\-|[^=!<>]=[^=!<>]).*\);' -- "*.cpp" "*.h")
16+
if [[ ${OUTPUT} != "" ]]; then
17+
echo "Assertions should not have side effects:"
18+
echo
19+
echo "${OUTPUT}"
20+
EXIT_CODE=1
21+
fi
22+
23+
exit ${EXIT_CODE}

0 commit comments

Comments
 (0)