Fix #11679 FP knownArgument with known return value (#5007)

* Fix #11679 FP knownArgument with known return value

* Add test for #11051
This commit is contained in:
chrchr-github 2023-04-28 08:27:48 +02:00 committed by GitHub
parent 5b4c95f229
commit 2b92351b49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 0 deletions

View File

@ -1498,6 +1498,8 @@ void CheckCondition::alwaysTrueFalse()
continue;
if (Token::simpleMatch(tok, ":"))
continue;
if (Token::Match(tok->astOperand1(), "%name% (") && Token::simpleMatch(tok->astParent(), "return"))
continue;
if (tok->isComparisonOp() && isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand1()) &&
isSameExpression(mTokenizer->isCPP(),
true,

View File

@ -3597,6 +3597,8 @@ void CheckOther::checkKnownArgument()
continue;
if (isConstVarExpression(tok))
continue;
if (Token::Match(tok->astOperand1(), "%name% ("))
continue;
const Token * tok2 = tok;
if (isCPPCast(tok2))
tok2 = tok2->astOperand2();

View File

@ -4504,6 +4504,20 @@ private:
" if (j == 1) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void h(int);\n" // #11679
"bool g(int a) { h(a); return false; }\n"
"bool f(int i) {\n"
" return g(i);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(std::string a) {\n" // #11051
" a = \"x\";\n"
" if (a == \"x\") {}\n"
" return a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'a==\"x\"' is always true\n", errout.str());
}
void alwaysTrueSymbolic()

View File

@ -10696,6 +10696,15 @@ private:
" dostuff(self->maxsize * sizeof(intptr_t));\n"
"}");
ASSERT_EQUALS("", errout.str());
// #11679
check("bool g(int);\n"
"void h(int);\n"
"int k(int a) { h(a); return 0; }\n"
"void f(int i) {\n"
" if (g(k(i))) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void knownArgumentHiddenVariableExpression() {