diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 074a435a9..cbd73d0a5 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1495,9 +1495,12 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, *alloc = true; continue; } - if (var.isPointer() && (_tokenizer->isC() || var.typeStartToken()->isStandardType() || (var.type() && var.type()->needInitialization == Type::True)) && Token::Match(tok->next(), "= new")) { - if (alloc) + if (var.isPointer() && (var.typeStartToken()->isStandardType() || (var.type() && var.type()->needInitialization == Type::True)) && Token::Match(tok->next(), "= new")) { + if (alloc) { *alloc = true; + if (var.typeScope()->numConstructors > 0) + return false; + } continue; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 87bd6f15d..7801e34c6 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1455,21 +1455,28 @@ private: "{\n" " Fred *fred = new Fred;\n" " fred->foo();\n" - "};"); + "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVarB("struct Fred { int i; Fred(int, float); };\n" + "void f() {\n" + " Fred *fred = new Fred(1, 2);\n" + " fred->foo();\n" + "}"); ASSERT_EQUALS("", errout.str()); checkUninitVarB("void f()\n" "{\n" " Fred *fred = malloc(sizeof(Fred));\n" " x(&fred->f);\n" - "};"); + "}"); ASSERT_EQUALS("", errout.str()); checkUninitVarB("void f()\n" "{\n" " Fred *fred = malloc(sizeof(Fred));\n" " x(fred->f);\n" - "};"); + "}"); ASSERT_EQUALS("", errout.str()); checkUninitVarB("void foo(char *s)\n"