Skip to content

Commit 2b92351

Browse files
Fix #11679 FP knownArgument with known return value (#5007)
* Fix #11679 FP knownArgument with known return value * Add test for #11051
1 parent 5b4c95f commit 2b92351

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/checkcondition.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,8 @@ void CheckCondition::alwaysTrueFalse()
14981498
continue;
14991499
if (Token::simpleMatch(tok, ":"))
15001500
continue;
1501+
if (Token::Match(tok->astOperand1(), "%name% (") && Token::simpleMatch(tok->astParent(), "return"))
1502+
continue;
15011503
if (tok->isComparisonOp() && isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand1()) &&
15021504
isSameExpression(mTokenizer->isCPP(),
15031505
true,

lib/checkother.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,6 +3597,8 @@ void CheckOther::checkKnownArgument()
35973597
continue;
35983598
if (isConstVarExpression(tok))
35993599
continue;
3600+
if (Token::Match(tok->astOperand1(), "%name% ("))
3601+
continue;
36003602
const Token * tok2 = tok;
36013603
if (isCPPCast(tok2))
36023604
tok2 = tok2->astOperand2();

test/testcondition.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4504,6 +4504,20 @@ class TestCondition : public TestFixture {
45044504
" if (j == 1) {}\n"
45054505
"}\n");
45064506
ASSERT_EQUALS("", errout.str());
4507+
4508+
check("void h(int);\n" // #11679
4509+
"bool g(int a) { h(a); return false; }\n"
4510+
"bool f(int i) {\n"
4511+
" return g(i);\n"
4512+
"}\n");
4513+
ASSERT_EQUALS("", errout.str());
4514+
4515+
check("void f(std::string a) {\n" // #11051
4516+
" a = \"x\";\n"
4517+
" if (a == \"x\") {}\n"
4518+
" return a;\n"
4519+
"}\n");
4520+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'a==\"x\"' is always true\n", errout.str());
45074521
}
45084522

45094523
void alwaysTrueSymbolic()

test/testother.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10696,6 +10696,15 @@ class TestOther : public TestFixture {
1069610696
" dostuff(self->maxsize * sizeof(intptr_t));\n"
1069710697
"}");
1069810698
ASSERT_EQUALS("", errout.str());
10699+
10700+
// #11679
10701+
check("bool g(int);\n"
10702+
"void h(int);\n"
10703+
"int k(int a) { h(a); return 0; }\n"
10704+
"void f(int i) {\n"
10705+
" if (g(k(i))) {}\n"
10706+
"}\n");
10707+
ASSERT_EQUALS("", errout.str());
1069910708
}
1070010709

1070110710
void knownArgumentHiddenVariableExpression() {

0 commit comments

Comments
 (0)