Skip to content

Commit a6e1886

Browse files
committed
Merge branch 'chr_Fix8266' of https://github.com/chrchr-github/cppcheck into chr_Fix8266
2 parents fab2b08 + 3e28961 commit a6e1886

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

lib/astutils.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,17 +2174,28 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
21742174
return !fun->isConst();
21752175
}
21762176

2177-
const Token *ftok = tok2;
2178-
while (ftok && (!Token::Match(ftok, "[({]") || ftok->isCast()))
2177+
const Token* ftok = tok2, *ptok{};
2178+
while (ftok && (!Token::Match(ftok, "[({]") || ftok->isCast())) {
21792179
ftok = ftok->astParent();
2180+
if (ftok && ftok->tokType() == Token::eLogicalOp)
2181+
ptok = ftok->astOperand1(); // check lhs
2182+
}
2183+
2184+
if ((ftok && Token::Match(ftok->link(), ")|} !!{")) || (ptok && Token::Match(ptok->link(), ")|} !!{"))) {
2185+
bool isChanged = false, inconclusive = false;
2186+
if (ptok) {
2187+
isChanged |= isVariableChangedByFunctionCall(ptok->next(), indirect, settings, &inconclusive); // lhs can be a function
2188+
isChanged |= inconclusive;
2189+
}
21802190

2181-
if (ftok && Token::Match(ftok->link(), ")|} !!{")) {
2182-
const Token * ptok = tok2;
2183-
while (Token::Match(ptok->astParent(), ".|::|["))
2184-
ptok = ptok->astParent();
2185-
bool inconclusive = false;
2186-
bool isChanged = isVariableChangedByFunctionCall(ptok, indirect, settings, &inconclusive);
2187-
isChanged |= inconclusive;
2191+
if (!isChanged) {
2192+
ptok = tok2;
2193+
while (Token::Match(ptok->astParent(), ".|::|["))
2194+
ptok = ptok->astParent();
2195+
2196+
isChanged = isVariableChangedByFunctionCall(ptok, indirect, settings, &inconclusive);
2197+
isChanged |= inconclusive;
2198+
}
21882199
if (isChanged)
21892200
return true;
21902201
}

lib/checkcondition.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ void CheckCondition::multiCondition2()
693693
return ChildrenToVisit::op1_and_op2;
694694

695695
if ((!cond1->hasKnownIntValue() || !secondCondition->hasKnownIntValue()) &&
696-
isSameExpression(mTokenizer->isCPP(), true, cond1, secondCondition, mSettings->library, true, true, &errorPath)) {
696+
isSameExpression(mTokenizer->isCPP(), true, cond1, secondCondition, mSettings->library, true, true, &errorPath) &&
697+
!isExpressionChangedAt(cond1, secondCondition, 0, false, mSettings, mTokenizer->isCPP())) {
697698
if (!isAliased(vars) && !mTokenizer->hasIfdef(cond1, secondCondition)) {
698699
identicalConditionAfterEarlyExitError(cond1, secondCondition, errorPath);
699700
return ChildrenToVisit::done;

test/testother.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9357,7 +9357,9 @@ class TestOther : public TestFixture {
93579357
" dostuff(x * 0);\n"
93589358
" dostuff(0 * x);\n"
93599359
"}\n");
9360-
ASSERT_EQUALS("[test.cpp:3]: (style) Argument 'false&&x' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'.\n"
9360+
ASSERT_EQUALS("[test.cpp:2]: (style) Argument 'x&&false' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'.\n"
9361+
"[test.cpp:3]: (style) Argument 'false&&x' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'.\n"
9362+
"[test.cpp:4]: (style) Argument 'x||true' to function dostuff is always 1. Constant literal calculation disable/hide variable expression 'x'.\n"
93619363
"[test.cpp:5]: (style) Argument 'true||x' to function dostuff is always 1. Constant literal calculation disable/hide variable expression 'x'.\n"
93629364
"[test.cpp:6]: (style) Argument 'x*0' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'.\n"
93639365
"[test.cpp:7]: (style) Argument '0*x' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'.\n", errout.str());

0 commit comments

Comments
 (0)