From 990340ba9892f1b11bfbffd9a36154a30f1a5cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Sep 2012 18:29:05 +0200 Subject: [PATCH] Fixed #4072 (False positive: Structure is not initialized in the constructor (1.55)) --- lib/checkclass.cpp | 12 ++++++++++++ test/testconstructors.cpp | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index adf1feeba..c6e5c63fa 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -536,6 +536,18 @@ void CheckClass::initializeVarList(const Function &func, std::list callstack.push_back(ftok->str()); initializeVarList(*it, callstack, scope, usage); callstack.pop_back(); + + // Assume that variables that are passed to it are initialized.. + for (const Token *tok2 = ftok; tok2; tok2 = tok2->next()) { + if (Token::Match(tok2, "[;{}]")) + break; + if (Token::Match(tok2, "[(,] &| %var% [,)]")) { + tok2 = tok2->next(); + if (tok2->str() == "&") + tok2 = tok2->next(); + assignVar(tok2->str(), scope, usage); + } + } } // there is a called member function, but it has no implementation, so we assume it initializes everything diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 0a7f853ef..c6e6eaf61 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -139,6 +139,7 @@ private: TEST_CASE(uninitFunction2); // No FP when initialized in function TEST_CASE(uninitFunction3); // No FP when initialized in function TEST_CASE(uninitFunction4); + TEST_CASE(uninitFunction5); TEST_CASE(uninitSameClassName); // No FP when two classes have the same name TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions TEST_CASE(uninitJava); // Java: no FP when variable is initialized in declaration @@ -2207,6 +2208,20 @@ private: TODO_ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n", "", errout.str()); } + void uninitFunction5() { // #4072 - FP about struct that is initialized in function + check("struct Structure {\n" + " int C;\n" + "};\n" + "\n" + "class A {\n" + " Structure B;\n" + "public:\n" + " A() { Init( B ); };\n" + " void Init( Structure& S ) { S.C = 0; };\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + void uninitSameClassName() { check("class B\n" "{\n"