Fixed #4403 (False positive 'Conversion of string literal <string> to bool always evaluates to true.' with BOOST_ASSERT)

This commit is contained in:
Daniel Marjamäki 2012-12-20 20:48:48 +01:00
parent ccdea4dc2b
commit 7e8019e474
2 changed files with 13 additions and 8 deletions

View File

@ -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));
}
}

View File

@ -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"
"}");