Fix issue 9894: ValueFlow: wrong known value below while with assignment (#2804)

* Fix issue 9894: ValueFlow: wrong known value below while with assignment
This commit is contained in:
Paul Fultz II 2020-09-14 01:03:25 -05:00 committed by GitHub
parent ebb5ff0e19
commit bb7164171c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -4510,9 +4510,10 @@ struct ValueFlowConditionHandler {
if (Token::simpleMatch(top->link(), ") {")) {
Token *after = top->link()->linkAt(1);
const Token* unknownFunction = nullptr;
bool dead_if = isReturnScope(after, &settings->library, &unknownFunction) ||
(tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "while (") &&
!isBreakScope(after));
const bool isWhile =
tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "while (");
bool dead_if = (!isBreakScope(after) && isWhile) ||
(isReturnScope(after, &settings->library, &unknownFunction) && !isWhile);
bool dead_else = false;
if (!dead_if && unknownFunction) {

View File

@ -2478,6 +2478,20 @@ private:
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXImpossible(code, 8U, 0));
code = "bool c();\n"
"long f() {\n"
" bool stop = false;\n"
" while (!stop) {\n"
" if (c())\n"
" stop = true;\n"
" break;\n"
" }\n"
" int x = !stop;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXImpossible(code, 10U, 1));
ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 0));
}
void valueFlowAfterConditionExpr() {