Skip to content

Commit 1e7db37

Browse files
committed
Merge bitcoin/bitcoin#24856: lint: Converting lint-assertions.sh to lint-assertions.py
172c233 Porting lint-assertions.sh to lint-assertions.py (hiago) Pull request description: This PR is converting `test/lint/lint-assertions.sh` to `test/lint/lint-assertions.py`. It's an item of #24783. ACKs for top commit: laanwj: Tested ACK 172c233 Tree-SHA512: 94d5b03acfeaf2303fad95d489d6c3aa7bd655889ddaa807cc97e0613b8eb8f5ef094feee2a98d974606890deb554e76490a5c523d64eb5bc55afa6a43221aae
2 parents 16fa967 + 172c233 commit 1e7db37

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

test/lint/lint-assertions.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2018-2022 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+
import sys
10+
import subprocess
11+
12+
13+
def git_grep(params: [], error_msg: ""):
14+
try:
15+
output = subprocess.check_output(["git", "grep", *params], universal_newlines=True, encoding="utf8")
16+
print(error_msg)
17+
print(output)
18+
return 1
19+
except subprocess.CalledProcessError as ex1:
20+
if ex1.returncode > 1:
21+
raise ex1
22+
return 0
23+
24+
25+
def main():
26+
# PRE31-C (SEI CERT C Coding Standard):
27+
# "Assertions should not contain assignments, increment, or decrement operators."
28+
exit_code = git_grep([
29+
"-E",
30+
r"[^_]assert\(.*(\+\+|\-\-|[^=!<>]=[^=!<>]).*\);",
31+
"--",
32+
"*.cpp",
33+
"*.h"
34+
], "Assertions should not have side effects:")
35+
36+
# Macro CHECK_NONFATAL(condition) should be used instead of assert for RPC code, where it
37+
# is undesirable to crash the whole program. See: src/util/check.h
38+
# src/rpc/server.cpp is excluded from this check since it's mostly meta-code.
39+
exit_code |= git_grep([
40+
"-nE",
41+
r"\<(A|a)ssert *\(.*\);",
42+
"--",
43+
"src/rpc/",
44+
"src/wallet/rpc*",
45+
":(exclude)src/rpc/server.cpp"
46+
], "CHECK_NONFATAL(condition) should be used instead of assert for RPC code.")
47+
48+
sys.exit(exit_code)
49+
50+
51+
if __name__ == "__main__":
52+
main()

test/lint/lint-assertions.sh

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)