Fixed #7229 (Improve check; unknown evaluation order, write different message for self assignment)
This commit is contained in:
parent
07b929e13f
commit
27af1bcfd8
|
@ -2494,9 +2494,11 @@ void CheckOther::checkEvaluationOrder()
|
||||||
continue;
|
continue;
|
||||||
if (!tok->astOperand1())
|
if (!tok->astOperand1())
|
||||||
continue;
|
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
|
// If ast parent is a sequence point then break
|
||||||
const Token * const parent = tok2->astParent();
|
const Token * const parent = tok2->astParent();
|
||||||
|
if (!parent)
|
||||||
|
break;
|
||||||
if (Token::Match(parent, "%oror%|&&|?|:|;"))
|
if (Token::Match(parent, "%oror%|&&|?|:|;"))
|
||||||
break;
|
break;
|
||||||
if (parent->str() == ",") {
|
if (parent->str() == ",") {
|
||||||
|
@ -2509,6 +2511,16 @@ void CheckOther::checkEvaluationOrder()
|
||||||
if (parent->str() == "(" && parent->astOperand2())
|
if (parent->str() == "(" && parent->astOperand2())
|
||||||
break;
|
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?
|
// Is expression used?
|
||||||
bool foundError = false;
|
bool foundError = false;
|
||||||
std::stack<const Token *> tokens;
|
std::stack<const Token *> tokens;
|
||||||
|
|
|
@ -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());
|
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
|
// sequence points
|
||||||
{
|
{
|
||||||
// FP
|
// FP
|
||||||
|
|
Loading…
Reference in New Issue