From c966f31183253e62dc6c138acd50fcfc5c0ebb4e Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Tue, 6 Nov 2018 00:44:08 -0500 Subject: [PATCH] Fixed #8835 (friend class and non-empty constructor: Uninitialized members not reported) (#1466) --- lib/checkclass.cpp | 2 +- test/testconstructors.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 02ba55e2b..6f136bfd5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -764,7 +764,7 @@ void CheckClass::initializeVarList(const Function &func, std::liststr() != "if") { + } else if (Token::Match(ftok, "::| %name% (") && !Token::Match(ftok, "if|while|for")) { if (ftok->str() == "::") ftok = ftok->next(); diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index eebc8106b..d8fe11ea5 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -145,6 +145,7 @@ private: TEST_CASE(uninitVar29); TEST_CASE(uninitVar30); // ticket #6417 TEST_CASE(uninitVar31); // ticket #8271 + TEST_CASE(uninitVar32); // ticket #8835 TEST_CASE(uninitVarEnum1); TEST_CASE(uninitVarEnum2); // ticket #8146 TEST_CASE(uninitVarStream); @@ -2426,6 +2427,39 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitVar32() { // ticket #8835 + check("class Foo {\n" + " friend class Bar;\n" + " int member;\n" + "public:\n" + " Foo()\n" + " {\n" + " if (1) {}\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str()); + check("class Foo {\n" + " friend class Bar;\n" + " int member;\n" + "public:\n" + " Foo()\n" + " {\n" + " while (1) {}\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str()); + check("class Foo {\n" + " friend class Bar;\n" + " int member;\n" + "public:\n" + " Foo()\n" + " {\n" + " for (;;) {}\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str()); + } + void uninitVarArray1() { check("class John\n" "{\n"