Skip to content

Commit dee2ad8

Browse files
Fix #10337 FP selfAssignment (#4682)
1 parent edbd4f6 commit dee2ad8

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/astutils.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,16 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
14811481
if (!tok_str_eq) {
14821482
const Token* refTok1 = followReferences(tok1, errors);
14831483
const Token* refTok2 = followReferences(tok2, errors);
1484-
if (refTok1 != tok1 || refTok2 != tok2)
1484+
if (refTok1 != tok1 || refTok2 != tok2) {
1485+
if (refTok1 && !refTok1->varId() && refTok2 && !refTok2->varId()) { // complex reference expression
1486+
const Token *start = refTok1, *end = refTok2;
1487+
if (!precedes(start, end))
1488+
std::swap(start, end);
1489+
if (isExpressionChanged(start, start, end, nullptr, cpp))
1490+
return false;
1491+
}
14851492
return isSameExpression(cpp, macro, refTok1, refTok2, library, pure, followVar, errors);
1493+
}
14861494
}
14871495
if (tok1->varId() != tok2->varId() || !tok_str_eq || tok1->originalName() != tok2->originalName()) {
14881496
if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) ||

test/testother.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4890,6 +4890,15 @@ class TestOther : public TestFixture {
48904890
" int i;\n"
48914891
"};\n");
48924892
ASSERT_EQUALS("", errout.str());
4893+
4894+
check("void f() {\n" // #10337
4895+
" int b[2] = { 1, 2 };\n"
4896+
" int idx = 0;\n"
4897+
" int& i = b[idx];\n"
4898+
" idx++;\n"
4899+
" i = b[idx];\n"
4900+
"};\n");
4901+
ASSERT_EQUALS("", errout.str());
48934902
}
48944903

48954904
void trac1132() {

0 commit comments

Comments
 (0)