From 7e8019e47466c2b13d52b7087614478bd4b4aa03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 20 Dec 2012 20:48:48 +0100 Subject: [PATCH] Fixed #4403 (False positive 'Conversion of string literal to bool always evaluates to true.' with BOOST_ASSERT) --- lib/checkother.cpp | 16 ++++++++-------- test/testother.cpp | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 961f0f897..eaec8fde8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2466,6 +2466,12 @@ void CheckOther::checkIncorrectStringCompare() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { + // skip "assert(str && ..)" and "assert(.. && str)" + if (Token::Match(tok, "%var% (") && + (Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )")) && + (tok->str().find("assert")+6U==tok->str().size() || tok->str().find("ASSERT")+6U==tok->str().size())) + tok = tok->next()->link(); + if (Token::Match(tok, ". substr ( %any% , %num% ) ==|!= %str%")) { MathLib::bigint clen = MathLib::toLongNumber(tok->strAt(5)); std::size_t slen = Token::getStrLength(tok->tokAt(8)); @@ -2478,15 +2484,9 @@ void CheckOther::checkIncorrectStringCompare() if (clen != (int)slen) { incorrectStringCompareError(tok->next(), "substr", tok->str()); } - } else if (Token::Match(tok, "&&|%oror% %str% &&|%oror%|)")) { - // assert(condition && "debug message") would be considered a fp. - if (tok->str() == "&&" && tok->strAt(2) == ")" && tok->linkAt(2)->previous()->str() == "assert") - continue; + } else if (Token::Match(tok, "&&|%oror%|( %str% &&|%oror%|)") && !Token::Match(tok, "( %str% )")) { incorrectStringBooleanError(tok->next(), tok->strAt(1)); - } else if (Token::Match(tok, "if|while|assert ( %str% &&|%oror%|)")) { - // assert("debug message" && condition) would be considered a fp. - if (tok->strAt(3) == "&&" && tok->str() == "assert") - continue; + } else if (Token::Match(tok, "if|while ( %str% )")) { incorrectStringBooleanError(tok->tokAt(2), tok->strAt(2)); } } diff --git a/test/testother.cpp b/test/testother.cpp index 3368848a2..ac6264ef5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4610,6 +4610,11 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("int f() {\n" + " BOOST_ASSERT (\"Hello\" && test);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("int f() {\n" " return f2(\"Hello\");\n" "}");