Fixed #5850 (Wrong Message on self assignment)

This commit is contained in:
Daniel Marjamäki 2014-06-28 15:26:22 +02:00
parent 4d6c17818d
commit 41baffdda1
3 changed files with 11 additions and 8 deletions

View File

@ -2764,11 +2764,9 @@ void CheckOther::checkDuplicateExpression()
continue; continue;
if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) { if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) {
if (isWithoutSideEffects(_tokenizer, tok->astOperand1())) { if (isWithoutSideEffects(_tokenizer, tok->astOperand1())) {
if (assignment) { if (assignment)
// Need a hack to cope with our ternary operator simplification: selfAssignmentError(tok, tok->astOperand1()->expressionString());
if (!Token::Match(tok->tokAt(-2), "{ %var% = %var% ; } else { %varid% = !!%varid%", tok->previous()->varId())) else
selfAssignmentError(tok, tok->strAt(-1));
} else
duplicateExpressionError(tok, tok, tok->str()); duplicateExpressionError(tok, tok, tok->str());
} }
} else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative } else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative

View File

@ -1166,8 +1166,13 @@ std::string Token::expressionString() const
while (start->astOperand1() && start->astOperand2()) while (start->astOperand1() && start->astOperand2())
start = start->astOperand1(); start = start->astOperand1();
const Token *end = top; const Token *end = top;
while (end->astOperand1() && end->astOperand2()) while (end->astOperand1() && end->astOperand2()) {
if (Token::Match(end,"(|[")) {
end = end->link();
break;
}
end = end->astOperand2(); end = end->astOperand2();
}
std::string ret; std::string ret;
for (const Token *tok = start; tok && tok != end; tok = tok->next()) { for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
ret += tok->str(); ret += tok->str();

View File

@ -3424,7 +3424,7 @@ private:
"void foo(A* a1, A* a2) {\n" "void foo(A* a1, A* a2) {\n"
" a1->b = a1->b;\n" " a1->b = a1->b;\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'a1->b' to itself.\n", "[test.cpp:3]: (warning) Redundant assignment of 'b' to itself.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'a1.b' to itself.\n", errout.str());
// #4073 (segmentation fault) // #4073 (segmentation fault)
check("void Foo::myFunc( int a )\n" check("void Foo::myFunc( int a )\n"
@ -3494,7 +3494,7 @@ private:
"void Foo::func() {\n" "void Foo::func() {\n"
" this->var = var;\n" " this->var = var;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'var' to itself.\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'this.var' to itself.\n", errout.str());
check("class Foo {\n" check("class Foo {\n"
" int var;\n" " int var;\n"