Fix FN with default init (#4162)

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

* Format

* Fix FN with default init
This commit is contained in:
chrchr-github 2022-06-01 23:18:59 +02:00 committed by GitHub
parent 2452a2c01d
commit cd7362e0e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 18 deletions

View File

@ -950,15 +950,15 @@ void CheckOther::checkVariableScope()
}; };
const Token* tok = var->nameToken()->next(); const Token* tok = var->nameToken()->next();
if (Token::Match(tok, "; %varid% = %any% ;", var->declarationId())) { // bail for assignment if (Token::Match(tok, "; %varid% = %any% ;", var->declarationId())) { // bailout for assignment
tok = tok->tokAt(3); tok = tok->tokAt(3);
if (!isSimpleExpr(tok)) if (!isSimpleExpr(tok))
continue; continue;
} }
else if (Token::Match(tok, "{|(")) { // bail for constructor else if (Token::Match(tok, "{|(")) { // bailout for constructor
const Token* argTok = tok->astOperand2(); const Token* argTok = tok->astOperand2();
bool bail = false; bool bail = false;
do { while (argTok) {
if (Token::simpleMatch(argTok, ",")) { if (Token::simpleMatch(argTok, ",")) {
if (!isSimpleExpr(argTok->astOperand2())) { if (!isSimpleExpr(argTok->astOperand2())) {
bail = true; bail = true;
@ -969,7 +969,7 @@ void CheckOther::checkVariableScope()
break; break;
} }
argTok = argTok->astOperand1(); argTok = argTok->astOperand1();
} while (argTok); }
if (bail) if (bail)
continue; continue;
} }

View File

@ -1385,79 +1385,87 @@ private:
check("bool g(std::vector<int>&);\n" check("bool g(std::vector<int>&);\n"
"void h(std::vector<int>);\n" "void h(std::vector<int>);\n"
"void f0(std::vector<int> v) {\n" "void f0(std::vector<int> v) {\n"
" std::vector<int> w{ v };" " std::vector<int> w{ v };\n"
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f1(std::vector<int> v) {\n" "void f1(std::vector<int> v) {\n"
" std::vector<int> w{ v.begin(), v.end() };" " std::vector<int> w{ v.begin(), v.end() };\n"
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f2(std::vector<int> v) {\n" "void f2(std::vector<int> v) {\n"
" std::vector<int> w{ 10, 0, std::allocator<int>() };" // FN " std::vector<int> w{ 10, 0, std::allocator<int>() };\n" // FN
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f3(std::vector<int> v) {\n" "void f3(std::vector<int> v) {\n"
" std::vector<int> w{ 10, 0 };" // warn " std::vector<int> w{ 10, 0 };\n" // warn
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f4(std::vector<int> v) {\n" "void f4(std::vector<int> v) {\n"
" std::vector<int> w{ 10 };" // warn " std::vector<int> w{ 10 };\n" // warn
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f5(std::vector<int> v) {\n" "void f5(std::vector<int> v) {\n"
" std::vector<int> w(v);" " std::vector<int> w(v);\n"
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f6(std::vector<int> v) {\n" "void f6(std::vector<int> v) {\n"
" std::vector<int> w(v.begin(), v.end());" " std::vector<int> w(v.begin(), v.end());\n"
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f7(std::vector<int> v) {\n" "void f7(std::vector<int> v) {\n"
" std::vector<int> w(10, 0, std::allocator<int>);" // FN " std::vector<int> w(10, 0, std::allocator<int>);\n" // FN
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f8(std::vector<int> v) {\n" "void f8(std::vector<int> v) {\n"
" std::vector<int> w(10, 0);" // warn " std::vector<int> w(10, 0);\n" // warn
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n" "}\n"
"void f9(std::vector<int> v) {\n" "void f9(std::vector<int> v) {\n"
" std::vector<int> w(10);" // warn " std::vector<int> w(10);\n" // warn
" bool b = g(v);\n"
" if (b)\n"
" h(w);\n"
" h(v);\n"
"}\n"
"void f10(std::vector<int> v) {\n"
" std::vector<int> w{};\n" // warn
" bool b = g(v);\n" " bool b = g(v);\n"
" if (b)\n" " if (b)\n"
" h(w);\n" " h(w);\n"
" h(v);\n" " h(v);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:22]: (style) The scope of the variable 'w' can be reduced.\n" ASSERT_EQUALS("[test.cpp:25]: (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:32]: (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:60]: (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", "[test.cpp:67]: (style) The scope of the variable 'w' can be reduced.\n"
"[test.cpp:74]: (style) The scope of the variable 'w' can be reduced.\n",
errout.str()); errout.str());
} }