/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "checkclass.h"
#include "errortypes.h"
#include "fixture.h"
#include "helpers.h"
#include "standards.h"
#include "settings.h"
#include
class TestConstructors : public TestFixture {
public:
TestConstructors() : TestFixture("TestConstructors") {}
private:
const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build();
const Settings settings_i = settingsBuilder(settings).certainty(Certainty::inconclusive).build();
struct CheckOptions
{
bool inconclusive = false;
};
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings settings1 = options.inconclusive ? settings_i : settings;
check_(file, line, code, settings1);
}
template
void check_(const char* file, int line, const char (&code)[size], const Settings& settings1) {
SimpleTokenizer tokenizer(settings1, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
// Check class constructors..
CheckClassImpl checkClass(&tokenizer, settings1, *this);
checkClass.constructors();
}
void run() override {
mNewTemplate = true;
TEST_CASE(simple1);
TEST_CASE(simple2);
TEST_CASE(simple3);
TEST_CASE(simple4);
TEST_CASE(simple5); // ticket #2560
TEST_CASE(simple6); // ticket #4085 - uninstantiated template class
TEST_CASE(simple7); // ticket #4531
TEST_CASE(simple8);
TEST_CASE(simple9); // ticket #4574
TEST_CASE(simple10); // ticket #4388
TEST_CASE(simple11); // ticket #4536, #6214
TEST_CASE(simple12); // ticket #4620
TEST_CASE(simple13); // #5498 - no constructor, c++11 assignments
TEST_CASE(simple14); // #6253 template base
TEST_CASE(simple15); // #8942 multiple arguments, decltype
TEST_CASE(simple16); // copy members with c++11 init
TEST_CASE(simple17); // #10360
TEST_CASE(simple18);
TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2);
TEST_CASE(noConstructor3);
TEST_CASE(noConstructor4);
TEST_CASE(noConstructor5);
TEST_CASE(noConstructor6); // ticket #4386
TEST_CASE(noConstructor7); // ticket #4391
TEST_CASE(noConstructor8); // ticket #4404
TEST_CASE(noConstructor9); // ticket #4419
TEST_CASE(noConstructor10); // ticket #6614
TEST_CASE(noConstructor11); // ticket #3552
TEST_CASE(noConstructor12); // #8951 - member initialization
TEST_CASE(noConstructor13); // #9998
TEST_CASE(noConstructor14); // #10770
TEST_CASE(noConstructor15); // #5499
TEST_CASE(noConstructor16);
TEST_CASE(forwardDeclaration); // ticket #4290/#3190
TEST_CASE(initvar_with_this); // BUG 2190300
TEST_CASE(initvar_if); // BUG 2190290
TEST_CASE(initvar_operator_eq1); // BUG 2190376
TEST_CASE(initvar_operator_eq2); // BUG 2190376
TEST_CASE(initvar_operator_eq3);
TEST_CASE(initvar_operator_eq4); // ticket #2204
TEST_CASE(initvar_operator_eq5); // ticket #4119
TEST_CASE(initvar_operator_eq6);
TEST_CASE(initvar_operator_eq7);
TEST_CASE(initvar_operator_eq8);
TEST_CASE(initvar_operator_eq9);
TEST_CASE(initvar_operator_eq10);
TEST_CASE(initvar_operator_eq11);
TEST_CASE(initvar_same_classname); // BUG 2208157
TEST_CASE(initvar_chained_assign); // BUG 2270433
TEST_CASE(initvar_2constructors); // BUG 2270353
TEST_CASE(initvar_constvar);
TEST_CASE(initvar_mutablevar);
TEST_CASE(initvar_staticvar);
TEST_CASE(initvar_brace_init);
TEST_CASE(initvar_union);
TEST_CASE(initvar_delegate); // ticket #4302
TEST_CASE(initvar_delegate2);
TEST_CASE(initvar_derived_class);
TEST_CASE(initvar_derived_pod_struct_with_union); // #11101
TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor
TEST_CASE(initvar_derived_private_constructor);
TEST_CASE(initvar_copy_constructor); // ticket #1611
TEST_CASE(initvar_nested_constructor); // ticket #1375
TEST_CASE(initvar_nocopy1); // ticket #2474
TEST_CASE(initvar_nocopy2); // ticket #2484
TEST_CASE(initvar_nocopy3); // ticket #3611
TEST_CASE(initvar_nocopy4); // ticket #9247
TEST_CASE(initvar_with_member_function_this); // ticket #4824
TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor
TEST_CASE(initvar_func_ret_func_ptr); // ticket #4449
TEST_CASE(initvar_alias); // #6921
TEST_CASE(initvar_templateMember); // #7205
TEST_CASE(initvar_smartptr); // #10237
TEST_CASE(operatorEqSTL);
TEST_CASE(uninitVar1);
TEST_CASE(uninitVar2);
TEST_CASE(uninitVar3);
TEST_CASE(uninitVar4);
TEST_CASE(uninitVar5);
TEST_CASE(uninitVar6);
TEST_CASE(uninitVar7);
TEST_CASE(uninitVar8);
TEST_CASE(uninitVar9); // ticket #1730
TEST_CASE(uninitVar10); // ticket #1993
TEST_CASE(uninitVar11);
TEST_CASE(uninitVar12); // ticket #2078
TEST_CASE(uninitVar13); // ticket #1195
TEST_CASE(uninitVar14); // ticket #2149
TEST_CASE(uninitVar15);
TEST_CASE(uninitVar16);
TEST_CASE(uninitVar17);
TEST_CASE(uninitVar18); // ticket #2465
TEST_CASE(uninitVar19); // ticket #2792
TEST_CASE(uninitVar20); // ticket #2867
TEST_CASE(uninitVar21); // ticket #2947
TEST_CASE(uninitVar22); // ticket #3043
TEST_CASE(uninitVar23); // ticket #3702
TEST_CASE(uninitVar24); // ticket #3190
TEST_CASE(uninitVar25); // ticket #4789
TEST_CASE(uninitVar26);
TEST_CASE(uninitVar27); // ticket #5170 - rtl::math::setNan(&d)
TEST_CASE(uninitVar28); // ticket #6258
TEST_CASE(uninitVar29);
TEST_CASE(uninitVar30); // ticket #6417
TEST_CASE(uninitVar31); // ticket #8271
TEST_CASE(uninitVar32); // ticket #8835
TEST_CASE(uninitVar33); // ticket #10295
TEST_CASE(uninitVar34); // ticket #10841
TEST_CASE(uninitVar35);
TEST_CASE(uninitVar36);
TEST_CASE(uninitVar37);
TEST_CASE(uninitVarEnum1);
TEST_CASE(uninitVarEnum2); // ticket #8146
TEST_CASE(uninitVarStream);
TEST_CASE(uninitVarTypedef);
TEST_CASE(uninitVarMemset);
TEST_CASE(uninitVarArray1);
TEST_CASE(uninitVarArray2);
TEST_CASE(uninitVarArray3);
TEST_CASE(uninitVarArray4);
TEST_CASE(uninitVarArray5);
TEST_CASE(uninitVarArray6);
TEST_CASE(uninitVarArray7);
TEST_CASE(uninitVarArray8);
TEST_CASE(uninitVarArray9); // ticket #6957, #6959
TEST_CASE(uninitVarArray10);
TEST_CASE(uninitVarArray11);
TEST_CASE(uninitVarArray2D);
TEST_CASE(uninitVarArray3D);
TEST_CASE(uninitVarCpp11Init1);
TEST_CASE(uninitVarCpp11Init2);
TEST_CASE(uninitVarStruct1); // ticket #2172
TEST_CASE(uninitVarStruct2); // ticket #838
TEST_CASE(uninitVarUnion1); // ticket #3196
TEST_CASE(uninitVarUnion2);
TEST_CASE(uninitMissingFuncDef); // can't expand function in constructor
TEST_CASE(privateCtor1); // If constructor is private..
TEST_CASE(privateCtor2); // If constructor is private..
TEST_CASE(function); // Function is not variable
TEST_CASE(uninitVarPublished); // Borland C++: Variables in the published section are auto-initialized
TEST_CASE(uninitVarInheritClassInit); // Borland C++: if class inherits from TObject, all variables are initialized
TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]'
TEST_CASE(uninitFunction1); // No FP when initialized in function
TEST_CASE(uninitFunction2); // No FP when initialized in function
TEST_CASE(uninitFunction3); // No FP when initialized in function
TEST_CASE(uninitFunction4);
TEST_CASE(uninitFunction5);
TEST_CASE(uninitSameClassName); // No FP when two classes have the same name
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
TEST_CASE(uninitVarOperatorEqual); // ticket #2415
TEST_CASE(uninitVarPointer); // ticket #3801
TEST_CASE(uninitConstVar);
TEST_CASE(constructors_crash1); // ticket #5641
TEST_CASE(classWithOperatorInName);// ticket #2827
TEST_CASE(templateConstructor); // ticket #7942
TEST_CASE(typedefArray); // ticket #5766
TEST_CASE(uninitAssignmentWithOperator); // ticket #7429
TEST_CASE(uninitCompoundAssignment); // ticket #7429
TEST_CASE(uninitComparisonAssignment); // ticket #7429
TEST_CASE(uninitTemplate1); // ticket #7372
TEST_CASE(unknownTemplateType);
}
void simple1() {
check("class Fred\n"
"{\n"
"public:\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class Fred\n"
"{\n"
"private:\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:1:1]: (style) The class 'Fred' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n", errout_str());
check("struct Fred\n"
"{\n"
"private:\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:1:1]: (style) The struct 'Fred' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n", errout_str());
}
void simple2() {
check("class Fred\n"
"{\n"
"public:\n"
" Fred() : i(0) { }\n"
" Fred(Fred const & other) : i(other.i) {}\n"
" Fred(Fred && other) : i(other.i) {}\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { i = 0; }\n"
" Fred(Fred const & other) {i=other.i}\n"
" Fred(Fred && other) {i=other.i}\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { }\n"
" Fred(Fred const & other) {}\n"
" Fred(Fred && other) {}\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'Fred::i' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:5:5]: (warning) Member variable 'Fred::i' is not initialized in the copy constructor. [uninitMemberVar]\n"
"[test.cpp:6:5]: (warning) Member variable 'Fred::i' is not initialized in the move constructor. [uninitMemberVar]\n", errout_str());
check("struct Fred\n"
"{\n"
" Fred() { }\n"
" Fred(Fred const & other) {}\n"
" Fred(Fred && other) {}\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:3:5]: (warning) Member variable 'Fred::i' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:4:5]: (warning) Member variable 'Fred::i' is not initialized in the copy constructor. [uninitMemberVar]\n"
"[test.cpp:5:5]: (warning) Member variable 'Fred::i' is not initialized in the move constructor. [uninitMemberVar]\n", errout_str());
}
void simple3() {
check("struct Fred\n"
"{\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred() :i(0)\n"
"{ }");
ASSERT_EQUALS("", errout_str());
check("struct Fred\n"
"{\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ i = 0; }");
ASSERT_EQUALS("", errout_str());
check("struct Fred\n"
"{\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("[test.cpp:6:7]: (warning) Member variable 'Fred::i' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void simple4() {
check("struct Fred\n"
"{\n"
" Fred();\n"
" explicit Fred(int _i);\n"
" Fred(Fred const & other);\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ }\n"
"Fred::Fred(int _i)\n"
"{\n"
" i = _i;\n"
"}\n", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:8:7]: (warning, inconclusive) Member variable 'Fred::i' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void simple5() { // ticket #2560
check("namespace Nsp\n"
"{\n"
" class B { };\n"
"}\n"
"class Altren : public Nsp::B\n"
"{\n"
"public:\n"
" Altren () : Nsp::B(), mValue(0)\n"
" {\n"
" }\n"
"private:\n"
" int mValue;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void simple6() { // ticket #4085 - uninstantiated template class
check("template struct A {\n"
" A() { x = 0; }\n"
" A(const T & t) { x = t.x; }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("template struct A {\n"
" A() : x(0) { }\n"
" A(const T & t) : x(t.x) { }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("template struct A {\n"
" A() : x(0) { }\n"
" A(const T & t) : x(t.x) { }\n"
"private:\n"
" int x;\n"
" int y;\n"
"};");
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Member variable 'A::y' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:3:5]: (warning) Member variable 'A::y' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void simple7() { // ticket #4531
check("class Fred;\n"
"struct Fred {\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void simple8() {
check("struct Fred { int x; };\n"
"class Barney { Fred fred; };\n"
"class Wilma { struct Betty { int x; } betty; };");
ASSERT_EQUALS("[test.cpp:2:1]: (style) The class 'Barney' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n"
"[test.cpp:3:1]: (style) The class 'Wilma' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n", errout_str());
}
void simple9() { // ticket #4574
check("class Unknown::Fred {\n"
"public:\n"
" Fred() : x(0) { }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void simple10() {
check("class Fred {\n" // ticket #4388
"public:\n"
" Fred() = default;\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3:5]: (warning) Member variable 'Fred::x' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
check("struct S {\n" // #9391
" S() = default;\n"
" ~S() = default;\n"
" S(const S&) = default;\n"
" S(S&&) = default;\n"
" S& operator=(const S&) = default;\n"
" S& operator=(S&&) = default;\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Member variable 'S::i' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void simple11() { // ticket #4536, #6214
check("class Fred {\n"
"public:\n"
" Fred() {}\n"
"private:\n"
" int x = 0;\n"
" int y = f();\n"
" int z{0};\n"
" int (*pf[2])(){nullptr, nullptr};\n"
" int a[2][3] = {{1,2,3},{4,5,6}};\n"
" int b{1}, c{2};\n"
" int d, e{3};\n"
" int f{4}, g;\n"
"};");
ASSERT_EQUALS("[test.cpp:3:5]: (warning) Member variable 'Fred::d' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:3:5]: (warning) Member variable 'Fred::g' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void simple12() { // ticket #4620
check("class Fred {\n"
" int x;\n"
"public:\n"
" Fred() { Init(); }\n"
" void Init(int i = 0);\n"
"};\n"
"void Fred::Init(int i) { x = i; }");
ASSERT_EQUALS("", errout_str());
check("class Fred {\n"
" int x;\n"
" int y;\n"
"public:\n"
" Fred() { Init(0); }\n"
" void Init(int i, int j = 0);\n"
"};\n"
"void Fred::Init(int i, int j) { x = i; y = j; }");
ASSERT_EQUALS("", errout_str());
}
void simple13() { // #5498
check("class Fred {\n"
" int x=1;\n"
" int *y=0;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void simple14() { // #6253 template base
check("class Fred : public Base {"
"public:"
" Fred()\n"
" :Base(1),\n"
" x(1)\n"
" {}\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class Fred : public Base {"
"public:"
" Fred()\n"
" :Base{1},\n"
" x{1}\n"
" {}\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void simple15() { // #8942
check("class A\n"
"{\n"
"public:\n"
" int member;\n"
"};\n"
"class B\n"
"{\n"
"public:\n"
" B(const decltype(A::member)& x, const decltype(A::member)& y) : x(x), y(y) {}\n"
"private:\n"
" const decltype(A::member)& x;\n"
" const decltype(A::member)& y;\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void simple16() {
check("struct S {\n"
" int i{};\n"
" S() = default;\n"
" S(const S& s) {}\n"
" S& operator=(const S& s) { return *this; }\n"
"};\n", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:4:5]: (warning, inconclusive) Member variable 'S::i' is not assigned in the copy constructor. Should it be copied? [missingMemberCopy]\n"
"[test.cpp:5:8]: (warning) Member variable 'S::i' is not assigned a value in 'S::operator='. [operatorEqVarError]\n",
errout_str());
check("struct S {\n"
" int i;\n"
" S() : i(0) {}\n"
" S(const S& s) {}\n"
" S& operator=(const S& s) { return *this; }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'S::i' is not initialized in the copy constructor. [uninitMemberVar]\n"
"[test.cpp:5:8]: (warning) Member variable 'S::i' is not assigned a value in 'S::operator='. [operatorEqVarError]\n",
errout_str());
}
void simple17() { // #10360
check("class Base {\n"
"public:\n"
" virtual void Copy(const Base& Src) = 0;\n"
"};\n"
"class Derived : public Base {\n"
"public:\n"
" Derived() : i(0) {}\n"
" Derived(const Derived& Src);\n"
" void Copy(const Base& Src) override;\n"
" int i;\n"
"};\n"
"Derived::Derived(const Derived& Src) {\n"
" Copy(Src);\n"
"}\n"
"void Derived::Copy(const Base& Src) {\n"
" auto d = dynamic_cast(Src);\n"
" i = d.i;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void simple18() { // #13302
check("struct S {};\n"
"class C1 { S& r; };\n"
"class C2 { S* p; };\n");
ASSERT_EQUALS("[test.cpp:2:1]: (style) The class 'C1' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n"
"[test.cpp:3:1]: (style) The class 'C2' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n",
errout_str());
}
void noConstructor1() {
// There are nonstatic member variables - constructor is needed
check("class Fred\n"
"{\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:1:1]: (style) The class 'Fred' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n", errout_str());
}
void noConstructor2() {
check("class Fred\n"
"{\n"
"public:\n"
" static void foobar();\n"
"};\n"
"\n"
"void Fred::foobar()\n"
"{ }");
ASSERT_EQUALS("", errout_str());
}
void noConstructor3() {
check("class Fred\n"
"{\n"
"private:\n"
" static int foobar;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void noConstructor4() {
check("class Fred\n"
"{\n"
"public:\n"
" int foobar;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void noConstructor5() {
check("namespace Foo\n"
"{\n"
" int i;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void noConstructor6() {
// ticket #4386
check("class Ccpucycles {\n"
" friend class foo::bar;\n"
" Ccpucycles() :\n"
" m_v(0), m_b(true)\n"
" {}\n"
"private:\n"
" cpucyclesT m_v;\n"
" bool m_b;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void noConstructor7() {
// ticket #4391
check("short bar;\n"
"class foo;");
ASSERT_EQUALS("", errout_str());
}
void noConstructor8() {
// ticket #4404
check("class LineSegment;\n"
"class PointArray { };\n"
"void* tech_ = NULL;");
ASSERT_EQUALS("", errout_str());
}
void noConstructor9() {
// ticket #4419
check("class CGreeting : public CGreetingBase {\n"
"public:\n"
" CGreeting() : CGreetingBase(), MessageSet(false) {}\n"
"private:\n"
" bool MessageSet;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void noConstructor10() {
// ticket #6614
check("class A : public wxDialog\n"
"{\n"
"private:\n"
" DECLARE_EVENT_TABLE()\n"
"public:\n"
" A(wxWindow *parent,\n"
" wxWindowID id = 1,\n"
" const wxString &title = wxT(" "),\n"
" const wxPoint& pos = wxDefaultPosition,\n"
" const wxSize& size = wxDefaultSize,\n"
" long style = wxDIALOG_NO_PARENT | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxCLOSE_BOX);\n"
" virtual ~A();\n"
"private:\n"
" wxTimer *WxTimer1;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void noConstructor11() { // #3552
check("class Fred { int x; };\n"
"union U { int y; Fred fred; };");
ASSERT_EQUALS("", errout_str());
}
void noConstructor12() { // #8951
check("class Fred { int x{0}; };");
ASSERT_EQUALS("", errout_str());
check("class Fred { int x=0; };");
ASSERT_EQUALS("", errout_str());
check("class Fred { int x[1]={0}; };"); // #8850
ASSERT_EQUALS("", errout_str());
check("class Fred { int x[1]{0}; };");
ASSERT_EQUALS("", errout_str());
}
void noConstructor13() { // #9998
check("struct C { int v; };\n"
"struct B { C c[5] = {}; };\n"
"struct A {\n"
" A() {}\n"
" B b;\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void noConstructor14() { // #10770
check("typedef void (*Func)();\n"
"class C {\n"
"public:\n"
" void f();\n"
"private:\n"
" Func fp = nullptr;\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void noConstructor15() { // #5499
check("class C {\n"
"private:\n"
" int i1 = 0;\n"
" int i2;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'C::i2' is not initialized. [uninitMemberVarPrivate]\n", errout_str());
check("class C {\n"
"private:\n"
" int i1;\n"
" int i2;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:1:1]: (style) The class 'C' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]\n", errout_str());
check("class C {\n"
"public:\n"
" C(int i) : i1(i) {}\n"
"private:\n"
" int i1;\n"
" int i2;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3:5]: (warning) Member variable 'C::i2' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void noConstructor16() {
check("struct S {\n" // #14546
" int a = 0, b;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2:16]: (warning) Member variable 'S::b' has no initializer. [uninitMemberVarNoCtor]\n", errout_str());
check("struct S {\n"
" int a, b;\n"
"};\n");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"
" explicit S(int);\n"
" S(const S&);\n"
" int i;\n"
"};\n"
"struct T {\n"
" S s;\n"
" int j{};\n"
"};\n");
ASSERT_EQUALS("", errout_str());
const char code[] = "struct S { int i = 0; };\n" // #14697
"struct T {\n"
" S s;\n"
" int j;\n"
"};\n"
"struct U {\n"
" std::string a;\n"
" int k;\n"
"};\n";
const Settings s = settingsBuilder(settings).cpp(Standards::CPP11).build();
check(code, s);
ASSERT_EQUALS("", errout_str());
check(code);
ASSERT_EQUALS("[test.cpp:4:9]: (warning) Member variable 'T::j' has no initializer. [uninitMemberVarNoCtor]\n"
"[test.cpp:8:9]: (warning) Member variable 'U::k' has no initializer. [uninitMemberVarNoCtor]\n",
errout_str());
}
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
// ticket #3190 "SymbolDatabase: Parse of sub class constructor fails"
void forwardDeclaration() {
check("class foo;\n"
"int bar;");
ASSERT_EQUALS("", errout_str());
check("class foo;\n"
"class foo;");
ASSERT_EQUALS("", errout_str());
check("class foo{};\n"
"class foo;");
ASSERT_EQUALS("", errout_str());
}
void initvar_with_this() {
check("struct Fred\n"
"{\n"
" Fred()\n"
" { this->i = 0; }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_if() {
check("struct Fred\n"
"{\n"
" Fred()\n"
" {\n"
" if (true)\n"
" i = 0;\n"
" else\n"
" i = 1;\n"
" }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq1() {
// Bug 2190376 and #3820 - False positive, Uninitialized member variable with operator=
check("struct Fred\n"
"{\n"
" int i;\n"
"\n"
" Fred()\n"
" { i = 0; }\n"
"\n"
" Fred(const Fred &fred)\n"
" { *this = fred; }\n"
"\n"
" const Fred & operator=(const Fred &fred)\n"
" { i = fred.i; return *this; }\n"
"};");
ASSERT_EQUALS("", errout_str());
check("struct Fred {\n"
" int i;\n"
"\n"
" Fred(const Fred &fred)\n"
" { (*this) = fred; }\n"
"\n"
" const Fred & operator=(const Fred &fred)\n"
" { i = fred.i; return *this; }\n"
"};");
ASSERT_EQUALS("", errout_str());
check("struct A\n"
"{\n"
" A() : i(0), j(0) {}\n"
"\n"
" A &operator=(const int &value)\n"
" {\n"
" i = value;\n"
" return (*this);\n"
" }\n"
"\n"
" int i;\n"
" int j;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq2() {
check("struct Fred\n"
"{\n"
" Fred() { i = 0; }\n"
" void operator=(const Fred &fred) { }\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:4:10]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
}
void initvar_operator_eq3() {
check("struct Fred\n"
"{\n"
" Fred() { Init(); }\n"
" void operator=(const Fred &fred) { Init(); }\n"
"private:\n"
" void Init() { i = 0; }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq4() {
check("class Fred\n"
"{\n"
" int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:6:12]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
check("class Fred\n"
"{\n"
" int * i;\n"
"public:\n"
" Fred() : i(NULL) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:6:12]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
check("class Fred\n"
"{\n"
" const int * i;\n"
"public:\n"
" Fred() : i(NULL) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:6:12]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
check("class Fred\n"
"{\n"
" const int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq5() { // #4119 - false positive when using swap to assign variables
check("class Fred {\n"
" int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" ~Fred() { }\n"
" Fred(const Fred &fred) : i(fred.i) { }\n"
" Fred & operator=(const Fred &rhs) {\n"
" Fred(rhs).swap(*this);\n"
" return *this;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq6() { // std::vector
check("struct Fred {\n"
" uint8_t data;\n"
" Fred & operator=(const Fred &rhs) {\n"
" return *this;\n"
" }\n"
"};",dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:3:12]: (warning, inconclusive) Member variable 'Fred::data' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
check("struct Fred {\n"
" std::vector ints;\n"
" Fred & operator=(const Fred &rhs) {\n"
" return *this;\n"
" }\n"
"};",dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:3:12]: (warning, inconclusive) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
check("struct Fred {\n"
" Data data;\n"
" Fred & operator=(const Fred &rhs) {\n"
" return *this;\n"
" }\n"
"};",dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:3:12]: (warning, inconclusive) Member variable 'Fred::data' is not assigned a value in 'Fred::operator='. [operatorEqVarError]\n", errout_str());
}
void initvar_operator_eq7() {
check("struct B {\n"
" virtual void CopyImpl(const B& Src) = 0;\n"
" void Copy(const B& Src);\n"
"};\n"
"struct D : B {};\n"
"struct DD : D {\n"
" void CopyImpl(const B& Src) override;\n"
" DD& operator=(const DD& Src);\n"
" int i{};\n"
"};\n"
"DD& DD::operator=(const DD& Src) {\n"
" if (this != &Src)\n"
" Copy(Src);\n"
" return *this;\n"
"}\n", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq8() {
check("struct B {\n"
" int b;\n"
"};\n"
"struct D1 : B {\n"
" D1& operator=(const D1& src);\n"
" int d1;\n"
"};\n"
"struct D2 : D1 {\n"
" D2& operator=(const D2& src);\n"
" int d2;\n"
"};\n"
"struct D3 : D2 {\n"
" D3& operator=(const D3& src) {\n"
" D1::operator=(src);\n"
" d3_1 = src.d3_1;\n"
" }\n"
" int d3_1;\n"
" int d3_2;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:13:9]: (warning) Member variable 'D3::d3_2' is not assigned a value in 'D3::operator='. [operatorEqVarError]\n"
"[test.cpp:13:9]: (warning) Member variable 'D3::d2' is not assigned a value in 'D3::operator='. [operatorEqVarError]\n", errout_str());
}
void initvar_operator_eq9() { // ticket #13203
check("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" if (&s != this) {\n"
" *(m_data) = *(s.m_data);\n"
" }\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq10() {
check("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" if (&s != this) {\n"
" (*m_data) = *(s.m_data);\n"
" }\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void initvar_operator_eq11() {
check("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4:8]: (warning) Member variable 'S::m_data' is not assigned a value in 'S::operator='. [operatorEqVarError]\n", errout_str());
}
void initvar_same_classname() {
// Bug 2208157 - False positive: Uninitialized variable, same class name
check("void func1()\n"
"{\n"
" class Fred\n"
" {\n"
" int a;\n"
" Fred() { a = 0; }\n"
" };\n"
"}\n"
"\n"
"void func2()\n"
"{\n"
" class Fred\n"
" {\n"
" int b;\n"
" Fred() { b = 0; }\n"
" };\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void func1()\n"
"{\n"
" struct Fred\n"
" {\n"
" int a;\n"
" Fred() { a = 0; }\n"
" };\n"
"}\n"
"\n"
"void func2()\n"
"{\n"
" class Fred\n"
" {\n"
" int b;\n"
" Fred() { b = 0; }\n"
" };\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void func1()\n"
"{\n"
" struct Fred\n"
" {\n"
" int a;\n"
" Fred() { a = 0; }\n"
" };\n"
"}\n"
"\n"
"void func2()\n"
"{\n"
" struct Fred\n"
" {\n"
" int b;\n"
" Fred() { b = 0; }\n"
" };\n"
"}");
ASSERT_EQUALS("", errout_str());
check("class Foo {\n"
" void func1()\n"
" {\n"
" struct Fred\n"
" {\n"
" int a;\n"
" Fred() { a = 0; }\n"
" };\n"
" }\n"
"\n"
" void func2()\n"
" {\n"
" struct Fred\n"
" {\n"
" int b;\n"
" Fred() { b = 0; }\n"
" };\n"
" }\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class Foo {\n"
" void func1()\n"
" {\n"
" struct Fred\n"
" {\n"
" int a;\n"
" Fred() { }\n"
" };\n"
" }\n"
"\n"
" void func2()\n"
" {\n"
" struct Fred\n"
" {\n"
" int b;\n"
" Fred() { }\n"
" };\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:7:13]: (warning) Member variable 'Fred::a' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:16:13]: (warning) Member variable 'Fred::b' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void initvar_chained_assign() {
// Bug 2270433 - Uninitialized variable false positive on chained assigns
check("struct c\n"
"{\n"
" c();\n"
"\n"
" int m_iMyInt1;\n"
" int m_iMyInt2;\n"
"}\n"
"\n"
"c::c()\n"
"{\n"
" m_iMyInt1 = m_iMyInt2 = 0;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void initvar_2constructors() {
check("struct c\n"
"{\n"
" c();\n"
" explicit c(bool b);"
"\n"
" void InitInt();\n"
"\n"
" int m_iMyInt;\n"
" int m_bMyBool;\n"
"}\n"
"\n"
"c::c()\n"
"{\n"
" m_bMyBool = false;\n"
" InitInt();"
"}\n"
"\n"
"c::c(bool b)\n"
"{\n"
" m_bMyBool = b;\n"
" InitInt();\n"
"}\n"
"\n"
"void c::InitInt()\n"
"{\n"
" m_iMyInt = 0;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void initvar_constvar() {
check("struct Fred\n"
"{\n"
" const char *s;\n"
" Fred();\n"
"};\n"
"Fred::Fred() : s(NULL)\n"
"{ }");
ASSERT_EQUALS("", errout_str());
check("struct Fred\n"
"{\n"
" const char *s;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ s = NULL; }");
ASSERT_EQUALS("", errout_str());
check("struct Fred\n"
"{\n"
" const char *s;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("[test.cpp:6:7]: (warning) Member variable 'Fred::s' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void initvar_mutablevar() {
check("class Foo {\n"
"public:\n"
" Foo() { update(); }\n"
"private:\n"
" void update() const;\n"
" mutable int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_staticvar() {
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { }\n"
" static void *p;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_brace_init() { // #10142
check("class C\n"
"{\n"
"public:\n"
" C() {}\n"
"\n"
"private:\n"
" std::map * values_{};\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_union() {
check("class Fred\n"
"{\n"
" union\n"
" {\n"
" int a;\n"
" char b[4];\n"
" } U;\n"
"public:\n"
" Fred()\n"
" {\n"
" U.a = 0;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class Fred\n"
"{\n"
" union\n"
" {\n"
" int a;\n"
" char b[4];\n"
" } U;\n"
"public:\n"
" Fred()\n"
" {\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:9:5]: (warning) Member variable 'Fred::U' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
void initvar_delegate() {
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) { }\n"
" A() : A(42) {}\n"
"};");
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:5:5]: (warning) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) { number = n; }\n"
" A() : A(42) {}\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) : A() { }\n"
" A() {}\n"
"};", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:5:5]: (warning, inconclusive) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) : A() { }\n"
" A() { number = 42; }\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) { }\n"
" A() : A{42} {}\n"
"};");
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:5:5]: (warning) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) { number = n; }\n"
" A() : A{42} {}\n"
"};");
ASSERT_EQUALS("", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) : A{} { }\n"
" A() {}\n"
"};", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:4:5]: (warning) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:5:5]: (warning, inconclusive) Member variable 'A::number' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
check("class A {\n"
" int number;\n"
"public:\n"
" A(int n) : A{} { }\n"
" A() { number = 42; }\n"
"};");
ASSERT_EQUALS("", errout_str());
// Ticket #6675
check("struct Foo {\n"
" Foo();\n"
" Foo(int foo);\n"
" int foo_;\n"
"};\n"
"Foo::Foo() : Foo(0) {}\n"
"Foo::Foo(int foo) : foo_(foo) {}");
ASSERT_EQUALS("", errout_str());
// Noexcept ctors
check("class A {\n"
"private:\n"
" int _a;\n"
"public:\n"
" A(const int a) noexcept : _a{a} {}\n"
" A() noexcept;\n"
"};\n"
"\n"
"A::A() noexcept: A(0) {}");
ASSERT_EQUALS("", errout_str());
// Ticket #8581
check("class A {\n"
"private:\n"
" int _a;\n"
"public:\n"
" A(int a) : _a(a) {}\n"
" A(float a) : A(int(a)) {}\n"
"};");
ASSERT_EQUALS("", errout_str());
// Ticket #8258
check("struct F{};\n"
"struct Foo {\n"
" Foo(int a, F&& f, int b = 21) : _a(a), _b(b), _f(f) {}\n"
" Foo(int x, const char* value) : Foo(x, F(), 42) {}\n"
" Foo(int x, int* value) : Foo(x, F()) {}\n"
" int _a;\n"
" int _b;\n"
" F _f;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void initvar_delegate2() {
check("class Foo {\n"
"public:\n"
" explicit Foo(const Bar bar);\n"
" Foo(const std::string& id);\n"
" virtual ~RtpSession() { }\n"
"protected:\n"
" bool a;\n"
" uint16_t b;\n"
"};\n"
"\n"
"Foo::Foo(const Bar var)\n"
" : Foo(bar->getId())\n"
"{\n"
"}\n"
"\n"
"Foo::Foo(const std::string& id)\n"
" : a(true)\n"
" , b(0)\n"
"{\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void initvar_derived_class() {
check("class Base {\n" // #10161
"public:\n"
" virtual void foo() = 0;\n"
" int x;\n" //