fix #7783: false positive knownConditionTrueFalse on assert(0 && "message").

This commit is contained in:
Matthias Krüger 2016-11-23 19:59:00 +01:00
parent e1a3c0955a
commit cf24ea5221
2 changed files with 24 additions and 2 deletions

View File

@ -991,8 +991,16 @@ void CheckCondition::alwaysTrueFalse()
// Don't warn in assertions. Condition is often 'always true' by intention.
// If platform,defines,etc cause 'always false' then that is not dangerous neither.
const std::string str = tok->astParent() && tok->astParent()->previous() ? tok->astParent()->previous()->str() : "";
if (str.find("assert")!=std::string::npos || str.find("ASSERT")!=std::string::npos)
bool assertFound = false;
for (const Token * tok2 = tok->astParent(); tok2 ; tok2 = tok2->astParent()) { // move backwards and try to find "assert"
if (tok2->str() == "(" && tok2->astOperand2()) {
const std::string& str = tok2->previous()->str();
if ((str.find("assert")!=std::string::npos || str.find("ASSERT")!=std::string::npos))
assertFound = true;
break;
}
}
if (assertFound)
continue;
// Don't warn when there are expanded macros..

View File

@ -1775,6 +1775,20 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// #7783 FP knownConditionTrueFalse on assert(0 && "message")
check("void foo(int x) {\n"
" if (x<0)\n"
" {\n"
" assert(0 && \"bla\");\n"
" ASSERT(0 && \"bla\");\n"
" assert_foo(0 && \"bla\");\n"
" ASSERT_FOO(0 && \"bla\");\n"
" assert((int)(0==0));\n"
" assert((int)(0==0) && \"bla\");\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #7750 warn about number and char literals in boolean expressions
check("void f() {\n"
" if('a'){}\n"