From cd7362e0e7da094709ea0251c2fb71449787e762 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 1 Jun 2022 23:18:59 +0200 Subject: [PATCH] Fix FN with default init (#4162) * Fix #11099 FP: variableScope when using range constructor to vector * Format * Fix FN with default init --- lib/checkother.cpp | 8 ++++---- test/testother.cpp | 36 ++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 12f83b70e..54e2e19f8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -950,15 +950,15 @@ void CheckOther::checkVariableScope() }; 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); if (!isSimpleExpr(tok)) continue; } - else if (Token::Match(tok, "{|(")) { // bail for constructor + else if (Token::Match(tok, "{|(")) { // bailout for constructor const Token* argTok = tok->astOperand2(); bool bail = false; - do { + while (argTok) { if (Token::simpleMatch(argTok, ",")) { if (!isSimpleExpr(argTok->astOperand2())) { bail = true; @@ -969,7 +969,7 @@ void CheckOther::checkVariableScope() break; } argTok = argTok->astOperand1(); - } while (argTok); + } if (bail) continue; } diff --git a/test/testother.cpp b/test/testother.cpp index 81dfb05ff..f132beee2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1385,79 +1385,87 @@ private: check("bool g(std::vector&);\n" "void h(std::vector);\n" "void f0(std::vector v) {\n" - " std::vector w{ v };" + " std::vector w{ v };\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f1(std::vector v) {\n" - " std::vector w{ v.begin(), v.end() };" + " std::vector w{ v.begin(), v.end() };\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f2(std::vector v) {\n" - " std::vector w{ 10, 0, std::allocator() };" // FN + " std::vector w{ 10, 0, std::allocator() };\n" // FN " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f3(std::vector v) {\n" - " std::vector w{ 10, 0 };" // warn + " std::vector w{ 10, 0 };\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f4(std::vector v) {\n" - " std::vector w{ 10 };" // warn + " std::vector w{ 10 };\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f5(std::vector v) {\n" - " std::vector w(v);" + " std::vector w(v);\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f6(std::vector v) {\n" - " std::vector w(v.begin(), v.end());" + " std::vector w(v.begin(), v.end());\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f7(std::vector v) {\n" - " std::vector w(10, 0, std::allocator);" // FN + " std::vector w(10, 0, std::allocator);\n" // FN " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f8(std::vector v) {\n" - " std::vector w(10, 0);" // warn + " std::vector w(10, 0);\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f9(std::vector v) {\n" - " std::vector w(10);" // warn + " std::vector w(10);\n" // warn + " bool b = g(v);\n" + " if (b)\n" + " h(w);\n" + " h(v);\n" + "}\n" + "void f10(std::vector v) {\n" + " std::vector w{};\n" // 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", + ASSERT_EQUALS("[test.cpp:25]: (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:60]: (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()); }