FwdAnalysis: Fix false negatives for struct members

This commit is contained in:
Daniel Marjamäki 2018-12-16 18:32:34 +01:00
parent 30220fa7fc
commit 21eb1c5e22
2 changed files with 17 additions and 1 deletions

View File

@ -1137,8 +1137,16 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
if (exprVarIds.find(tok->varId()) != exprVarIds.end()) {
const Token *parent = tok;
while (Token::Match(parent->astParent(), ".|::|["))
bool other = false;
while (Token::Match(parent->astParent(), ".|::|[")) {
parent = parent->astParent();
if (Token::Match(parent, ". %var%") && parent->next()->varId() && exprVarIds.find(parent->next()->varId()) == exprVarIds.end()) {
other = true;
break;
}
}
if (other)
continue;
if (Token::simpleMatch(parent->astParent(), "=") && parent == parent->astParent()->astOperand1()) {
if (!local && hasFunctionCall(parent->astParent()->astOperand2())) {
// TODO: this is a quick bailout

View File

@ -5981,6 +5981,14 @@ private:
" x.a = _mm_set1_ps(2.0);\n"
"}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:8]: (style) Variable 'x.a' is reassigned a value before the old one has been used.\n", errout.str());
check("void f() {\n"
" struct AB ab;\n"
" ab.x = 23;\n"
" ab.y = 41;\n"
" ab.x = 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Variable 'ab.x' is reassigned a value before the old one has been used.\n", errout.str());
}
void redundantVarAssignment_7133() {