Fixed #4403 (False positive 'Conversion of string literal <string> to bool always evaluates to true.' with BOOST_ASSERT)
This commit is contained in:
parent
ccdea4dc2b
commit
7e8019e474
|
@ -2466,6 +2466,12 @@ void CheckOther::checkIncorrectStringCompare()
|
||||||
for (std::size_t i = 0; i < functions; ++i) {
|
for (std::size_t i = 0; i < functions; ++i) {
|
||||||
const Scope * scope = symbolDatabase->functionScopes[i];
|
const Scope * scope = symbolDatabase->functionScopes[i];
|
||||||
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
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%")) {
|
if (Token::Match(tok, ". substr ( %any% , %num% ) ==|!= %str%")) {
|
||||||
MathLib::bigint clen = MathLib::toLongNumber(tok->strAt(5));
|
MathLib::bigint clen = MathLib::toLongNumber(tok->strAt(5));
|
||||||
std::size_t slen = Token::getStrLength(tok->tokAt(8));
|
std::size_t slen = Token::getStrLength(tok->tokAt(8));
|
||||||
|
@ -2478,15 +2484,9 @@ void CheckOther::checkIncorrectStringCompare()
|
||||||
if (clen != (int)slen) {
|
if (clen != (int)slen) {
|
||||||
incorrectStringCompareError(tok->next(), "substr", tok->str());
|
incorrectStringCompareError(tok->next(), "substr", tok->str());
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok, "&&|%oror% %str% &&|%oror%|)")) {
|
} else if (Token::Match(tok, "&&|%oror%|( %str% &&|%oror%|)") && !Token::Match(tok, "( %str% )")) {
|
||||||
// assert(condition && "debug message") would be considered a fp.
|
|
||||||
if (tok->str() == "&&" && tok->strAt(2) == ")" && tok->linkAt(2)->previous()->str() == "assert")
|
|
||||||
continue;
|
|
||||||
incorrectStringBooleanError(tok->next(), tok->strAt(1));
|
incorrectStringBooleanError(tok->next(), tok->strAt(1));
|
||||||
} else if (Token::Match(tok, "if|while|assert ( %str% &&|%oror%|)")) {
|
} else if (Token::Match(tok, "if|while ( %str% )")) {
|
||||||
// assert("debug message" && condition) would be considered a fp.
|
|
||||||
if (tok->strAt(3) == "&&" && tok->str() == "assert")
|
|
||||||
continue;
|
|
||||||
incorrectStringBooleanError(tok->tokAt(2), tok->strAt(2));
|
incorrectStringBooleanError(tok->tokAt(2), tok->strAt(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4610,6 +4610,11 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("int f() {\n"
|
||||||
|
" BOOST_ASSERT (\"Hello\" && test);\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("int f() {\n"
|
check("int f() {\n"
|
||||||
" return f2(\"Hello\");\n"
|
" return f2(\"Hello\");\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
Loading…
Reference in New Issue