diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 16387dcaa..7d8d572ca 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -19,6 +19,7 @@ //--------------------------------------------------------------------------- #include "checkother.h" +#include "checknullpointer.h" // CheckNullPointer::isPointerDeRef() #include "mathlib.h" #include "symboldatabase.h" @@ -3107,8 +3108,12 @@ void CheckOther::checkSuspiciousStringCompare() std::swap(varTok, litTok); const Variable* var = symbolDatabase->getVariableFromVarId(varTok->varId()); - if (var && var->isPointer()) - suspiciousStringCompareError(tok, var->name()); + if (var) { + bool unknown=false; + if (_tokenizer->isC() || + (var->isPointer() && !CheckNullPointer::isPointerDeRef(tok, unknown, symbolDatabase))) + suspiciousStringCompareError(tok, var->name()); + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index 452aa3acf..009fda1db 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5170,6 +5170,34 @@ private: " return \"x\" == c;\n" "}"); ASSERT_EQUALS("", errout.str()); + + // Ticket #4257 + check("bool foo() {\n" + "MyString *str=Getter();\n" + "return *str==\"bug\"; }\n", "test.c"); + ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable 'str'. Did you intend to use strcmp() instead?\n", errout.str()); + + // Ticket #4257 + check("bool foo() {\n" + "MyString *str=Getter();\n" + "return *str==\"bug\"; }\n"); + ASSERT_EQUALS("", errout.str()); + + // Ticket #4257 + check("bool foo() {\n" + "MyString **str=OtherGetter();\n" + "return *str==\"bug\"; }\n"); + TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", + "", + errout.str()); + + // Ticket #4257 + check("bool foo() {\n" + "MyString str=OtherGetter2();\n" + "return &str==\"bug\"; }\n"); + TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", + "", + errout.str()); } void checkPointerSizeof() {