Fixed #4257 (False Positive: String literal compared with variable - for non-pointer variable)
This commit is contained in:
parent
0115bb8d24
commit
3656366c7e
|
@ -19,6 +19,7 @@
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
#include "checkother.h"
|
||||
#include "checknullpointer.h" // CheckNullPointer::isPointerDeRef()
|
||||
#include "mathlib.h"
|
||||
#include "symboldatabase.h"
|
||||
|
||||
|
@ -3107,11 +3108,15 @@ void CheckOther::checkSuspiciousStringCompare()
|
|||
std::swap(varTok, litTok);
|
||||
|
||||
const Variable* var = symbolDatabase->getVariableFromVarId(varTok->varId());
|
||||
if (var && var->isPointer())
|
||||
if (var) {
|
||||
bool unknown=false;
|
||||
if (_tokenizer->isC() ||
|
||||
(var->isPointer() && !CheckNullPointer::isPointerDeRef(tok, unknown, symbolDatabase)))
|
||||
suspiciousStringCompareError(tok, var->name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CheckOther::suspiciousStringCompareError(const Token* tok, const std::string& var)
|
||||
{
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue