From f04d47ac6193b2994dbc68d527a082936484471e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 3 May 2023 16:24:56 +0200 Subject: [PATCH] Fix remaining example from #11131 (#5029) --- lib/checkother.cpp | 4 +++- test/testother.cpp | 7 +++++-- test/testuninitvar.cpp | 9 +++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8834d44c0..3eec33991 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 diff --git a/test/testother.cpp b/test/testother.cpp index b14eb6deb..de554a3ca 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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()); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 0e0e536f0..b15ba1da5 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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