From f2e7071922eaf7957da5d54c463702aa4ddfbf56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Aug 2018 19:04:10 +0200 Subject: [PATCH] Fix FP, conversion of char literal '\0' to boolean is not true --- lib/checkstring.cpp | 2 +- test/teststring.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index a4211d8b8..b2b1cb003 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -307,7 +307,7 @@ void CheckString::checkIncorrectStringCompare() } } else if (Token::Match(tok, "&&|%oror%|( %str%|%char% &&|%oror%|)") && !Token::Match(tok, "( %str%|%char% )")) { incorrectStringBooleanError(tok->next(), tok->strAt(1)); - } else if (Token::Match(tok, "if|while ( %str%|%char% )")) { + } else if (Token::Match(tok, "if|while ( %str%|%char% )") && !tok->tokAt(2)->getValue(0)) { incorrectStringBooleanError(tok->tokAt(2), tok->strAt(2)); } } diff --git a/test/teststring.cpp b/test/teststring.cpp index e2fcd7b7f..39b1a5cae 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -591,6 +591,11 @@ private: "[test.cpp:3]: (warning) Conversion of char literal 'b' to bool always evaluates to true.\n" "[test.cpp:4]: (warning) Conversion of char literal 'c' to bool always evaluates to true.\n" , errout.str()); + + check("void f() {\n" + " if('\\0'){}\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void deadStrcmp() {