Skip to content

Commit 9c8378a

Browse files
authored
cleaned up rules folder and test remaining ones (danmar#6951)
1 parent 0a8ec90 commit 9c8378a

File tree

3 files changed

+186
-53
lines changed

3 files changed

+186
-53
lines changed

rules/error-reporting.xml

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

rules/token-matching.xml

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

test/cli/rules_test.py

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import os
2+
import sys
3+
4+
from testutils import cppcheck
5+
6+
__script_dir = os.path.dirname(os.path.abspath(__file__))
7+
__root_dir = os.path.abspath(os.path.join(__script_dir, '..', '..'))
8+
__rules_dir = os.path.join(__root_dir, 'rules')
9+
10+
11+
def test_empty_catch_block(tmp_path):
12+
test_file = tmp_path / 'test.cpp'
13+
with open(test_file, 'wt') as f:
14+
f.write("""
15+
void f()
16+
{
17+
try
18+
{
19+
}
20+
catch (...)
21+
{
22+
}
23+
}
24+
""")
25+
26+
rule_file = os.path.join(__rules_dir, 'empty-catch-block.xml')
27+
args = [
28+
'--template=simple',
29+
'--rule-file={}'.format(rule_file),
30+
str(test_file)
31+
]
32+
ret, stdout, stderr = cppcheck(args)
33+
assert ret == 0
34+
assert stdout.splitlines() == [
35+
'Checking {} ...'.format(test_file),
36+
'Processing rule: \\}\\s*catch\\s*\\(.*\\)\\s*\\{\\s*\\}'
37+
]
38+
assert stderr.splitlines() == [
39+
'{}:6:0: style: Empty catch block found. [rule]'.format(test_file)
40+
]
41+
42+
43+
def test_show_all_defines(tmp_path):
44+
test_file = tmp_path / 'test.cpp'
45+
with open(test_file, 'wt') as f:
46+
f.write("""
47+
#define DEF_1
48+
49+
void f()
50+
{
51+
}
52+
""")
53+
54+
rule_file = os.path.join(__rules_dir, 'show-all-defines.rule')
55+
args = [
56+
'--template=simple',
57+
'-DDEF_2',
58+
'--rule-file={}'.format(rule_file),
59+
str(test_file)
60+
]
61+
ret, stdout, stderr = cppcheck(args)
62+
assert ret == 0
63+
assert stdout.splitlines() == [
64+
'Checking {} ...'.format(test_file),
65+
'Processing rule: .*',
66+
'Checking {}: DEF_2=1...'.format(test_file)
67+
]
68+
if sys.platform == 'win32':
69+
test_file = str(test_file).replace('\\', '/')
70+
assert stderr.splitlines() == [
71+
# TODO: this message looks strange
72+
":1:0: information: found ' # line 2 \"{}\" # define DEF_1' [showalldefines]".format(test_file)
73+
]
74+
75+
76+
def test_stl(tmp_path):
77+
test_file = tmp_path / 'test.cpp'
78+
with open(test_file, 'wt') as f:
79+
f.write("""
80+
void f()
81+
{
82+
std::string s;
83+
if (s.find("t") == 17)
84+
{
85+
}
86+
}
87+
""")
88+
89+
rule_file = os.path.join(__rules_dir, 'stl.xml')
90+
args = [
91+
'--template=simple',
92+
'--rule-file={}'.format(rule_file),
93+
str(test_file)
94+
]
95+
ret, stdout, stderr = cppcheck(args)
96+
assert ret == 0
97+
assert stdout.splitlines() == [
98+
'Checking {} ...'.format(test_file),
99+
'Processing rule: \\. find \\( "[^"]+?" \\) == \\d+ '
100+
]
101+
assert stderr.splitlines() == [
102+
'{}:5:0: performance: When looking for a string at a fixed position compare [UselessSTDStringFind]'.format(test_file)
103+
]
104+
105+
106+
def test_strlen_empty_str(tmp_path):
107+
test_file = tmp_path / 'test.cpp'
108+
with open(test_file, 'wt') as f:
109+
f.write("""
110+
void f(const char* s)
111+
{
112+
if (strlen(s) > 0)
113+
{
114+
}
115+
}
116+
""")
117+
118+
rule_file = os.path.join(__rules_dir, 'strlen-empty-str.xml')
119+
args = [
120+
'--template=simple',
121+
'--rule-file={}'.format(rule_file),
122+
str(test_file)
123+
]
124+
ret, stdout, stderr = cppcheck(args)
125+
assert ret == 0
126+
assert stdout.splitlines() == [
127+
'Checking {} ...'.format(test_file),
128+
'Processing rule: if \\( ([!] )*?(strlen) \\( \\w+? \\) ([>] [0] )*?\\) { '
129+
]
130+
assert stderr.splitlines() == [
131+
'{}:4:0: performance: Using strlen() to check if a string is empty is not efficient. [StrlenEmptyString]'.format(test_file)
132+
]
133+
134+
135+
def test_suggest_nullptr(tmp_path):
136+
test_file = tmp_path / 'test.cpp'
137+
with open(test_file, 'wt') as f:
138+
f.write("""
139+
void f()
140+
{
141+
const char* p = 0;
142+
}
143+
""")
144+
145+
rule_file = os.path.join(__rules_dir, 'suggest_nullptr.xml')
146+
args = [
147+
'--template=simple',
148+
'--rule-file={}'.format(rule_file),
149+
str(test_file)
150+
]
151+
ret, stdout, stderr = cppcheck(args)
152+
assert ret == 0
153+
assert stdout.splitlines() == [
154+
'Checking {} ...'.format(test_file),
155+
'Processing rule: (\\b\\w+\\b) \\* (\\b\\w+\\b) = 0 ;'
156+
]
157+
assert stderr.splitlines() == [
158+
"{}:4:0: style: Prefer to use a 'nullptr' instead of initializing a pointer with 0. [modernizeUseNullPtr]".format(test_file)
159+
]
160+
161+
162+
def test_unused_deref(tmp_path):
163+
test_file = tmp_path / 'test.cpp'
164+
with open(test_file, 'wt') as f:
165+
f.write("""
166+
void f(const char* p)
167+
{
168+
*p++;
169+
}
170+
""")
171+
172+
rule_file = os.path.join(__rules_dir, 'unused-deref.xml')
173+
args = [
174+
'--template=simple',
175+
'--rule-file={}'.format(rule_file),
176+
str(test_file)
177+
]
178+
ret, stdout, stderr = cppcheck(args)
179+
assert ret == 0
180+
assert stdout.splitlines() == [
181+
'Checking {} ...'.format(test_file),
182+
'Processing rule: [;{}] [*] \\w+? (\\+\\+|\\-\\-) ; '
183+
]
184+
assert stderr.splitlines() == [
185+
'{}:3:0: style: Redundant * found, "*p++" is the same as "*(p++)". [UnusedDeref]'.format(test_file)
186+
]

0 commit comments

Comments
 (0)