Fix #11788 False positive: unreadVariable in else (#5248)

This commit is contained in:
chrchr-github 2023-07-18 21:11:03 +02:00 committed by GitHub
parent 2878c68ec0
commit d2546d5252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -3844,6 +3844,13 @@ const Token* ValueFlow::getEndOfExprScope(const Token* tok, const Scope* default
const Token* varEnd = getEndOfVarScope(var);
if (!end || (smallest ? precedes(varEnd, end) : succeeds(varEnd, end)))
end = varEnd;
const Token* top = var->nameToken()->astTop();
if (top && Token::simpleMatch(top->tokAt(-1), "if (")) { // variable declared in if (...)
const Token* elseTok = top->link()->linkAt(1);
if (Token::simpleMatch(elseTok, "} else {") && tok->scope()->isNestedIn(elseTok->tokAt(2)->scope()))
end = tok->scope()->bodyEnd;
}
}
}
return ChildrenToVisit::op1_and_op2;

View File

@ -5446,6 +5446,37 @@ private:
" return y;\n"
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("int f(int i) {\n" // #11788
" if (int x = i) {\n"
" return x;\n"
" }\n"
" else {\n"
" x = 12;\n"
" return x;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f(int i) {\n"
" if (int x = i) {\n"
" while (x < 100) {\n"
" if (x % 2 == 0) {\n"
" x += 3;\n"
" }\n"
" else if (x % 3 == 0) {\n"
" x += 5;\n"
" }\n"
" else {\n"
" x += 7;\n"
" }\n"
" x += 6;\n"
" }\n"
" return x;\n"
" }\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarOpAssign() {