Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,25 @@ class TestCondition : public TestFixture {
" 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) {
Expand Down