Skip to content

Commit c966f31

Browse files
IOBYTEdanmar
authored andcommitted
Fixed #8835 (friend class and non-empty constructor: Uninitialized members not reported) (cppcheck-opensource#1466)
1 parent 00340ef commit c966f31

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
764764
else {
765765
assignAllVar(usage);
766766
}
767-
} else if (Token::Match(ftok, "::| %name% (") && ftok->str() != "if") {
767+
} else if (Token::Match(ftok, "::| %name% (") && !Token::Match(ftok, "if|while|for")) {
768768
if (ftok->str() == "::")
769769
ftok = ftok->next();
770770

test/testconstructors.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class TestConstructors : public TestFixture {
145145
TEST_CASE(uninitVar29);
146146
TEST_CASE(uninitVar30); // ticket #6417
147147
TEST_CASE(uninitVar31); // ticket #8271
148+
TEST_CASE(uninitVar32); // ticket #8835
148149
TEST_CASE(uninitVarEnum1);
149150
TEST_CASE(uninitVarEnum2); // ticket #8146
150151
TEST_CASE(uninitVarStream);
@@ -2426,6 +2427,39 @@ class TestConstructors : public TestFixture {
24262427
ASSERT_EQUALS("", errout.str());
24272428
}
24282429

2430+
void uninitVar32() { // ticket #8835
2431+
check("class Foo {\n"
2432+
" friend class Bar;\n"
2433+
" int member;\n"
2434+
"public:\n"
2435+
" Foo()\n"
2436+
" {\n"
2437+
" if (1) {}\n"
2438+
" }\n"
2439+
"};\n");
2440+
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
2441+
check("class Foo {\n"
2442+
" friend class Bar;\n"
2443+
" int member;\n"
2444+
"public:\n"
2445+
" Foo()\n"
2446+
" {\n"
2447+
" while (1) {}\n"
2448+
" }\n"
2449+
"};\n");
2450+
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
2451+
check("class Foo {\n"
2452+
" friend class Bar;\n"
2453+
" int member;\n"
2454+
"public:\n"
2455+
" Foo()\n"
2456+
" {\n"
2457+
" for (;;) {}\n"
2458+
" }\n"
2459+
"};\n");
2460+
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
2461+
}
2462+
24292463
void uninitVarArray1() {
24302464
check("class John\n"
24312465
"{\n"

0 commit comments

Comments
 (0)