Skip to content

Commit 510a29d

Browse files
Partial fix for #6294 FN uninitMemberVar for pointers used in constructor (#7983)
1 parent cb76e52 commit 510a29d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/checkclass.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,13 +1084,11 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
10841084
}
10851085

10861086
// Assignment of array item of member variable?
1087-
else if (Token::Match(ftok, "* %name% =")) {
1087+
else if (Token::Match(ftok, "* %name% =") && (!func.isConstructor() || (!ftok->next()->variable() || ftok->next()->variable()->isArray()))) {
10881088
assignVar(usage, ftok->next()->varId());
1089-
} else if (Token::Match(ftok, "( * %name% ) =")) {
1089+
} else if (Token::Match(ftok, "( * %name% ) =") && (!func.isConstructor() || (!ftok->tokAt(2)->variable() || ftok->tokAt(2)->variable()->isArray()))) {
10901090
assignVar(usage, ftok->tokAt(2)->varId());
1091-
} else if (Token::Match(ftok, "* ( %name% ) =")) {
1092-
assignVar(usage, ftok->tokAt(2)->varId());
1093-
} else if (Token::Match(ftok, "* this . %name% =")) {
1091+
} else if (Token::Match(ftok, "* this . %name% =") && (!func.isConstructor() || (!ftok->tokAt(3)->variable() || ftok->tokAt(3)->variable()->isArray()))) {
10941092
assignVar(usage, ftok->tokAt(3)->varId());
10951093
} else if (astIsRangeBasedForDecl(ftok)) {
10961094
if (const Variable* rangeVar = ftok->astParent()->astOperand1()->variable()) {

test/testconstructors.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4165,6 +4165,18 @@ class TestConstructors : public TestFixture {
41654165
" std::map<int, int>* pMap = nullptr;\n"
41664166
"};\n");
41674167
ASSERT_EQUALS("", errout_str());
4168+
4169+
check("struct S {\n" // #6294
4170+
" S() : r(new int) {\n"
4171+
" *p = 0;\n"
4172+
" *(q) = 1;\n"
4173+
" *r = 2;\n"
4174+
" }\n"
4175+
" int *p, *q, *r;\n"
4176+
"};\n");
4177+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Member variable 'S::p' is not initialized in the constructor. [uninitMemberVar]\n"
4178+
"[test.cpp:2:5]: (warning) Member variable 'S::q' is not initialized in the constructor. [uninitMemberVar]\n",
4179+
errout_str());
41684180
}
41694181

41704182
void uninitConstVar() {

0 commit comments

Comments
 (0)