Fixed #7229 (Improve check; unknown evaluation order, write different message for self assignment)

This commit is contained in:
Daniel Marjamäki 2015-12-25 18:00:19 +01:00
parent 07b929e13f
commit 27af1bcfd8
2 changed files with 19 additions and 1 deletions

View File

@ -2494,9 +2494,11 @@ void CheckOther::checkEvaluationOrder()
continue;
if (!tok->astOperand1())
continue;
for (const Token *tok2 = tok; tok2 && tok2->astParent(); tok2 = tok2->astParent()) {
for (const Token *tok2 = tok;; tok2 = tok2->astParent()) {
// If ast parent is a sequence point then break
const Token * const parent = tok2->astParent();
if (!parent)
break;
if (Token::Match(parent, "%oror%|&&|?|:|;"))
break;
if (parent->str() == ",") {
@ -2509,6 +2511,16 @@ void CheckOther::checkEvaluationOrder()
if (parent->str() == "(" && parent->astOperand2())
break;
// self assignment..
if (tok2 == tok &&
tok->str() == "=" &&
parent->str() == "=" &&
isSameExpression(_tokenizer->isCPP(), tok->astOperand1(), parent->astOperand1(), _settings->library.functionpure)) {
if (_settings->isEnabled("warning"))
selfAssignmentError(parent, tok->astOperand1()->expressionString());
break;
}
// Is expression used?
bool foundError = false;
std::stack<const Token *> tokens;

View File

@ -6112,6 +6112,12 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Expression '++exp,(char**)&exp' depends on order of evaluation of side effects\n", errout.str());
// self assignment
check("void f() {\n"
" int x = x = y + 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant assignment of 'x' to itself.\n", errout.str());
// sequence points
{
// FP