Fix FP issue 8801: Condition 'a+b' is always true (#1444)
This commit is contained in:
parent
2989c44f59
commit
edde0eedaa
|
@ -1235,6 +1235,25 @@ void CheckCondition::clarifyConditionError(const Token *tok, bool assign, bool b
|
||||||
errmsg, CWE398, false);
|
errmsg, CWE398, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool isConstVarExpression(const Token * tok)
|
||||||
|
{
|
||||||
|
if(!tok)
|
||||||
|
return false;
|
||||||
|
if(Token::Match(tok, "%cop%")) {
|
||||||
|
if(tok->astOperand1() && !isConstVarExpression(tok->astOperand1()))
|
||||||
|
return false;
|
||||||
|
if(tok->astOperand2() && !isConstVarExpression(tok->astOperand2()))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(Token::Match(tok, "%bool%|%num%|%str%|%char%|nullptr|NULL"))
|
||||||
|
return true;
|
||||||
|
if(tok->isEnumerator())
|
||||||
|
return true;
|
||||||
|
if(tok->variable())
|
||||||
|
return tok->variable()->isConst();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void CheckCondition::alwaysTrueFalse()
|
void CheckCondition::alwaysTrueFalse()
|
||||||
{
|
{
|
||||||
|
@ -1269,7 +1288,7 @@ void CheckCondition::alwaysTrueFalse()
|
||||||
if (!(constIfWhileExpression || constValExpr || compExpr || returnStatement))
|
if (!(constIfWhileExpression || constValExpr || compExpr || returnStatement))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (returnStatement && (tok->isEnumerator() || Token::Match(tok, "nullptr|NULL")))
|
if (returnStatement && isConstVarExpression(tok))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (returnStatement && Token::simpleMatch(tok->astParent(), "return") && tok->variable() && (
|
if (returnStatement && Token::simpleMatch(tok->astParent(), "return") && tok->variable() && (
|
||||||
|
|
|
@ -2603,6 +2603,13 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("int f() {\n"
|
||||||
|
" const int a = 50;\n"
|
||||||
|
" const int b = 52;\n"
|
||||||
|
" return a+b;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("bool& g();\n"
|
check("bool& g();\n"
|
||||||
"bool f() {\n"
|
"bool f() {\n"
|
||||||
" bool & b = g();\n"
|
" bool & b = g();\n"
|
||||||
|
|
Loading…
Reference in New Issue