Fixed #9420 (False positive - redundantInitialization)

This commit is contained in:
Daniel Marjamäki 2019-12-18 19:39:23 +01:00
parent 7977bbf456
commit 235ef0a01e
2 changed files with 22 additions and 0 deletions

View File

@ -1681,6 +1681,15 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
return Result(Result::Type::BAILOUT);
}
if (mWhat == What::Reassign &&
Token::simpleMatch(tok, ";") &&
Token::simpleMatch(tok->astParent(), ";") &&
Token::simpleMatch(tok->astParent()->astParent(), "(") &&
Token::simpleMatch(tok->astParent()->astParent()->previous(), "for (") &&
!isUnchanged(tok, tok->astParent()->astParent()->link(), exprVarIds, local))
// TODO: This is a quick bailout to avoid FP #9420, there are false negatives (TODO_ASSERT_EQUALS)
return Result(Result::Type::BAILOUT);
if (expr->isName() && Token::Match(tok, "%name% (") && tok->str().find("<") != std::string::npos && tok->str().find(expr->str()) != std::string::npos)
return Result(Result::Type::BAILOUT);

View File

@ -6629,6 +6629,19 @@ private:
" } while (false);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(int num) {\n" // #9420 FP
" int a = num;\n"
" for (int b = 0; b < num; a = b++)\n"
" dostuff(a);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(int num) {\n" // #9420 FN
" int a = num;\n"
" for (int b = 0; b < num; a = b++);\n"
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
}
void redundantVarAssignment_after_switch() {