diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 447dcd032..782f18b4d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2764,11 +2764,9 @@ void CheckOther::checkDuplicateExpression() continue; if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) { if (isWithoutSideEffects(_tokenizer, tok->astOperand1())) { - if (assignment) { - // Need a hack to cope with our ternary operator simplification: - if (!Token::Match(tok->tokAt(-2), "{ %var% = %var% ; } else { %varid% = !!%varid%", tok->previous()->varId())) - selfAssignmentError(tok, tok->strAt(-1)); - } else + if (assignment) + selfAssignmentError(tok, tok->astOperand1()->expressionString()); + else duplicateExpressionError(tok, tok, tok->str()); } } else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative diff --git a/lib/token.cpp b/lib/token.cpp index 3ed697c31..6555516ec 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1166,8 +1166,13 @@ std::string Token::expressionString() const while (start->astOperand1() && start->astOperand2()) start = start->astOperand1(); 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(); + } std::string ret; for (const Token *tok = start; tok && tok != end; tok = tok->next()) { ret += tok->str(); diff --git a/test/testother.cpp b/test/testother.cpp index ca6a915a4..e5a21c0c8 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3424,7 +3424,7 @@ private: "void foo(A* a1, A* a2) {\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) check("void Foo::myFunc( int a )\n" @@ -3494,7 +3494,7 @@ private: "void Foo::func() {\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" " int var;\n"