Fix #11059 FP multiCondition when variable is assigned in if/else (#4102)

This commit is contained in:
chrchr-github 2022-05-11 20:01:13 +02:00 committed by GitHub
parent d0b7fe887a
commit 63a1698335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -2413,7 +2413,7 @@ static bool isExpressionChangedAt(const F& getExprTok,
if (depth < 0)
return true;
if (tok->exprId() != exprid) {
if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% ("))
if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure()))
// TODO: Is global variable really changed by function call?
return true;
const bool pointer = astIsPointer(tok);

View File

@ -513,9 +513,10 @@ void CheckCondition::multiCondition()
if (tok2->astOperand2()) {
ErrorPath errorPath;
if (isOverlappingCond(cond1, tok2->astOperand2(), true))
if (isOverlappingCond(cond1, tok2->astOperand2(), true) && !isExpressionChanged(cond1, cond1, tok2->astOperand2(), mSettings, mTokenizer->isCPP()))
overlappingElseIfConditionError(tok2->astOperand2(), cond1->linenr());
else if (isOppositeCond(true, mTokenizer->isCPP(), cond1, tok2->astOperand2(), mSettings->library, true, true, &errorPath))
else if (isOppositeCond(true, mTokenizer->isCPP(), cond1, tok2->astOperand2(), mSettings->library, true, true, &errorPath) &&
!isExpressionChanged(cond1, cond1, tok2->astOperand2(), mSettings, mTokenizer->isCPP()))
oppositeElseIfConditionError(cond1, tok2->astOperand2(), errorPath);
}
}

View File

@ -538,6 +538,25 @@ private:
" else if (!!a) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Expression is always false because 'else if' condition matches previous condition at line 2.\n", errout.str());
// #11059
check("int f();\n"
"void g() {\n"
" int i = f();\n"
" if (i == 3) {}\n"
" else if ((i = f()) == 5) {}\n"
" else if (i == 3) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("int f();\n"
"void g() {\n"
" int i = f();\n"
" if (i == 3) {}\n"
" else if ((i = f()) == 5) {}\n"
" else if (i != 3) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkPureFunction_(const char code[], const char* file, int line) {