| 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): |