Skip to content

Commit 465f74b

Browse files
committed
Fixed cppcheck-opensource#6398 - false negative: (warning) Unnecessary comparison of static strings.
1 parent 1aa3adb commit 465f74b

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

lib/checkstring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void CheckString::checkAlwaysTrueOrFalseStringCompare()
3939
return;
4040

4141
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
42-
if (Token::Match(tok, "strncmp|strcmp|stricmp|strcmpi|strcasecmp|strncasecmp|wcscmp|wcsncmp (")) {
42+
if (Token::Match(tok, "memcmp|strncmp|strcmp|stricmp|strverscmp|bcmp|strcmpi|strcasecmp|strncasecmp|wcsncasecmp|wcscasecmp|wmemcmp|wcscmp|wcsncmp (")) {
4343
if (Token::Match(tok->tokAt(2), "%str% , %str%")) {
4444
const std::string &str1 = tok->strAt(2);
4545
const std::string &str2 = tok->strAt(4);

test/teststring.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,31 @@ class TestString : public TestFixture {
9999

100100
void alwaysTrueFalseStringCompare() {
101101
check("void f() {\n"
102-
" if (strcmp(\"A\",\"A\") == 0){}\n"
103-
" if (strncmp(\"A\",\"A\",1) == 0){}\n"
104-
" if (strcasecmp(\"A\",\"A\") == 0){}\n"
105-
" if (strncasecmp(\"A\",\"A\",1) == 0){}\n"
102+
" if (strcmp(\"A\",\"A\")){}\n"
103+
" if (strncmp(\"A\",\"A\",1)){}\n"
104+
" if (strcasecmp(\"A\",\"A\")){}\n"
105+
" if (strncasecmp(\"A\",\"A\",1)){}\n"
106+
" if (memcmp(\"A\",\"A\",1)){}\n"
107+
" if (strverscmp(\"A\",\"A\")){}\n"
108+
" if (bcmp(\"A\",\"A\",1)){}\n"
109+
" if (wcsncasecmp(L\"A\",L\"A\",1)){}\n"
110+
" if (wcsncmp(L\"A\",L\"A\",1)){}\n"
111+
" if (wmemcmp(L\"A\",L\"A\",1)){}\n"
112+
" if (wcscmp(L\"A\",L\"A\")){}\n"
113+
" if (wcscasecmp(L\"A\",L\"A\")){}\n"
106114
"}");
107115
ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n"
108116
"[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n"
109117
"[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n"
110-
"[test.cpp:5]: (warning) Unnecessary comparison of static strings.\n", errout.str());
118+
"[test.cpp:5]: (warning) Unnecessary comparison of static strings.\n"
119+
"[test.cpp:6]: (warning) Unnecessary comparison of static strings.\n"
120+
"[test.cpp:7]: (warning) Unnecessary comparison of static strings.\n"
121+
"[test.cpp:8]: (warning) Unnecessary comparison of static strings.\n"
122+
"[test.cpp:9]: (warning) Unnecessary comparison of static strings.\n"
123+
"[test.cpp:10]: (warning) Unnecessary comparison of static strings.\n"
124+
"[test.cpp:11]: (warning) Unnecessary comparison of static strings.\n"
125+
"[test.cpp:12]: (warning) Unnecessary comparison of static strings.\n"
126+
"[test.cpp:13]: (warning) Unnecessary comparison of static strings.\n", errout.str());
111127

112128
check_preprocess_suppress(
113129
"#define MACRO \"00FF00\"\n"

0 commit comments

Comments
 (0)