Skip to content

Commit 38bdece

Browse files
Fix #9391 False negative: Uninitialized struct member (default constructor) (#4088)
* Fix #9391 False negative: Uninitialized struct member (default constructor) * Format * Initialize variables * Init
1 parent be6daa9 commit 38bdece

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

lib/checkclass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void CheckClass::constructors()
206206
std::vector<Usage> usageList = createUsageList(scope);
207207

208208
for (const Function &func : scope->functionList) {
209-
if (!func.hasBody() || !(func.isConstructor() || func.type == Function::eOperatorEqual))
209+
if ((!func.hasBody() && !func.isDefault()) || !(func.isConstructor() || func.type == Function::eOperatorEqual))
210210
continue;
211211

212212
// Bail: If initializer list is not recognized as a variable or type then skip since parsing is incomplete
@@ -694,7 +694,8 @@ bool CheckClass::isBaseClassFunc(const Token *tok, const Scope *scope)
694694
void CheckClass::initializeVarList(const Function &func, std::list<const Function *> &callstack, const Scope *scope, std::vector<Usage> &usage)
695695
{
696696
if (!func.functionScope)
697-
throw InternalError(nullptr, "Internal Error: Invalid syntax"); // #5702
697+
return;
698+
698699
bool initList = func.isConstructor();
699700
const Token *ftok = func.arg->link()->next();
700701
int level = 0;

lib/ctu.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ namespace CTU {
6060
Location(const Tokenizer *tokenizer, const Token *tok);
6161
Location(const std::string &fileName, nonneg int lineNumber, nonneg int column) : fileName(fileName), lineNumber(lineNumber), column(column) {}
6262
std::string fileName;
63-
nonneg int lineNumber;
64-
nonneg int column;
63+
nonneg int lineNumber{};
64+
nonneg int column{};
6565
};
6666

6767
struct UnsafeUsage {
6868
UnsafeUsage() = default;
6969
UnsafeUsage(const std::string &myId, nonneg int myArgNr, const std::string &myArgumentName, const Location &location, MathLib::bigint value) : myId(myId), myArgNr(myArgNr), myArgumentName(myArgumentName), location(location), value(value) {}
7070
std::string myId;
71-
nonneg int myArgNr;
71+
nonneg int myArgNr{};
7272
std::string myArgumentName;
7373
Location location;
74-
MathLib::bigint value;
74+
MathLib::bigint value{};
7575
std::string toString() const;
7676
};
7777

@@ -84,7 +84,7 @@ namespace CTU {
8484
CallBase(const Tokenizer *tokenizer, const Token *callToken);
8585
virtual ~CallBase() {}
8686
std::string callId;
87-
int callArgNr;
87+
int callArgNr{};
8888
std::string callFunctionName;
8989
Location location;
9090
protected:
@@ -119,7 +119,7 @@ namespace CTU {
119119
bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
120120

121121
std::string myId;
122-
nonneg int myArgNr;
122+
nonneg int myArgNr{};
123123
};
124124

125125
std::list<FunctionCall> functionCalls;

test/testconstructors.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,20 @@ class TestConstructors : public TestFixture {
422422
ASSERT_EQUALS("", errout.str());
423423
}
424424

425-
void simple10() { // ticket #4388
426-
check("class Fred {\n"
425+
void simple10() {
426+
check("class Fred {\n" // ticket #4388
427427
"public:\n"
428428
" Fred() = default;\n"
429429
"private:\n"
430430
" int x;\n"
431431
"};");
432-
ASSERT_EQUALS("", errout.str());
432+
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n", errout.str());
433+
434+
check("struct S {\n" // #9391
435+
" S() = default;\n"
436+
" int i;\n"
437+
"};\n");
438+
ASSERT_EQUALS("[test.cpp:2]: (warning) Member variable 'S::i' is not initialized in the constructor.\n", errout.str());
433439
}
434440

435441
void simple11() { // ticket #4536, #6214

0 commit comments

Comments
 (0)