From dee2ad875637990cd55803404fd6201aa83ed5cc Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 18 Jan 2023 16:59:56 +0100 Subject: [PATCH] Fix #10337 FP selfAssignment (#4682) --- lib/astutils.cpp | 10 +++++++++- test/testother.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 41d2f882b..e365a4268 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1481,8 +1481,16 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 if (!tok_str_eq) { const Token* refTok1 = followReferences(tok1, errors); const Token* refTok2 = followReferences(tok2, errors); - if (refTok1 != tok1 || refTok2 != tok2) + if (refTok1 != tok1 || refTok2 != tok2) { + if (refTok1 && !refTok1->varId() && refTok2 && !refTok2->varId()) { // complex reference expression + const Token *start = refTok1, *end = refTok2; + if (!precedes(start, end)) + std::swap(start, end); + if (isExpressionChanged(start, start, end, nullptr, cpp)) + return false; + } return isSameExpression(cpp, macro, refTok1, refTok2, library, pure, followVar, errors); + } } if (tok1->varId() != tok2->varId() || !tok_str_eq || tok1->originalName() != tok2->originalName()) { if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) || diff --git a/test/testother.cpp b/test/testother.cpp index 0e351f405..e6c291a2a 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4890,6 +4890,15 @@ private: " int i;\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #10337 + " int b[2] = { 1, 2 };\n" + " int idx = 0;\n" + " int& i = b[idx];\n" + " idx++;\n" + " i = b[idx];\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); } void trac1132() {