Fix #9303 FP uninitvar after lambda expression (#3903)

* Fix #9303 FP uninitvar after lambda expression

* Format
This commit is contained in:
chrchr-github 2022-03-15 14:32:33 +01:00 committed by GitHub
parent f90a93591f
commit 1aff160411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -365,7 +365,7 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue> variableValue)
{
const bool suppressErrors(possibleInit && *possibleInit); // Assume that this is a variable delaratkon, rather than a fundef
const bool suppressErrors(possibleInit && *possibleInit); // Assume that this is a variable declaration, rather than a fundef
const bool printDebug = mSettings->debugwarnings;
if (possibleInit)
@ -392,8 +392,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
break;
}
// Unconditional inner scope or try..
if (tok->str() == "{" && Token::Match(tok->previous(), ",|;|{|}|try")) {
// Unconditional inner scope, try, lambda, init list
if (tok->str() == "{" && Token::Match(tok->previous(), ",|;|{|}|]|try")) {
bool possibleInitInner = false;
if (checkScopeForVariable(tok->next(), var, &possibleInitInner, noreturn, alloc, membervar, variableValue))
return true;

View File

@ -3079,6 +3079,35 @@ private:
" } catch(...) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
checkUninitVar("void f(bool x) {\n"
" bool b;\n"
" {\n"
" auto g = []{};\n"
" b = x;\n"
" }\n"
" if (b) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f(bool x) {\n"
" bool b;\n"
" {\n"
" int i[2]{ 1, 2 };\n"
" b = x;\n"
" }\n"
" if (b) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f(bool x) {\n"
" bool b;\n"
" {\n"
" auto g = []{};\n"
" }\n"
" if (b) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: b\n", errout.str());
}
void uninitvar_funcptr() {