Fix #9391 False negative: Uninitialized struct member (default constructor) (#4088)

* Fix #9391 False negative: Uninitialized struct member (default constructor)

* Format

* Initialize variables

* Init
This commit is contained in:
chrchr-github 2022-05-09 20:28:21 +02:00 committed by GitHub
parent be6daa94bb
commit 38bdece3fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 11 deletions

View File

@ -206,7 +206,7 @@ void CheckClass::constructors()
std::vector<Usage> usageList = createUsageList(scope);
for (const Function &func : scope->functionList) {
if (!func.hasBody() || !(func.isConstructor() || func.type == Function::eOperatorEqual))
if ((!func.hasBody() && !func.isDefault()) || !(func.isConstructor() || func.type == Function::eOperatorEqual))
continue;
// 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)
void CheckClass::initializeVarList(const Function &func, std::list<const Function *> &callstack, const Scope *scope, std::vector<Usage> &usage)
{
if (!func.functionScope)
throw InternalError(nullptr, "Internal Error: Invalid syntax"); // #5702
return;
bool initList = func.isConstructor();
const Token *ftok = func.arg->link()->next();
int level = 0;

View File

@ -60,18 +60,18 @@ namespace CTU {
Location(const Tokenizer *tokenizer, const Token *tok);
Location(const std::string &fileName, nonneg int lineNumber, nonneg int column) : fileName(fileName), lineNumber(lineNumber), column(column) {}
std::string fileName;
nonneg int lineNumber;
nonneg int column;
nonneg int lineNumber{};
nonneg int column{};
};
struct UnsafeUsage {
UnsafeUsage() = default;
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) {}
std::string myId;
nonneg int myArgNr;
nonneg int myArgNr{};
std::string myArgumentName;
Location location;
MathLib::bigint value;
MathLib::bigint value{};
std::string toString() const;
};
@ -84,7 +84,7 @@ namespace CTU {
CallBase(const Tokenizer *tokenizer, const Token *callToken);
virtual ~CallBase() {}
std::string callId;
int callArgNr;
int callArgNr{};
std::string callFunctionName;
Location location;
protected:
@ -119,7 +119,7 @@ namespace CTU {
bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
std::string myId;
nonneg int myArgNr;
nonneg int myArgNr{};
};
std::list<FunctionCall> functionCalls;

View File

@ -422,14 +422,20 @@ private:
ASSERT_EQUALS("", errout.str());
}
void simple10() { // ticket #4388
check("class Fred {\n"
void simple10() {
check("class Fred {\n" // ticket #4388
"public:\n"
" Fred() = default;\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n", errout.str());
check("struct S {\n" // #9391
" S() = default;\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Member variable 'S::i' is not initialized in the constructor.\n", errout.str());
}
void simple11() { // ticket #4536, #6214