Fix wrong message

This commit is contained in:
Daniel Marjamäki 2018-08-12 08:01:15 +02:00
parent a9ae897f8c
commit be4ae66e36
2 changed files with 8 additions and 1 deletions

View File

@ -322,10 +322,12 @@ void CheckString::incorrectStringCompareError(const Token *tok, const std::strin
void CheckString::incorrectStringBooleanError(const Token *tok, const std::string& string)
{
const bool charLiteral = string[0] == '\'';
const std::string literalType = charLiteral ? "char" : "string";
const std::string result = (string == "\'\\0\'") ? "false" : "true";
reportError(tok,
Severity::warning,
charLiteral ? "incorrectCharBooleanError" : "incorrectStringBooleanError",
"Conversion of " + std::string(charLiteral ? "char" : "string") + " literal " + string + " to bool always evaluates to true.", CWE571, false);
"Conversion of " + literalType + " literal " + string + " to bool always evaluates to " + result + '.', CWE571, false);
}
//---------------------------------------------------------------------------

View File

@ -596,6 +596,11 @@ private:
" if('\\0'){}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if('\\0' || cond){}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of char literal '\\0' to bool always evaluates to false.\n", errout.str());
}
void deadStrcmp() {