Fix #11099 FP: variableScope when using range constructor to vector (#4161)

* Fix #11099 FP: variableScope when using range constructor to vector

* Format
This commit is contained in:
chrchr-github 2022-06-01 21:15:41 +02:00 committed by GitHub
parent 204b91a295
commit 2452a2c01d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 2 deletions

View File

@ -945,10 +945,32 @@ void CheckOther::checkVariableScope()
if (forHead)
continue;
auto isSimpleExpr = [](const Token* tok) {
return tok && (tok->isNumber() || tok->tokType() == Token::eString || tok->tokType() == Token::eChar || tok->isBoolean());
};
const Token* tok = var->nameToken()->next();
if (Token::Match(tok, "; %varid% = %any% ;", var->declarationId())) {
if (Token::Match(tok, "; %varid% = %any% ;", var->declarationId())) { // bail for assignment
tok = tok->tokAt(3);
if (!tok->isNumber() && tok->tokType() != Token::eString && tok->tokType() != Token::eChar && !tok->isBoolean())
if (!isSimpleExpr(tok))
continue;
}
else if (Token::Match(tok, "{|(")) { // bail for constructor
const Token* argTok = tok->astOperand2();
bool bail = false;
do {
if (Token::simpleMatch(argTok, ",")) {
if (!isSimpleExpr(argTok->astOperand2())) {
bail = true;
break;
}
} else if (!isSimpleExpr(argTok)) {
bail = true;
break;
}
argTok = argTok->astOperand1();
} while (argTok);
if (bail)
continue;
}
// bailout if initialized with function call that has possible side effects

View File

@ -98,6 +98,7 @@ private:
TEST_CASE(varScope28); // #10527
TEST_CASE(varScope29); // #10888
TEST_CASE(varScope30); // #8541
TEST_CASE(varScope31); // #11099
TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
@ -1380,6 +1381,86 @@ private:
ASSERT_EQUALS("", errout.str());
}
void varScope31() { // #11099
check("bool g(std::vector<int>&);\n"
"void h(std::vector<int>);\n"
"void f0(std::vector<int> v) {\n"
" std::vector<int> w{ v };"
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f1(std::vector<int> v) {\n"
" std::vector<int> w{ v.begin(), v.end() };"
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f2(std::vector<int> v) {\n"
" std::vector<int> w{ 10, 0, std::allocator<int>() };" // FN
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f3(std::vector<int> v) {\n"
" std::vector<int> w{ 10, 0 };" // warn
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f4(std::vector<int> v) {\n"
" std::vector<int> w{ 10 };" // warn
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f5(std::vector<int> v) {\n"
" std::vector<int> w(v);"
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f6(std::vector<int> v) {\n"
" std::vector<int> w(v.begin(), v.end());"
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f7(std::vector<int> v) {\n"
" std::vector<int> w(10, 0, std::allocator<int>);" // FN
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f8(std::vector<int> v) {\n"
" std::vector<int> w(10, 0);" // warn
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f9(std::vector<int> v) {\n"
" std::vector<int> w(10);" // warn
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:22]: (style) The scope of the variable 'w' can be reduced.\n"
"[test.cpp:28]: (style) The scope of the variable 'w' can be reduced.\n"
"[test.cpp:52]: (style) The scope of the variable 'w' can be reduced.\n"
"[test.cpp:58]: (style) The scope of the variable 'w' can be reduced.\n",
errout.str());
}
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
// Clear the error buffer..