Skip to content

Commit 06752d7

Browse files
pfultz2danmar
authored andcommitted
Fix issue 9485: knownConditionTrueFalse false positive with integer constants (#2447)
* Fix issue 9485: knownConditionTrueFalse false positive with integer constants * Formatting
1 parent 538679a commit 06752d7

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

lib/checkother.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,15 @@ void CheckOther::checkDuplicateExpression()
19871987
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>|+=|*=|<<=|>>=")) {
19881988
if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1(), true))
19891989
continue;
1990-
if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, true, true, &errorPath)) {
1990+
const bool followVar = !isConstVarExpression(tok) || Token::Match(tok, "%comp%|%oror%|&&");
1991+
if (isSameExpression(mTokenizer->isCPP(),
1992+
true,
1993+
tok->astOperand1(),
1994+
tok->astOperand2(),
1995+
mSettings->library,
1996+
true,
1997+
followVar,
1998+
&errorPath)) {
19911999
if (isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand1())) {
19922000
const bool assignment = tok->str() == "=";
19932001
if (assignment && warningEnabled)
@@ -2005,17 +2013,39 @@ void CheckOther::checkDuplicateExpression()
20052013
duplicateExpressionError(tok->astOperand1(), tok->astOperand2(), tok, errorPath);
20062014
}
20072015
}
2008-
} else if (tok->str() == "=" && Token::simpleMatch(tok->astOperand2(), "=") && isSameExpression(mTokenizer->isCPP(), false, tok->astOperand1(), tok->astOperand2()->astOperand1(), mSettings->library, true, false)) {
2016+
} else if (tok->str() == "=" && Token::simpleMatch(tok->astOperand2(), "=") &&
2017+
isSameExpression(mTokenizer->isCPP(),
2018+
false,
2019+
tok->astOperand1(),
2020+
tok->astOperand2()->astOperand1(),
2021+
mSettings->library,
2022+
true,
2023+
false)) {
20092024
if (warningEnabled && isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand1())) {
20102025
selfAssignmentError(tok, tok->astOperand1()->expressionString());
20112026
}
20122027
} else if (styleEnabled &&
2013-
isOppositeExpression(mTokenizer->isCPP(), tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath) &&
2028+
isOppositeExpression(mTokenizer->isCPP(),
2029+
tok->astOperand1(),
2030+
tok->astOperand2(),
2031+
mSettings->library,
2032+
false,
2033+
true,
2034+
&errorPath) &&
20142035
!Token::Match(tok, "=|-|-=|/|/=") &&
20152036
isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand1())) {
20162037
oppositeExpressionError(tok, errorPath);
20172038
} else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative
2018-
if (styleEnabled && tok->astOperand2() && tok->str() == tok->astOperand1()->str() && isSameExpression(mTokenizer->isCPP(), true, tok->astOperand2(), tok->astOperand1()->astOperand2(), mSettings->library, true, true, &errorPath) && isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand2()))
2039+
if (styleEnabled && tok->astOperand2() && tok->str() == tok->astOperand1()->str() &&
2040+
isSameExpression(mTokenizer->isCPP(),
2041+
true,
2042+
tok->astOperand2(),
2043+
tok->astOperand1()->astOperand2(),
2044+
mSettings->library,
2045+
true,
2046+
followVar,
2047+
&errorPath) &&
2048+
isWithoutSideEffects(mTokenizer->isCPP(), tok->astOperand2()))
20192049
duplicateExpressionError(tok->astOperand2(), tok->astOperand1()->astOperand2(), tok, errorPath);
20202050
else if (tok->astOperand2() && isConstExpression(tok->astOperand1(), mSettings->library, true, mTokenizer->isCPP())) {
20212051
const Token *ast1 = tok->astOperand1();

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class TestOther : public TestFixture {
139139
TEST_CASE(duplicateExpression7);
140140
TEST_CASE(duplicateExpression8);
141141
TEST_CASE(duplicateExpression9); // #9320
142+
TEST_CASE(duplicateExpression10); // #9485
142143
TEST_CASE(duplicateExpressionLoop);
143144
TEST_CASE(duplicateValueTernary);
144145
TEST_CASE(duplicateExpressionTernary); // #6391
@@ -4657,6 +4658,18 @@ class TestOther : public TestFixture {
46574658
ASSERT_EQUALS("", errout.str());
46584659
}
46594660

4661+
void duplicateExpression10()
4662+
{
4663+
// #9485
4664+
check("int f() {\n"
4665+
" const int a = 1;\n"
4666+
" const int b = a-1;\n"
4667+
" const int c = a+1;\n"
4668+
" return c;\n"
4669+
"}\n");
4670+
ASSERT_EQUALS("", errout.str());
4671+
}
4672+
46604673
void duplicateExpressionLoop() {
46614674
check("void f() {\n"
46624675
" int a = 1;\n"

0 commit comments

Comments
 (0)