Skip to content

Commit be4ae66

Browse files
committed
Fix wrong message
1 parent a9ae897 commit be4ae66

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/checkstring.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,12 @@ void CheckString::incorrectStringCompareError(const Token *tok, const std::strin
322322
void CheckString::incorrectStringBooleanError(const Token *tok, const std::string& string)
323323
{
324324
const bool charLiteral = string[0] == '\'';
325+
const std::string literalType = charLiteral ? "char" : "string";
326+
const std::string result = (string == "\'\\0\'") ? "false" : "true";
325327
reportError(tok,
326328
Severity::warning,
327329
charLiteral ? "incorrectCharBooleanError" : "incorrectStringBooleanError",
328-
"Conversion of " + std::string(charLiteral ? "char" : "string") + " literal " + string + " to bool always evaluates to true.", CWE571, false);
330+
"Conversion of " + literalType + " literal " + string + " to bool always evaluates to " + result + '.', CWE571, false);
329331
}
330332

331333
//---------------------------------------------------------------------------

test/teststring.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,11 @@ class TestString : public TestFixture {
596596
" if('\\0'){}\n"
597597
"}");
598598
ASSERT_EQUALS("", errout.str());
599+
600+
check("void f() {\n"
601+
" if('\\0' || cond){}\n"
602+
"}");
603+
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of char literal '\\0' to bool always evaluates to false.\n", errout.str());
599604
}
600605

601606
void deadStrcmp() {

0 commit comments

Comments
 (0)