diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 622beae8d..9ef52b334 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -565,7 +565,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } // = { .. } - else if (Token::simpleMatch(tok, "= {")) { + else if (Token::simpleMatch(tok, "= {") || (Token::Match(tok, "%name% {") && tok->variable() && tok == tok->variable()->nameToken())) { // end token const Token *end = tok->next()->link(); @@ -657,7 +657,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } // Unknown or unhandled inner scope - else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try")) { + else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try" && !(tok->variable() && tok == tok->variable()->nameToken()))) { if (tok->str() == "struct" || tok->str() == "union") { tok = tok->linkAt(1); continue; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6a9a9d9dd..032d86f98 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2048,6 +2048,15 @@ private: " A* a = new A{};\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar("int f() {\n" // #10596 + " int* a = new int;\n" + " int i{};\n" + " i += *a;\n" + " delete a;\n" + " return i;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: a\n", errout.str()); } // class / struct..