Fix remaining example from #11131 (#5029)

This commit is contained in:
chrchr-github 2023-05-03 16:24:56 +02:00 committed by GitHub
parent 07b802fd2a
commit f04d47ac61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -922,12 +922,14 @@ void CheckOther::checkVariableScope()
continue;
const Token* tok = var->nameToken()->next();
bool isConstructor = false;
if (Token::Match(tok, "; %varid% =", var->declarationId())) { // bailout for assignment
tok = tok->tokAt(2)->astOperand2();
if (!isSimpleExpr(tok, var, mSettings))
continue;
}
else if (Token::Match(tok, "{|(")) { // bailout for constructor
isConstructor = true;
const Token* argTok = tok->astOperand2();
bool bail = false;
while (argTok) {
@ -946,7 +948,7 @@ void CheckOther::checkVariableScope()
continue;
}
// bailout if initialized with function call that has possible side effects
if (Token::Match(tok, "[(=]") && Token::simpleMatch(tok->astOperand2(), "("))
if (!isConstructor && Token::Match(tok, "[(=]") && Token::simpleMatch(tok->astOperand2(), "("))
continue;
bool reduce = true;
bool used = false; // Don't warn about unused variables

View File

@ -1587,9 +1587,11 @@ private:
"void f(const S& s) {\n"
" std::string str = s.getStr();\n"
" std::string str2{ s.getStr() };\n"
" std::string str3(s.getStr());\n"
" if (s.getB()) {\n"
" if (str == \"abc\") {}\n"
" if (str2 == \"abc\") {}\n"
" if (str3 == \"abc\") {}\n"
" }\n"
"}\n"
"void f(const char* s, bool b) {\n"
@ -1606,8 +1608,9 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:21]: (style) The scope of the variable 'str' can be reduced.\n"
"[test.cpp:22]: (style) The scope of the variable 'str2' can be reduced.\n"
"[test.cpp:29]: (style) The scope of the variable 'i' can be reduced.\n"
"[test.cpp:35]: (style) The scope of the variable 'k' can be reduced.\n",
"[test.cpp:23]: (style) The scope of the variable 'str3' can be reduced.\n"
"[test.cpp:31]: (style) The scope of the variable 'i' can be reduced.\n"
"[test.cpp:37]: (style) The scope of the variable 'k' can be reduced.\n",
errout.str());
}

View File

@ -6014,6 +6014,15 @@ private:
" if (tmp[0]) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #11055
valueFlowUninit("void g(int*);\n"
"void f(bool b) {\n"
" int i;\n"
" int* p = b ? &i : nullptr;\n"
" g(p);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value