You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
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());
}
voidsimple2() {
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());
}
voidsimple3() {
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());
ASSERT_EQUALS("[test.cpp:8:7]: (warning, inconclusive) Member variable 'Fred::i' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}
voidsimple5() { // 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());
}
voidsimple6() { // ticket #4085 - uninstantiated template class
check("template <class T> struct A {\n"
" A<T>() { x = 0; }\n"
" A<T>(const T & t) { x = t.x; }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("template <class T> struct A {\n"
" A<T>() : x(0) { }\n"
" A<T>(const T & t) : x(t.x) { }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
check("template <class T> struct A {\n"
" A<T>() : x(0) { }\n"
" A<T>(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());
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());
}
voidsimple9() { // ticket #4574
check("class Unknown::Fred {\n"
"public:\n"
" Fred() : x(0) { }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
voidsimple10() {
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());
}
voidsimple11() { // 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());
}
voidsimple12() { // 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("[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());
}
voidsimple17() { // #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<const Derived&>(Src);\n"
" i = d.i;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
voidsimple18() { // #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());
}
voidnoConstructor1() {
// 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());
}
voidnoConstructor2() {
check("class Fred\n"
"{\n"
"public:\n"
" static void foobar();\n"
"};\n"
"\n"
"void Fred::foobar()\n"
"{ }");
ASSERT_EQUALS("", errout_str());
}
voidnoConstructor3() {
check("class Fred\n"
"{\n"
"private:\n"
" static int foobar;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
voidnoConstructor4() {
check("class Fred\n"
"{\n"
"public:\n"
" int foobar;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
voidnoConstructor5() {
check("namespace Foo\n"
"{\n"
" int i;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
voidnoConstructor6() {
// 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());
}
voidnoConstructor7() {
// ticket #4391
check("short bar;\n"
"class foo;");
ASSERT_EQUALS("", errout_str());
}
voidnoConstructor8() {
// ticket #4404
check("class LineSegment;\n"
"class PointArray { };\n"
"void* tech_ = NULL;");
ASSERT_EQUALS("", errout_str());
}
voidnoConstructor9() {
// ticket #4419
check("class CGreeting : public CGreetingBase<char> {\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());
}
voidnoConstructor16() {
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());
constchar 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"
voidforwardDeclaration() {
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());
}
voidinitvar_with_this() {
check("struct Fred\n"
"{\n"
" Fred()\n"
" { this->i = 0; }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
voidinitvar_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());
}
voidinitvar_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());
}
voidinitvar_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());
}
voidinitvar_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());
}
voidinitvar_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());
}
voidinitvar_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());
}
voidinitvar_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());