From 7364cc8040b4258580864af09649db082cb6d00e Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 22 Jan 2015 10:35:17 +0100 Subject: [PATCH] Fixed false positive in fityk: operator new calling constructor --- lib/checkuninitvar.cpp | 7 +++++-- test/testuninitvar.cpp | 13 ++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) 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 67834f925..edb3b6239 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"