diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index a7801507c..70e31ccdf 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -168,7 +168,7 @@ void CheckString::checkSuspiciousStringCompare() continue; if (varTok->tokType() == Token::eString || varTok->tokType() == Token::eNumber) std::swap(varTok, litTok); - else if (litTok->tokType() != Token::eString && litTok->tokType() != Token::eNumber) + else if (!Token::Match(litTok, "%char%|%num%|%str%")) continue; // Pointer addition? @@ -200,10 +200,13 @@ void CheckString::checkSuspiciousStringCompare() varTok = varTok->astParent(); const std::string varname = varTok->expressionString(); + const bool ischar(litTok->tokType() == Token::eChar || + (!litTok->originalName().empty() && + litTok->originalName().front() == '\'')); if (litTok->tokType() == Token::eString) { if (_tokenizer->isC() || (var && var->isArrayOrPointer())) suspiciousStringCompareError(tok, varname); - } else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) { + } else if (ischar && var && var->isPointer()) { suspiciousStringCompareError_char(tok, varname); } } diff --git a/test/teststring.cpp b/test/teststring.cpp index bac904459..98879546c 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -367,7 +367,7 @@ private: void suspiciousStringCompare_char() { check("bool foo(char* c) {\n" - " return c == '\\0';\n" + " return c == 'x';\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (warning) Char literal compared with pointer 'c'. Did you intend to dereference it?\n", errout.str());