From 465f74b3bb540b80fad6e9a2fd77632522389c9e Mon Sep 17 00:00:00 2001 From: Martin Ettl Date: Fri, 2 Jan 2015 12:58:04 +0100 Subject: [PATCH] Fixed #6398 - false negative: (warning) Unnecessary comparison of static strings. --- lib/checkstring.cpp | 2 +- test/teststring.cpp | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index f24221b22..4708c5e5b 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -39,7 +39,7 @@ void CheckString::checkAlwaysTrueOrFalseStringCompare() return; for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (Token::Match(tok, "strncmp|strcmp|stricmp|strcmpi|strcasecmp|strncasecmp|wcscmp|wcsncmp (")) { + if (Token::Match(tok, "memcmp|strncmp|strcmp|stricmp|strverscmp|bcmp|strcmpi|strcasecmp|strncasecmp|wcsncasecmp|wcscasecmp|wmemcmp|wcscmp|wcsncmp (")) { if (Token::Match(tok->tokAt(2), "%str% , %str%")) { const std::string &str1 = tok->strAt(2); const std::string &str2 = tok->strAt(4); diff --git a/test/teststring.cpp b/test/teststring.cpp index fcbd5fb60..c6080c97a 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -99,15 +99,31 @@ private: void alwaysTrueFalseStringCompare() { check("void f() {\n" - " if (strcmp(\"A\",\"A\") == 0){}\n" - " if (strncmp(\"A\",\"A\",1) == 0){}\n" - " if (strcasecmp(\"A\",\"A\") == 0){}\n" - " if (strncasecmp(\"A\",\"A\",1) == 0){}\n" + " if (strcmp(\"A\",\"A\")){}\n" + " if (strncmp(\"A\",\"A\",1)){}\n" + " if (strcasecmp(\"A\",\"A\")){}\n" + " if (strncasecmp(\"A\",\"A\",1)){}\n" + " if (memcmp(\"A\",\"A\",1)){}\n" + " if (strverscmp(\"A\",\"A\")){}\n" + " if (bcmp(\"A\",\"A\",1)){}\n" + " if (wcsncasecmp(L\"A\",L\"A\",1)){}\n" + " if (wcsncmp(L\"A\",L\"A\",1)){}\n" + " if (wmemcmp(L\"A\",L\"A\",1)){}\n" + " if (wcscmp(L\"A\",L\"A\")){}\n" + " if (wcscasecmp(L\"A\",L\"A\")){}\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n" "[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n" "[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n" - "[test.cpp:5]: (warning) Unnecessary comparison of static strings.\n", errout.str()); + "[test.cpp:5]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:6]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:7]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:8]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:9]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:10]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:11]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:12]: (warning) Unnecessary comparison of static strings.\n" + "[test.cpp:13]: (warning) Unnecessary comparison of static strings.\n", errout.str()); check_preprocess_suppress( "#define MACRO \"00FF00\"\n"