Fix #10337 FP selfAssignment (#4682)

This commit is contained in:
chrchr-github 2023-01-18 16:59:56 +01:00 committed by GitHub
parent edbd4f6e81
commit dee2ad8756
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -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,"<|>")) ||

View File

@ -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() {