2008-12-18 22:28:57 +01:00
/*
2009-01-21 21:04:20 +01:00
* Cppcheck - A tool for static C / C + + code analysis
2022-02-05 11:45:17 +01:00
* Copyright ( C ) 2007 - 2022 Cppcheck team .
2008-12-18 22:28:57 +01:00
*
* 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
2009-09-27 17:08:31 +02:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
2017-05-27 04:33:47 +02:00
2009-10-25 12:49:06 +01:00
# include "checkclass.h"
2022-01-27 19:03:20 +01:00
# include "errortypes.h"
# include "standards.h"
2017-05-27 04:33:47 +02:00
# include "settings.h"
2008-12-18 22:28:57 +01:00
# include "testsuite.h"
2017-05-27 04:33:47 +02:00
# include "tokenize.h"
2008-12-18 22:28:57 +01:00
2022-01-27 19:03:20 +01:00
# include <iosfwd>
# include <list>
# include <string>
2008-12-18 22:28:57 +01:00
2011-10-13 20:53:06 +02:00
class TestConstructors : public TestFixture {
2008-12-18 22:28:57 +01:00
public :
2021-08-07 20:51:18 +02:00
TestConstructors ( ) : TestFixture ( " TestConstructors " ) { }
2008-12-18 22:28:57 +01:00
private :
2015-10-07 18:33:57 +02:00
Settings settings ;
2008-12-18 22:28:57 +01:00
2021-11-29 07:34:39 +01:00
# define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_ ( const char * file , int line , const char code [ ] , bool inconclusive = false ) {
2008-12-18 22:28:57 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
2021-02-24 22:00:06 +01:00
settings . certainty . setEnabled ( Certainty : : inconclusive , inconclusive ) ;
2010-12-01 18:00:55 +01:00
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
2021-11-29 07:34:39 +01:00
ASSERT_LOC ( tokenizer . tokenize ( istr , " test.cpp " ) , file , line ) ;
2010-12-01 18:00:55 +01:00
// Check class constructors..
2009-03-20 17:30:24 +01:00
CheckClass checkClass ( & tokenizer , & settings , this ) ;
2008-12-20 09:48:52 +01:00
checkClass . constructors ( ) ;
2008-12-18 22:28:57 +01:00
}
2021-11-29 07:34:39 +01:00
void check_ ( const char * file , int line , const char code [ ] , const Settings & s ) {
2021-06-13 18:56:04 +02:00
// Clear the error buffer..
errout . str ( " " ) ;
// Tokenize..
Tokenizer tokenizer ( & s , this ) ;
std : : istringstream istr ( code ) ;
2021-11-29 07:34:39 +01:00
ASSERT_LOC ( tokenizer . tokenize ( istr , " test.cpp " ) , file , line ) ;
2021-06-13 18:56:04 +02:00
// Check class constructors..
CheckClass checkClass ( & tokenizer , & s , this ) ;
checkClass . constructors ( ) ;
}
2022-02-10 23:02:24 +01:00
void run ( ) override {
2021-02-24 22:00:06 +01:00
settings . severity . enable ( Severity : : style ) ;
settings . severity . enable ( Severity : : warning ) ;
2015-10-07 18:33:57 +02:00
2009-01-05 16:49:57 +01:00
TEST_CASE ( simple1 ) ;
TEST_CASE ( simple2 ) ;
TEST_CASE ( simple3 ) ;
TEST_CASE ( simple4 ) ;
2011-02-10 13:37:55 +01:00
TEST_CASE ( simple5 ) ; // ticket #2560
2012-09-22 16:49:28 +02:00
TEST_CASE ( simple6 ) ; // ticket #4085 - uninstantiated template class
2013-02-01 06:31:02 +01:00
TEST_CASE ( simple7 ) ; // ticket #4531
2013-02-08 06:55:45 +01:00
TEST_CASE ( simple8 ) ;
2013-02-13 06:00:04 +01:00
TEST_CASE ( simple9 ) ; // ticket #4574
2013-02-15 06:44:07 +01:00
TEST_CASE ( simple10 ) ; // ticket #4388
2014-10-12 15:06:07 +02:00
TEST_CASE ( simple11 ) ; // ticket #4536, #6214
2013-02-27 06:59:04 +01:00
TEST_CASE ( simple12 ) ; // ticket #4620
2014-03-24 06:15:51 +01:00
TEST_CASE ( simple13 ) ; // #5498 - no constructor, c++11 assignments
2015-04-06 19:47:21 +02:00
TEST_CASE ( simple14 ) ; // #6253 template base
2021-07-01 21:09:06 +02:00
TEST_CASE ( simple15 ) ; // #8942 multiple arguments, decltype
2022-02-27 19:15:19 +01:00
TEST_CASE ( simple16 ) ; // copy members with c++11 init
TEST_CASE ( simple17 ) ; // #10360
2009-01-05 16:49:57 +01:00
2014-08-05 12:19:52 +02:00
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
2020-05-17 19:12:16 +02:00
TEST_CASE ( noConstructor10 ) ; // ticket #6614
2016-05-25 19:05:16 +02:00
TEST_CASE ( noConstructor11 ) ; // ticket #3552
2019-02-02 18:24:48 +01:00
TEST_CASE ( noConstructor12 ) ; // #8951 - member initialization
2022-01-06 15:21:05 +01:00
TEST_CASE ( noConstructor13 ) ; // #9998
2022-03-03 13:56:24 +01:00
TEST_CASE ( noConstructor14 ) ; // #10770
2022-03-07 08:39:19 +01:00
TEST_CASE ( noConstructor15 ) ; // #5499
2014-08-05 12:19:52 +02:00
TEST_CASE ( forwardDeclaration ) ; // ticket #4290/#3190
2009-01-05 16:49:57 +01:00
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 ) ;
2010-11-16 07:30:55 +01:00
TEST_CASE ( initvar_operator_eq4 ) ; // ticket #2204
2012-09-22 08:50:36 +02:00
TEST_CASE ( initvar_operator_eq5 ) ; // ticket #4119
2018-06-27 14:02:57 +02:00
TEST_CASE ( initvar_operator_eq6 ) ;
2022-03-02 07:46:23 +01:00
TEST_CASE ( initvar_operator_eq7 ) ;
2009-01-05 16:49:57 +01:00
TEST_CASE ( initvar_same_classname ) ; // BUG 2208157
TEST_CASE ( initvar_chained_assign ) ; // BUG 2270433
TEST_CASE ( initvar_2constructors ) ; // BUG 2270353
2009-03-12 21:33:10 +01:00
TEST_CASE ( initvar_constvar ) ;
2009-05-17 19:27:16 +02:00
TEST_CASE ( initvar_staticvar ) ;
2021-09-05 16:35:08 +02:00
TEST_CASE ( initvar_brace_init ) ;
2011-03-28 00:37:31 +02:00
TEST_CASE ( initvar_union ) ;
2013-01-01 09:53:40 +01:00
TEST_CASE ( initvar_delegate ) ; // ticket #4302
2019-01-10 20:13:37 +01:00
TEST_CASE ( initvar_delegate2 ) ;
2022-02-16 21:30:19 +01:00
TEST_CASE ( initvar_derived_class ) ;
2009-01-05 16:49:57 +01:00
TEST_CASE ( initvar_private_constructor ) ; // BUG 2354171 - private constructor
2010-07-14 19:00:52 +02:00
TEST_CASE ( initvar_copy_constructor ) ; // ticket #1611
2010-07-15 10:16:16 +02:00
TEST_CASE ( initvar_nested_constructor ) ; // ticket #1375
2011-01-22 17:43:36 +01:00
TEST_CASE ( initvar_nocopy1 ) ; // ticket #2474
TEST_CASE ( initvar_nocopy2 ) ; // ticket #2484
2012-09-20 19:16:26 +02:00
TEST_CASE ( initvar_nocopy3 ) ; // ticket #3611
2022-01-28 15:05:13 +01:00
TEST_CASE ( initvar_nocopy4 ) ; // ticket #9247
2013-07-08 06:38:33 +02:00
TEST_CASE ( initvar_with_member_function_this ) ; // ticket #4824
2009-01-05 16:49:57 +01:00
TEST_CASE ( initvar_destructor ) ; // No variables need to be initialized in a destructor
2012-12-29 08:30:14 +01:00
TEST_CASE ( initvar_func_ret_func_ptr ) ; // ticket #4449
2009-04-28 20:50:10 +02:00
2015-11-09 21:02:06 +01:00
TEST_CASE ( initvar_alias ) ; // #6921
2016-01-30 20:15:56 +01:00
TEST_CASE ( initvar_templateMember ) ; // #7205
2022-01-24 21:52:00 +01:00
TEST_CASE ( initvar_smartptr ) ; // #10237
2016-01-30 20:15:56 +01:00
2009-04-28 20:50:10 +02:00
TEST_CASE ( operatorEqSTL ) ;
2012-05-15 11:36:27 +02:00
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
2012-11-26 16:34:44 +01:00
TEST_CASE ( uninitVar23 ) ; // ticket #3702
2012-11-30 06:03:58 +01:00
TEST_CASE ( uninitVar24 ) ; // ticket #3190
2013-05-18 18:33:24 +02:00
TEST_CASE ( uninitVar25 ) ; // ticket #4789
2013-06-14 06:32:26 +02:00
TEST_CASE ( uninitVar26 ) ;
2013-12-15 14:07:29 +01:00
TEST_CASE ( uninitVar27 ) ; // ticket #5170 - rtl::math::setNan(&d)
2014-11-17 16:04:44 +01:00
TEST_CASE ( uninitVar28 ) ; // ticket #6258
2014-11-20 06:18:29 +01:00
TEST_CASE ( uninitVar29 ) ;
2015-01-12 06:11:22 +01:00
TEST_CASE ( uninitVar30 ) ; // ticket #6417
2017-11-11 21:28:24 +01:00
TEST_CASE ( uninitVar31 ) ; // ticket #8271
2018-11-06 06:44:08 +01:00
TEST_CASE ( uninitVar32 ) ; // ticket #8835
2021-05-24 19:32:15 +02:00
TEST_CASE ( uninitVar33 ) ; // ticket #10295
2022-03-16 20:35:55 +01:00
TEST_CASE ( uninitVar34 ) ; // ticket #10841
2018-05-30 15:40:16 +02:00
TEST_CASE ( uninitVarEnum1 ) ;
TEST_CASE ( uninitVarEnum2 ) ; // ticket #8146
2012-05-15 11:36:27 +02:00
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 ) ;
2015-08-29 13:11:00 +02:00
TEST_CASE ( uninitVarArray9 ) ; // ticket #6957, #6959
2012-05-15 11:36:27 +02:00
TEST_CASE ( uninitVarArray2D ) ;
TEST_CASE ( uninitVarArray3D ) ;
2012-12-27 17:21:30 +01:00
TEST_CASE ( uninitVarCpp11Init1 ) ;
TEST_CASE ( uninitVarCpp11Init2 ) ;
2012-05-15 11:36:27 +02:00
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
2021-06-13 18:56:04 +02:00
TEST_CASE ( uninitVarInheritClassInit ) ; // Borland C++: if class inherits from TObject, all variables are initialized
2012-05-15 11:36:27 +02:00
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 ) ;
2012-09-23 18:29:05 +02:00
TEST_CASE ( uninitFunction5 ) ;
2012-05-15 11:36:27 +02:00
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 ) ;
2014-08-05 12:19:52 +02:00
TEST_CASE ( constructors_crash1 ) ; // ticket #5641
2015-07-22 13:48:30 +02:00
TEST_CASE ( classWithOperatorInName ) ; // ticket #2827
2017-04-09 16:01:07 +02:00
TEST_CASE ( templateConstructor ) ; // ticket #7942
2018-01-24 09:51:22 +01:00
TEST_CASE ( typedefArray ) ; // ticket #5766
2017-05-28 16:00:06 +02:00
TEST_CASE ( uninitAssignmentWithOperator ) ; // ticket #7429
TEST_CASE ( uninitCompoundAssignment ) ; // ticket #7429
TEST_CASE ( uninitComparisonAssignment ) ; // ticket #7429
2019-05-17 20:24:41 +02:00
TEST_CASE ( uninitTemplate1 ) ; // ticket #7372
2021-02-13 13:41:12 +01:00
TEST_CASE ( unknownTemplateType ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simple1 ( ) {
2010-01-02 17:29:55 +01:00
check ( " class Fred \n "
" { \n "
" public: \n "
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-05 16:49:57 +01:00
check ( " class Fred \n "
" { \n "
2009-12-19 17:58:52 +01:00
" private: \n "
2009-01-05 16:49:57 +01:00
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2022-02-01 17:24:26 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (style) The class 'Fred' does not declare a constructor although it has private member variables which likely require initialization. \n " , errout . str ( ) ) ;
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" private: \n "
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2022-02-01 17:24:26 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (style) The struct 'Fred' does not declare a constructor although it has private member variables which likely require initialization. \n " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simple2 ( ) {
2010-01-02 17:29:55 +01:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() : i(0) { } \n "
2013-04-10 21:57:22 +02:00
" Fred(Fred const & other) : i(other.i) {} \n "
" Fred(Fred && other) : i(other.i) {} \n "
2010-01-02 17:29:55 +01:00
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { i = 0; } \n "
2013-04-10 21:57:22 +02:00
" Fred(Fred const & other) {i=other.i} \n "
" Fred(Fred && other) {i=other.i} \n "
2010-01-02 17:29:55 +01:00
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-05 16:49:57 +01:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { } \n "
2013-04-10 21:57:22 +02:00
" Fred(Fred const & other) {} \n "
" Fred(Fred && other) {} \n "
2009-01-05 16:49:57 +01:00
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2013-04-10 21:57:22 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:5]: (warning) Member variable 'Fred::i' is not initialized in the copy constructor. \n "
" [test.cpp:6]: (warning) Member variable 'Fred::i' is not initialized in the move constructor. \n " , errout . str ( ) ) ;
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" Fred() { } \n "
2013-04-10 21:57:22 +02:00
" Fred(Fred const & other) {} \n "
" Fred(Fred && other) {} \n "
2010-01-02 17:29:55 +01:00
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2013-04-10 21:57:22 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:4]: (warning) Member variable 'Fred::i' is not initialized in the copy constructor. \n "
" [test.cpp:5]: (warning) Member variable 'Fred::i' is not initialized in the move constructor. \n " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simple3 ( ) {
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" Fred(); \n "
" int i; \n "
" }; \n "
" Fred::Fred() :i(0) \n "
2013-03-20 15:36:16 +01:00
" { } " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Fred \n "
" { \n "
" Fred(); \n "
" int i; \n "
" }; \n "
" Fred::Fred() \n "
2013-03-20 15:36:16 +01:00
" { i = 0; } " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Fred \n "
" { \n "
" Fred(); \n "
" int i; \n "
" }; \n "
" Fred::Fred() \n "
2013-03-20 15:36:16 +01:00
" { } " ) ;
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simple4 ( ) {
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" Fred(); \n "
2011-10-28 22:06:57 +02:00
" explicit Fred(int _i); \n "
2013-04-10 21:57:22 +02:00
" Fred(Fred const & other); \n "
2010-01-02 17:29:55 +01:00
" int i; \n "
" }; \n "
" Fred::Fred() \n "
" { } \n "
" Fred::Fred(int _i) \n "
" { \n "
" i = _i; \n "
2013-03-09 09:20:48 +01:00
" } \n " , true ) ;
2013-04-10 21:57:22 +02:00
ASSERT_EQUALS ( " [test.cpp:8]: (warning, inconclusive) Member variable 'Fred::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void simple5 ( ) { // ticket #2560
2011-02-10 13:37:55 +01:00
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 ( ) ) ;
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
void simple6 ( ) { // ticket #4085 - uninstantiated template class
2012-09-22 16:49:28 +02:00
check ( " template <class T> struct A { \n "
" A<T>() { x = 0; } \n "
2013-01-04 10:35:24 +01:00
" A<T>(const T & t) { x = t.x; } \n "
2012-09-22 16:49:28 +02:00
" private: \n "
" int x; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-04 10:35:24 +01:00
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 ( ) ) ;
2020-11-22 18:17:40 +01:00
2020-11-23 18:29:08 +01:00
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]: (warning) Member variable 'A::y' is not initialized in the constructor. \n "
" [test.cpp:3]: (warning) Member variable 'A::y' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2012-09-22 16:49:28 +02:00
}
2014-11-20 14:20:09 +01:00
void simple7 ( ) { // ticket #4531
2013-02-01 06:31:02 +01:00
check ( " class Fred; \n "
" struct Fred { \n "
" int x; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simple8 ( ) {
2013-02-08 06:55:45 +01:00
check ( " struct Fred { int x; }; \n "
" class Barney { Fred fred; }; \n "
2013-03-20 15:36:16 +01:00
" class Wilma { struct Betty { int x; } betty; }; " ) ;
2022-02-01 17:24:26 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The class 'Barney' does not declare a constructor although it has private member variables which likely require initialization. \n "
" [test.cpp:3]: (style) The class 'Wilma' does not declare a constructor although it has private member variables which likely require initialization. \n " , errout . str ( ) ) ;
2013-02-08 06:55:45 +01:00
}
2014-11-20 14:20:09 +01:00
void simple9 ( ) { // ticket #4574
2013-02-13 06:00:04 +01:00
check ( " class Unknown::Fred { \n "
" public: \n "
" Fred() : x(0) { } \n "
" private: \n "
" int x; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-10-09 08:17:47 +02:00
void simple10 ( ) { // ticket #4388
2013-02-15 06:44:07 +01:00
check ( " class Fred { \n "
" public: \n "
" Fred() = default; \n "
" private: \n "
" int x; \n "
" }; " ) ;
2019-10-08 19:33:32 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-02-15 06:44:07 +01:00
}
2014-11-20 14:20:09 +01:00
void simple11 ( ) { // ticket #4536, #6214
2013-02-18 06:33:53 +01:00
check ( " class Fred { \n "
" public: \n "
" Fred() {} \n "
" private: \n "
" int x = 0; \n "
2014-10-12 15:06:07 +02:00
" int y = f(); \n "
" int z{0}; \n "
2014-10-16 09:11:09 +02:00
" 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 "
" }; " ) ;
2014-10-16 20:46:44 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'Fred::d' is not initialized in the constructor. \n "
" [test.cpp:3]: (warning) Member variable 'Fred::g' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2013-02-18 06:33:53 +01:00
}
2014-11-20 14:20:09 +01:00
void simple12 ( ) { // ticket #4620
2013-02-27 06:59:04 +01:00
check ( " class Fred { \n "
" int x; \n "
" public: \n "
" Fred() { Init(); } \n "
" void Init(int i = 0); \n "
" }; \n "
2013-03-20 15:36:16 +01:00
" void Fred::Init(int i) { x = i; } " ) ;
2013-02-27 06:59:04 +01:00
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 "
2013-03-20 15:36:16 +01:00
" void Fred::Init(int i, int j) { x = i; y = j; } " ) ;
2013-02-27 06:59:04 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void simple13 ( ) { // #5498
2014-03-24 06:15:51 +01:00
check ( " class Fred { \n "
" int x=1; \n "
" int *y=0; \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2014-03-24 06:15:51 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-04-06 19:47:21 +02:00
void simple14 ( ) { // #6253 template base
check ( " class Fred : public Base<A, B> { "
" public: "
" Fred() \n "
" :Base<A, B>(1), \n "
" x(1) \n "
" {} \n "
" private: \n "
" int x; \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2015-04-06 19:47:21 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred : public Base<A, B> { "
" public: "
" Fred() \n "
" :Base<A, B>{1}, \n "
" x{1} \n "
" {} \n "
" private: \n "
" int x; \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2015-04-06 19:47:21 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2022-02-27 19:15:19 +01:00
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 " , /*inconclusive*/ true ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning, inconclusive) Member variable 'S::i' is not assigned in the copy constructor. Should it be copied? \n "
" [test.cpp:5]: (warning) Member variable 'S::i' is not assigned a value in 'S::operator='. \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 " ) ;
2022-03-13 17:33:31 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'S::i' is not initialized in the copy constructor. \n "
2022-02-27 19:15:19 +01:00
" [test.cpp:5]: (warning) Member variable 'S::i' is not assigned a value in 'S::operator='. \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<const Derived&>(Src); \n "
" i = d.i; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-07-01 21:09:06 +02:00
void simple15 ( ) { // #8942
2021-07-01 21:09:32 +02:00
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 ( ) ) ;
2021-07-01 21:09:06 +02:00
}
2014-11-20 14:20:09 +01:00
void noConstructor1 ( ) {
2014-08-05 12:19:52 +02:00
// There are nonstatic member variables - constructor is needed
check ( " class Fred \n "
" { \n "
" int i; \n "
" }; " ) ;
2022-02-01 17:24:26 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (style) The class 'Fred' does not declare a constructor although it has private member variables which likely require initialization. \n " , errout . str ( ) ) ;
2014-08-05 12:19:52 +02:00
}
2014-11-20 14:20:09 +01:00
void noConstructor2 ( ) {
2014-08-05 12:19:52 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" static void foobar(); \n "
" }; \n "
" \n "
" void Fred::foobar() \n "
" { } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor3 ( ) {
2014-08-05 12:19:52 +02:00
check ( " class Fred \n "
" { \n "
" private: \n "
" static int foobar; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor4 ( ) {
2014-08-05 12:19:52 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" int foobar; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor5 ( ) {
2014-08-05 12:19:52 +02:00
check ( " namespace Foo \n "
" { \n "
" int i; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor6 ( ) {
2014-08-05 12:19:52 +02:00
// 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 ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor7 ( ) {
2014-08-05 12:19:52 +02:00
// ticket #4391
check ( " short bar; \n "
2021-02-20 12:58:42 +01:00
" class foo; " ) ;
2014-08-05 12:19:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor8 ( ) {
2014-08-05 12:19:52 +02:00
// ticket #4404
check ( " class LineSegment; \n "
" class PointArray { }; \n "
2021-02-20 12:58:42 +01:00
" void* tech_ = NULL; " ) ;
2014-08-05 12:19:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void noConstructor9 ( ) {
2014-08-05 12:19:52 +02:00
// ticket #4419
check ( " class CGreeting : public CGreetingBase<char> { \n "
" public: \n "
" CGreeting() : CGreetingBase<char>(), MessageSet(false) {} \n "
" private: \n "
" bool MessageSet; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-05-17 19:12:16 +02:00
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 "
2021-08-07 20:51:18 +02:00
" const wxString &title = wxT( " " ), \n "
2020-05-17 19:12:16 +02:00
" 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 "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2020-05-17 19:12:16 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2016-05-25 19:05:16 +02:00
void noConstructor11 ( ) { // #3552
check ( " class Fred { int x; }; \n "
" union U { int y; Fred fred; }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-02-02 18:24:48 +01:00
void noConstructor12 ( ) { // #8951
check ( " class Fred { int x{0}; }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred { int x=0; }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-02-02 18:34:41 +01:00
check ( " class Fred { int x[1]={0}; }; " ) ; // #8850
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred { int x[1]{0}; }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-02-02 18:24:48 +01:00
}
2022-01-06 15:21:05 +01:00
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 ( ) ) ;
}
2022-03-03 13:56:24 +01:00
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 ( ) ) ;
}
2022-03-07 08:39:19 +01:00
void noConstructor15 ( ) { // #5499
check ( " class C { \n "
" private: \n "
" int i1 = 0; \n "
" int i2; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'C::i2' is not initialized. \n " , errout . str ( ) ) ;
check ( " class C { \n "
" private: \n "
" int i1; \n "
" int i2; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) The class 'C' does not declare a constructor although it has private member variables which likely require initialization. \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]: (warning) Member variable 'C::i2' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-08-05 12:19:52 +02:00
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
// ticket #3190 "SymbolDatabase: Parse of sub class constructor fails"
2014-11-20 14:20:09 +01:00
void forwardDeclaration ( ) {
2014-08-05 12:19:52 +02:00
check ( " class foo; \n "
2021-02-20 12:58:42 +01:00
" int bar; " ) ;
2014-08-05 12:19:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class foo; \n "
2021-02-20 12:58:42 +01:00
" class foo; " ) ;
2014-08-05 12:19:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class foo{}; \n "
2021-02-20 12:58:42 +01:00
" class foo; " ) ;
2014-08-05 12:19:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void initvar_with_this ( ) {
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" Fred() \n "
" { this->i = 0; } \n "
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_if ( ) {
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" Fred() \n "
" { \n "
" if (true) \n "
" i = 0; \n "
" else \n "
" i = 1; \n "
" } \n "
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_operator_eq1 ( ) {
2012-05-22 11:27:21 +02:00
// Bug 2190376 and #3820 - False positive, Uninitialized member variable with operator=
2008-12-18 22:28:57 +01:00
2010-01-02 17:29:55 +01:00
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 "
2012-05-22 11:27:21 +02:00
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-01-02 17:29:55 +01:00
2012-05-22 11:27:21 +02:00
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 "
" }; " ) ;
2010-01-02 17:29:55 +01:00
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 "
2012-05-15 11:36:27 +02:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_operator_eq2 ( ) {
2010-01-02 17:29:55 +01:00
check ( " struct Fred \n "
" { \n "
" Fred() { i = 0; } \n "
" void operator=(const Fred &fred) { } \n "
" int i; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-07-09 11:11:05 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_operator_eq3 ( ) {
2010-01-02 17:29:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_operator_eq4 ( ) {
2010-11-16 07:30:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-07-09 11:11:05 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
2010-11-16 07:30:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-07-09 11:11:05 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
2010-11-16 07:30:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-07-09 11:11:05 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
2010-11-16 07:30:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-11-16 07:30:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-22 08:50:36 +02:00
}
2014-11-20 14:20:09 +01:00
void initvar_operator_eq5 ( ) { // #4119 - false positive when using swap to assign variables
2012-09-22 08:50:36 +02:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-09-22 08:50:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-16 07:30:55 +01:00
}
2018-06-27 14:02:57 +02:00
void initvar_operator_eq6 ( ) { // std::vector
check ( " struct Fred { \n "
" uint8_t data; \n "
" Fred & operator=(const Fred &rhs) { \n "
" return *this; \n "
" } \n "
" }; " , true ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Member variable 'Fred::data' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
check ( " struct Fred { \n "
" std::vector<int> ints; \n "
" Fred & operator=(const Fred &rhs) { \n "
" return *this; \n "
" } \n "
" }; " , true ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
check ( " struct Fred { \n "
" Data data; \n "
" Fred & operator=(const Fred &rhs) { \n "
" return *this; \n "
" } \n "
" }; " , true ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Member variable 'Fred::data' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
}
2022-03-02 07:46:23 +01:00
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 " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void initvar_same_classname ( ) {
2008-12-18 22:28:57 +01:00
// Bug 2208157 - False positive: Uninitialized variable, same class name
2009-01-05 16:49:57 +01:00
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 "
2013-03-19 09:18:58 +01:00
" } " ) ;
2009-01-05 16:49:57 +01:00
2009-06-14 07:33:46 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-01-02 17:29:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" } " ) ;
2010-01-02 17:29:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" } " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-09 17:06:07 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-11-09 17:06:07 +01:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2010-11-09 17:06:07 +01:00
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'Fred::a' is not initialized in the constructor. \n "
" [test.cpp:16]: (warning) Member variable 'Fred::b' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_chained_assign ( ) {
2008-12-18 22:28:57 +01:00
// Bug 2270433 - Uninitialized variable false positive on chained assigns
2010-01-02 17:29:55 +01:00
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 "
2013-03-19 09:18:58 +01:00
" } " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_2constructors ( ) {
2010-01-02 17:29:55 +01:00
check ( " struct c \n "
" { \n "
" c(); \n "
2011-10-28 22:06:57 +02:00
" explicit c(bool b); "
2010-01-02 17:29:55 +01:00
" \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 "
2013-03-19 09:18:58 +01:00
" } " ) ;
2010-01-02 17:29:55 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_constvar ( ) {
2010-01-02 17:29:55 +01:00
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 "
" { } " ) ;
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'Fred::s' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2009-03-12 21:33:10 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_staticvar ( ) {
2009-05-17 19:27:16 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { } \n "
" static void *p; \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2009-05-18 22:30:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-05-17 19:27:16 +02:00
}
2021-09-05 16:35:08 +02:00
void initvar_brace_init ( ) { // #10142
check ( " class C \n "
" { \n "
" public: \n "
" C() {} \n "
" \n "
" private: \n "
" std::map<int, double> * values_{}; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void initvar_union ( ) {
2011-03-28 00:37:31 +02:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2011-03-28 00:37:31 +02:00
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 "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2011-11-22 19:26:00 +01:00
TODO_ASSERT_EQUALS ( " [test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor. \n " , " " , errout . str ( ) ) ;
2011-03-28 00:37:31 +02:00
}
2009-05-17 19:27:16 +02:00
2014-11-20 14:20:09 +01:00
void initvar_delegate ( ) {
2013-01-01 09:53:40 +01:00
check ( " class A { \n "
" int number; \n "
" public: \n "
" A(int n) { } \n "
" A() : A(42) {} \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'A::number' is not initialized in the constructor. \n "
" [test.cpp:5]: (warning) Member variable 'A::number' is not initialized in the constructor. \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 "
2013-03-09 09:20:48 +01:00
" }; " , true ) ;
2013-01-01 09:53:40 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'A::number' is not initialized in the constructor. \n "
2013-03-09 09:20:48 +01:00
" [test.cpp:5]: (warning, inconclusive) Member variable 'A::number' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2013-01-01 09:53:40 +01:00
check ( " class A { \n "
" int number; \n "
" public: \n "
" A(int n) : A() { } \n "
" A() { number = 42; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-03-12 05:48:13 +01:00
check ( " class A { \n "
" int number; \n "
" public: \n "
" A(int n) { } \n "
" A() : A{42} {} \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'A::number' is not initialized in the constructor. \n "
" [test.cpp:5]: (warning) Member variable 'A::number' is not initialized in the constructor. \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 "
" }; " , true ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'A::number' is not initialized in the constructor. \n "
" [test.cpp:5]: (warning, inconclusive) Member variable 'A::number' is not initialized in the constructor. \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 ( ) ) ;
2015-05-10 11:27:47 +02:00
// Ticket #6675
check ( " struct Foo { \n "
" Foo(); \n "
" Foo(int foo); \n "
" int foo_; \n "
" }; \n "
" Foo::Foo() : Foo(0) {} \n "
2021-02-20 12:58:42 +01:00
" Foo::Foo(int foo) : foo_(foo) {} " ) ;
2015-05-10 11:27:47 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-09-02 22:54:23 +02:00
// 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 ( ) ) ;
2018-05-20 22:58:05 +02:00
// 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 ( ) ) ;
2018-05-22 06:42:37 +02:00
// 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 ( ) ) ;
2013-01-01 09:53:40 +01:00
}
2019-01-10 20:13:37 +01:00
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 ( ) ) ;
}
2013-01-01 09:53:40 +01:00
2021-02-01 18:58:51 +01:00
void initvar_derived_class ( ) {
2022-02-16 21:30:19 +01:00
check ( " class Base { \n " // #10161
2021-02-01 18:58:51 +01:00
" public: \n "
" virtual void foo() = 0; \n "
" int x; \n " // <- uninitialized
" }; \n "
" \n "
" class Derived: public Base { \n "
" public: \n "
" Derived() {} \n "
" void foo() override; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:9]: (warning) Member variable 'Base::x' is not initialized in the constructor. Maybe it should be initialized directly in the class Base? \n " , errout . str ( ) ) ;
2022-02-16 21:30:19 +01:00
check ( " struct A { \n " // #3462
" char ca; \n "
" A& operator=(const A& a) { \n "
" ca = a.ca; \n "
" return *this; \n "
" } \n "
" }; \n "
" struct B : public A { \n "
" char cb; \n "
" B& operator=(const B& b) { \n "
" A::operator=(b); \n "
" return *this; \n "
" } \n "
" }; \n " ) ;
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Member variable 'B::cb' is not assigned a value in 'B::operator='. \n " , errout . str ( ) ) ;
check ( " struct A { \n "
" char ca; \n "
" A& operator=(const A& a) { \n "
" return *this; \n "
" } \n "
" }; \n "
" struct B : public A { \n "
" char cb; \n "
" B& operator=(const B& b) { \n "
" A::operator=(b); \n "
" return *this; \n "
" } \n "
" }; \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'A::ca' is not assigned a value in 'A::operator='. \n "
" [test.cpp:9]: (warning) Member variable 'B::cb' is not assigned a value in 'B::operator='. \n "
" [test.cpp:9]: (warning) Member variable 'B::ca' is not assigned a value in 'B::operator='. \n " ,
errout . str ( ) ) ;
2021-02-01 18:58:51 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_private_constructor ( ) {
2018-04-02 15:26:15 +02:00
settings . standards . cpp = Standards : : CPP11 ;
check ( " class Fred \n "
" { \n "
" private: \n "
" int var; \n "
" Fred(); \n "
" }; \n "
" Fred::Fred() \n "
" { } " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor. \n " , errout . str ( ) ) ;
settings . standards . cpp = Standards : : CPP03 ;
2009-01-05 16:49:57 +01:00
check ( " class Fred \n "
" { \n "
" private: \n "
" int var; \n "
" Fred(); \n "
" }; \n "
" Fred::Fred() \n "
" { } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2008-12-18 22:28:57 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_copy_constructor ( ) { // ticket #1611
2010-07-14 19:00:52 +02:00
check ( " class Fred \n "
" { \n "
" private: \n "
" std::string var; \n "
" public: \n "
" Fred() { }; \n "
" Fred(const Fred &) { }; \n "
" }; " ) ;
2012-09-20 19:16:26 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred \n "
" { \n "
" private: \n "
" std::string var; \n "
" public: \n "
" Fred() { }; \n "
" Fred(const Fred &) { }; \n "
" }; " , true ) ;
2020-08-05 13:57:40 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning, inconclusive) Member variable 'Fred::var' is not assigned in the copy constructor. Should it be copied? \n " , errout . str ( ) ) ;
2012-09-20 19:16:26 +02:00
check ( " class Fred \n "
" { \n "
" private: \n "
" std::string var; \n "
" public: \n "
" Fred(); \n "
" Fred(const Fred &); \n "
" }; \n "
" Fred::Fred() { }; \n "
" Fred::Fred(const Fred &) { }; \n " , true ) ;
2020-08-05 13:57:40 +02:00
ASSERT_EQUALS ( " [test.cpp:10]: (warning, inconclusive) Member variable 'Fred::var' is not assigned in the copy constructor. Should it be copied? \n " , errout . str ( ) ) ;
2022-03-13 17:33:31 +01:00
check ( " class Baz {}; \n " // #8496
" class Bar { \n "
" public: \n "
" explicit Bar(Baz* pBaz = NULL) : i(0) {} \n "
" Bar(const Bar& bar) {} \n "
" int i; \n "
" }; \n " , true ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Bar::i' is not initialized in the copy constructor. \n " , errout . str ( ) ) ;
2010-07-14 19:00:52 +02:00
}
2014-11-20 14:20:09 +01:00
void initvar_nested_constructor ( ) { // ticket #1375
2010-07-15 10:16:16 +02:00
check ( " class A { \n "
" public: \n "
" A(); \n "
" struct B { \n "
2011-10-28 22:06:57 +02:00
" explicit B(int x); \n "
2010-07-15 10:16:16 +02:00
" struct C { \n "
2011-10-28 22:06:57 +02:00
" explicit C(int y); \n "
2010-07-15 10:16:16 +02:00
" struct D { \n "
" int d; \n "
2011-10-28 22:06:57 +02:00
" explicit D(int z); \n "
2010-07-15 10:16:16 +02:00
" }; \n "
" int c; \n "
" }; \n "
" int b; \n "
" }; \n "
" private: \n "
" int a; \n "
" B b; \n "
" }; \n "
" A::A(){} \n "
2010-12-02 07:35:01 +01:00
" A::B::B(int x){} \n "
" A::B::C::C(int y){} \n "
2013-03-20 15:36:16 +01:00
" A::B::C::D::D(int z){} " ) ;
2012-09-22 08:16:16 +02:00
// Note that the example code is not compilable. The A constructor must
// explicitly initialize A::b. A warning for A::b is not necessary.
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:20]: (warning) Member variable 'A::a' is not initialized in the constructor. \n "
" [test.cpp:21]: (warning) Member variable 'B::b' is not initialized in the constructor. \n "
" [test.cpp:22]: (warning) Member variable 'C::c' is not initialized in the constructor. \n "
" [test.cpp:23]: (warning) Member variable 'D::d' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2010-07-18 20:43:51 +02:00
check ( " class A { \n "
" public: \n "
" A(); \n "
" struct B { \n "
2011-10-28 22:06:57 +02:00
" explicit B(int x); \n "
2010-07-18 20:43:51 +02:00
" struct C { \n "
2011-10-28 22:06:57 +02:00
" explicit C(int y); \n "
2010-07-18 20:43:51 +02:00
" struct D { \n "
" D(const D &); \n "
" int d; \n "
" }; \n "
" int c; \n "
" }; \n "
" int b; \n "
" }; \n "
" private: \n "
" int a; \n "
" B b; \n "
" }; \n "
" A::A(){} \n "
2010-12-02 07:35:01 +01:00
" A::B::B(int x){} \n "
" A::B::C::C(int y){} \n "
2013-03-20 15:36:16 +01:00
" A::B::C::D::D(const A::B::C::D & d){} " ) ;
2012-09-22 08:16:16 +02:00
// Note that the example code is not compilable. The A constructor must
// explicitly initialize A::b. A warning for A::b is not necessary.
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:20]: (warning) Member variable 'A::a' is not initialized in the constructor. \n "
" [test.cpp:21]: (warning) Member variable 'B::b' is not initialized in the constructor. \n "
" [test.cpp:22]: (warning) Member variable 'C::c' is not initialized in the constructor. \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:23]: (warning) Member variable 'D::d' is not initialized in the copy constructor. \n " , errout . str ( ) ) ;
2010-07-18 20:43:51 +02:00
check ( " class A { \n "
" public: \n "
" A(); \n "
" struct B { \n "
2011-10-28 22:06:57 +02:00
" explicit B(int x); \n "
2010-07-18 20:43:51 +02:00
" struct C { \n "
2011-10-28 22:06:57 +02:00
" explicit C(int y); \n "
2010-07-18 20:43:51 +02:00
" struct D { \n "
2010-12-02 07:35:01 +01:00
" struct E { int e; }; \n "
2010-07-26 16:46:37 +02:00
" struct E d; \n "
2011-10-28 22:06:57 +02:00
" explicit D(const E &); \n "
2010-07-18 20:43:51 +02:00
" }; \n "
" int c; \n "
" }; \n "
" int b; \n "
" }; \n "
" private: \n "
" int a; \n "
" B b; \n "
" }; \n "
" A::A(){} \n "
2010-12-02 07:35:01 +01:00
" A::B::B(int x){} \n "
" A::B::C::C(int y){} \n "
2013-03-20 15:36:16 +01:00
" A::B::C::D::D(const A::B::C::D::E & e){} " ) ;
2012-09-22 08:16:16 +02:00
// Note that the example code is not compilable. The A constructor must
// explicitly initialize A::b. A warning for A::b is not necessary.
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:21]: (warning) Member variable 'A::a' is not initialized in the constructor. \n "
" [test.cpp:22]: (warning) Member variable 'B::b' is not initialized in the constructor. \n "
" [test.cpp:23]: (warning) Member variable 'C::c' is not initialized in the constructor. \n "
" [test.cpp:24]: (warning) Member variable 'D::d' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2010-07-15 10:16:16 +02:00
}
2014-11-20 14:20:09 +01:00
void initvar_nocopy1 ( ) { // ticket #2474
2011-01-18 07:34:11 +01:00
check ( " class B \n "
" { \n "
" B (const B & Var); \n "
2013-04-10 21:57:22 +02:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" A(A &&){} \n "
" const A& operator=(const A&){return *this;} \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class B \n "
" { \n "
" B (B && Var); \n "
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" A(A &&){} \n "
" const A& operator=(const A&){return *this;} \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class B \n "
" { \n "
2011-01-18 07:34:11 +01:00
" B & operator= (const B & Var); \n "
2013-04-10 21:57:22 +02:00
" public: \n "
" B (); \n "
2011-01-18 07:34:11 +01:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
2013-04-10 21:57:22 +02:00
" A(A &&){} \n "
2011-01-18 07:34:11 +01:00
" const A& operator=(const A&){return *this;} \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2011-01-18 07:34:11 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class B \n "
" { \n "
" public: \n "
" B (const B & Var); \n "
2013-04-10 21:57:22 +02:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" A(A &&){} \n "
" const A& operator=(const A&){return *this;} \n "
" }; " ) ;
2018-06-27 14:02:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-10 21:57:22 +02:00
2018-06-29 22:54:12 +02:00
check ( " class A : public std::vector<int> \n "
" { \n "
" public: \n "
" A(const A &a); \n "
" }; \n "
" class B \n "
" { \n "
" A a; \n "
" public: \n "
" B(){} \n "
" B(const B&){} \n "
" B(B &&){} \n "
" const B& operator=(const B&){return *this;} \n "
" }; " , true ) ;
2020-08-05 13:57:40 +02:00
ASSERT_EQUALS ( " [test.cpp:11]: (warning, inconclusive) Member variable 'B::a' is not assigned in the copy constructor. Should it be copied? \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:12]: (warning, inconclusive) Member variable 'B::a' is not assigned in the move constructor. Should it be moved? \n "
2018-06-29 22:54:12 +02:00
" [test.cpp:13]: (warning, inconclusive) Member variable 'B::a' is not assigned a value in 'B::operator='. \n " ,
errout . str ( ) ) ;
2013-04-10 21:57:22 +02:00
check ( " class B \n "
" { \n "
" public: \n "
" B (B && Var); \n "
2018-06-27 14:02:57 +02:00
" int data; \n "
2013-04-10 21:57:22 +02:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" A(A &&){} \n "
" const A& operator=(const A&){return *this;} \n "
" }; " ) ;
2022-03-13 17:33:31 +01:00
ASSERT_EQUALS ( " [test.cpp:13]: (warning) Member variable 'A::m_SemVar' is not initialized in the move constructor. \n " , errout . str ( ) ) ;
2013-04-10 21:57:22 +02:00
check ( " class B \n "
" { \n "
" public: \n "
" B (); \n "
2011-01-18 07:34:11 +01:00
" B & operator= (const B & Var); \n "
2018-06-27 14:02:57 +02:00
" int data; \n "
2011-01-18 07:34:11 +01:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
2013-04-10 21:57:22 +02:00
" A(A &&){} \n "
2011-01-18 07:34:11 +01:00
" const A& operator=(const A&){return *this;} \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2018-06-27 14:02:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-18 07:34:11 +01:00
check ( " class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" const A& operator=(const A&){return *this;} \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-09-20 19:16:26 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-22 17:43:36 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_nocopy2 ( ) { // ticket #2484
2011-01-22 17:43:36 +01:00
check ( " class B \n "
" { \n "
" B (B & Var); \n "
" B & operator= (const B & Var); \n "
2018-06-27 14:02:57 +02:00
" int data; \n "
2011-01-22 17:43:36 +01:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" const A& operator=(const A&){return *this;} \n "
2018-06-27 14:02:57 +02:00
" }; " , true ) ;
2011-01-22 17:43:36 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class B \n "
" { \n "
" public: \n "
" B (B & Var); \n "
" B & operator= (const B & Var); \n "
2018-06-27 14:02:57 +02:00
" int data; \n "
2011-01-22 17:43:36 +01:00
" }; \n "
" class A \n "
" { \n "
" B m_SemVar; \n "
" public: \n "
" A(){} \n "
" A(const A&){} \n "
" const A& operator=(const A&){return *this;} \n "
2018-06-27 14:02:57 +02:00
" }; " , true ) ;
2011-04-27 02:37:16 +02:00
ASSERT_EQUALS ( " [test.cpp:12]: (warning) Member variable 'A::m_SemVar' is not initialized in the constructor. \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:13]: (warning) Member variable 'A::m_SemVar' is not initialized in the copy constructor. \n "
2018-06-27 14:02:57 +02:00
" [test.cpp:14]: (warning) Member variable 'A::m_SemVar' is not assigned a value in 'A::operator='. \n " , errout . str ( ) ) ;
2011-01-18 07:34:11 +01:00
}
2014-11-20 14:20:09 +01:00
void initvar_nocopy3 ( ) { // #3611 - unknown type is non-copyable
2012-09-20 16:47:01 +02:00
check ( " struct A { \n "
" B b; \n "
" A() {} \n "
" A(const A& rhs) {} \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-20 19:16:26 +02:00
check ( " struct A { \n "
" B b; \n "
" A() {} \n "
" A(const A& rhs) {} \n "
" }; " , true ) ;
2020-08-05 13:57:40 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning, inconclusive) Member variable 'A::b' is not assigned in the copy constructor. Should it be copied? \n " , errout . str ( ) ) ;
2012-09-20 16:47:01 +02:00
}
2022-01-28 15:05:13 +01:00
void initvar_nocopy4 ( ) { // #9247
check ( " struct S { \n "
" S(const S & s); \n "
" void S::Set(const T& val); \n "
" void S::Set(const U& val); \n "
" T t; \n "
" }; \n "
" S::S(const S& s) { \n "
" Set(s.t); \n "
" } \n "
" void S::Set(const T& val) { \n "
" t = val; \n "
" } " , /*inconclusive*/ true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void initvar_with_member_function_this ( ) {
2013-07-08 06:38:33 +02:00
check ( " struct Foo { \n "
" Foo(int m) { this->setMember(m); } \n "
" void setMember(int m) { member = m; } \n "
" int member; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void initvar_destructor ( ) {
2009-01-05 16:49:57 +01:00
check ( " class Fred \n "
" { \n "
" private: \n "
" int var; \n "
" public: \n "
" Fred() : var(0) {} \n "
" ~Fred() {} \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-05 16:49:57 +01:00
}
2012-12-29 08:30:14 +01:00
2014-11-20 14:20:09 +01:00
void initvar_func_ret_func_ptr ( ) { // ticket #4449 (segmentation fault)
2012-12-29 08:30:14 +01:00
check ( " class something { \n "
" int * ( something :: * process()) () { return 0; } \n "
" something() { process(); } \n "
2013-03-19 09:18:58 +01:00
" }; " ) ;
2012-12-29 08:30:14 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-04-28 20:50:10 +02:00
2015-11-09 21:02:06 +01:00
void initvar_alias ( ) { // #6921
check ( " struct S { \n "
" int a; \n "
" S() { \n "
" int& pa = a; \n "
" pa = 4; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct S { \n "
" int a; \n "
" S() { \n "
" int* pa = &a; \n "
" *pa = 4; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct S { \n "
" int a[2]; \n "
" S() { \n "
" int* pa = a; \n "
" for (int i = 0; i < 2; i++) \n "
" *pa++ = i; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct S { \n "
" int* a[2]; \n "
" S() { \n "
" int* pa = a[1]; \n "
" *pa = 0; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'S::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct S { \n "
" int a; \n "
" S() { \n "
" int pa = a; \n "
" pa = 4; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'S::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2016-01-30 20:15:56 +01:00
void initvar_templateMember ( ) {
check ( " template<int n_> \n "
" struct Wrapper { \n "
" static void foo(int * x) { \n "
" for (int i(0); i <= n_; ++i) \n "
" x[i] = 5; \n "
" } \n "
" }; \n "
" class A { \n "
" public: \n "
" static constexpr int dim = 5; \n "
" int x[dim + 1]; \n "
" A() { \n "
" Wrapper<dim>::foo(x); \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2022-01-24 21:52:00 +01:00
void initvar_smartptr ( ) { // #10237
Settings s ;
s . libraries . emplace_back ( " std " ) ;
check ( " struct S { \n "
" explicit S(const std::shared_ptr<S>& sp) { \n "
" set(*sp); \n "
" } \n "
" double get() const { \n "
" return d; \n "
" } \n "
" void set(const S& rhs) { \n "
" d = rhs.get(); \n "
" } \n "
" double d; \n "
" }; " , s ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-02-16 07:02:26 +01:00
check ( " struct S { \n " // #8485
" explicit S(const T& rhs) { set(*rhs); } \n "
" void set(const S& v) { \n "
" d = v.d; \n "
" } \n "
" double d; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-01-24 21:52:00 +01:00
}
2014-11-20 14:20:09 +01:00
void operatorEqSTL ( ) {
2009-04-28 20:50:10 +02:00
check ( " class Fred \n "
" { \n "
" private: \n "
" std::vector<int> ints; \n "
" public: \n "
" Fred(); \n "
" void operator=(const Fred &f); \n "
" }; \n "
" \n "
" Fred::Fred() \n "
" { } \n "
" \n "
" void Fred::operator=(const Fred &f) \n "
2009-05-01 12:01:55 +02:00
" { } " , true ) ;
2012-09-20 19:16:26 +02:00
ASSERT_EQUALS ( " [test.cpp:13]: (warning, inconclusive) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
2022-03-03 09:41:26 +01:00
Settings s ;
s . certainty . setEnabled ( Certainty : : inconclusive , true ) ;
s . severity . enable ( Severity : : style ) ;
s . severity . enable ( Severity : : warning ) ;
LOAD_LIB_2 ( s . library , " std.cfg " ) ;
check ( " struct S { \n "
" S& operator=(const S& s) { return *this; } \n "
" std::mutex m; \n "
" }; \n " , s ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-04-28 20:50:10 +02:00
}
2012-05-15 11:36:27 +02:00
2014-11-20 14:20:09 +01:00
void uninitVar1 ( ) {
2012-05-15 11:36:27 +02:00
check ( " enum ECODES \n "
" { \n "
" CODE_1 = 0, \n "
" CODE_2 = 1 \n "
" }; \n "
" \n "
" class Fred \n "
" { \n "
" public: \n "
" Fred() {} \n "
" \n "
" private: \n "
" ECODES _code; \n "
" }; " ) ;
2015-09-06 18:37:22 +02:00
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
check ( " class A{}; \n "
" \n "
" class B : public A \n "
" { \n "
" public: \n "
" B() {} \n "
" private: \n "
" float f; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'B::f' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " class C \n "
" { \n "
" FILE *fp; \n "
" \n "
" public: \n "
" explicit C(FILE *fp); \n "
" }; \n "
" \n "
" C::C(FILE *fp) { \n "
" C::fp = fp; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar2 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" public: \n "
" John() { (*this).i = 0; } \n "
" private: \n "
" int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar3 ( ) {
2012-05-15 11:36:27 +02:00
// No FP when struct has constructor
check ( " class Foo \n "
" { \n "
" public: \n "
" Foo() { } \n "
" private: \n "
" struct Bar { \n "
" Bar(); \n "
" }; \n "
" Bar bars[2]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Using struct that doesn't have constructor
check ( " class Foo \n "
" { \n "
" public: \n "
" Foo() { } \n "
" private: \n "
" struct Bar { \n "
" int x; \n "
" }; \n "
" Bar bars[2]; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Foo::bars' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar4 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" public: \n "
" Foo() { bar.x = 0; } \n "
" private: \n "
" struct Bar { \n "
" int x; \n "
" }; \n "
" struct Bar bar; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar5 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" public: \n "
" Foo() { } \n "
" Foo &operator=(const Foo &) \n "
" { return *this; } \n "
" static int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar6 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo : public Bar \n "
" { \n "
" public: \n "
" explicit Foo(int i) : Bar(mi=i) { } \n "
" private: \n "
" int mi; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-08-05 16:11:42 +02:00
check ( " class Foo : public Bar \n "
" { \n "
" public: \n "
" explicit Foo(int i) : Bar{mi=i} { } \n "
" private: \n "
" int mi; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVar7 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo { \n "
" int a; \n "
" public: \n "
" Foo() : a(0) {} \n "
" Foo& operator=(const Foo&); \n "
" void Swap(Foo& rhs); \n "
" }; \n "
" \n "
" void Foo::Swap(Foo& rhs) { \n "
" std::swap(a,rhs.a); \n "
" } \n "
" \n "
" Foo& Foo::operator=(const Foo& rhs) { \n "
" Foo copy(rhs); \n "
" copy.Swap(*this); \n "
" return *this; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar8 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo { \n "
" int a; \n "
" public: \n "
" Foo() : a(0) {} \n "
" Foo& operator=(const Foo&); \n "
" }; \n "
" \n "
" Foo& Foo::operator=(const Foo& rhs) { \n "
" if (&rhs != this) \n "
" { \n "
" } \n "
" return *this; \n "
" } " ) ;
2012-07-09 11:11:05 +02:00
ASSERT_EQUALS ( " [test.cpp:8]: (warning) Member variable 'Foo::a' is not assigned a value in 'Foo::operator='. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVar9 ( ) { // ticket #1730
2012-05-15 11:36:27 +02:00
check ( " class Prefs { \n "
" private: \n "
" int xasd; \n "
" public: \n "
" explicit Prefs(wxSize size); \n "
" }; \n "
" Prefs::Prefs(wxSize size) \n "
" { \n "
" SetMinSize( wxSize( 48,48 ) ); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'Prefs::xasd' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar10 ( ) { // ticket #1993
2012-05-15 11:36:27 +02:00
check ( " class A { \n "
" public: \n "
" A(); \n "
" private: \n "
" int var1; \n "
" int var2; \n "
" }; \n "
" A::A() : var1(0) { } " ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (warning) Member variable 'A::var2' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar11 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class A { \n "
" public: \n "
" explicit A(int a = 0); \n "
" private: \n "
" int var; \n "
" }; \n "
" A::A(int a) { } " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'A::var' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar12 ( ) { // ticket #2078
2012-05-15 11:36:27 +02:00
check ( " class Point \n "
" { \n "
" public: \n "
" Point() \n "
" { \n "
" Point(0, 0); \n "
" } \n "
" Point(int x, int y) \n "
" : x(x), y(y) \n "
" {} \n "
" int x, y; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Point::x' is not initialized in the constructor. \n "
" [test.cpp:4]: (warning) Member variable 'Point::y' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar13 ( ) { // ticket #1195
2012-05-15 11:36:27 +02:00
check ( " class A { \n "
" private: \n "
" std::vector<int> *ints; \n "
" public: \n "
" A() \n "
" {} \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'A::ints' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar14 ( ) { // ticket #2149
2012-05-15 11:36:27 +02:00
// no namespace
check ( " class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" Foo::Foo() \n "
" { \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (warning) Member variable 'Foo::mMember' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// single namespace
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" Foo::Foo() \n "
" { \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Member variable 'Foo::mMember' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// constructor outside namespace
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" Foo::Foo() \n "
" { \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// constructor outside namespace
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" Output::Foo::Foo() \n "
" { \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:11]: (warning) Member variable 'Foo::mMember' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2021-11-20 08:52:27 +01:00
// constructor outside namespace with using, #4792
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" using namespace Output; "
" Foo::Foo() \n "
" { \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:11]: (warning) Member variable 'Foo::mMember' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
// constructor in separate namespace
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" namespace Output \n "
" { \n "
" Foo::Foo() \n "
" { \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:13]: (warning) Member variable 'Foo::mMember' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// constructor in different separate namespace
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" namespace Input \n "
" { \n "
" Foo::Foo() \n "
" { \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// constructor in different separate namespace (won't compile)
check ( " namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" namespace Input \n "
" { \n "
" Output::Foo::Foo() \n "
" { \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// constructor in nested separate namespace
check ( " namespace A \n "
" { \n "
" namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" namespace Output \n "
" { \n "
" Foo::Foo() \n "
" { \n "
" } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:15]: (warning) Member variable 'Foo::mMember' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// constructor in nested different separate namespace
check ( " namespace A \n "
" { \n "
" namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" namespace Input \n "
" { \n "
" Foo::Foo() \n "
" { \n "
" } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// constructor in nested different separate namespace
check ( " namespace A \n "
" { \n "
" namespace Output \n "
" { \n "
" class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" bool mMember; \n "
" }; \n "
" } \n "
" namespace Input \n "
" { \n "
" Output::Foo::Foo() \n "
" { \n "
" } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar15 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" int a; \n "
" public: \n "
" Fred(); \n "
" ~Fred(); \n "
" }; \n "
" Fred::~Fred() \n "
" { \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar16 ( ) {
2012-05-15 11:36:27 +02:00
check ( " struct Foo \n "
" { \n "
" int a; \n "
" void set(int x) { a = x; } \n "
" }; \n "
" class Bar \n "
" { \n "
" Foo foo; \n "
" public: \n "
" Bar() \n "
" { \n "
" foo.set(0); \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo \n "
" { \n "
" int a; \n "
" void set(int x) { a = x; } \n "
" }; \n "
" class Bar \n "
" { \n "
" Foo foo; \n "
" public: \n "
" Bar() \n "
" { \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Member variable 'Bar::foo' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar17 ( ) {
2012-05-15 11:36:27 +02:00
check ( " struct Foo \n "
" { \n "
" int a; \n "
" }; \n "
" class Bar \n "
" { \n "
" Foo foo[10]; \n "
" public: \n "
" Bar() \n "
" { \n "
" foo[0].a = 0; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo \n "
" { \n "
" int a; \n "
" }; \n "
" class Bar \n "
" { \n "
" Foo foo[10]; \n "
" public: \n "
" Bar() \n "
" { \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:9]: (warning) Member variable 'Bar::foo' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar18 ( ) { // ticket #2465
2012-05-15 11:36:27 +02:00
check ( " struct Altren \n "
" { \n "
" explicit Altren(int _a = 0) : value(0) { } \n "
" int value; \n "
" }; \n "
" class A \n "
" { \n "
" public: \n "
" A() { } \n "
" private: \n "
" Altren value; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar19 ( ) { // ticket #2792
2012-05-15 11:36:27 +02:00
check ( " class mystring \n "
" { \n "
" char* m_str; \n "
" int m_len; \n "
" public: \n "
" explicit mystring(const char* str) \n "
" { \n "
" m_len = strlen(str); \n "
" m_str = (char*) malloc(m_len+1); \n "
" memcpy(m_str, str, m_len+1); \n "
" } \n "
" mystring& operator=(const mystring& copy) \n "
" { \n "
" return (*this = copy.m_str); \n "
" } \n "
" ~mystring() \n "
" { \n "
" free(m_str); \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar20 ( ) { // ticket #2867
2012-05-15 11:36:27 +02:00
check ( " Object::MemFunc() { \n "
" class LocalClass { \n "
" public: \n "
" LocalClass() : dataLength_(0) {} \n "
" std::streamsize dataLength_; \n "
" double bitsInData_; \n "
" } obj; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'LocalClass::bitsInData_' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2019-05-17 20:24:41 +02:00
check ( " struct copy_protected; \n "
" Object::MemFunc() { \n "
2012-05-15 11:36:27 +02:00
" class LocalClass : public copy_protected { \n "
" public: \n "
" LocalClass() : copy_protected(1), dataLength_(0) {} \n "
" std::streamsize dataLength_; \n "
" double bitsInData_; \n "
" } obj; \n "
" }; " ) ;
2019-05-17 20:24:41 +02:00
ASSERT_EQUALS (
" [test.cpp:5]: (warning) Member variable 'LocalClass::bitsInData_' is not initialized in the constructor. \n " ,
errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
2019-05-17 20:24:41 +02:00
check ( " struct copy_protected; \n "
" Object::MemFunc() { \n "
2012-05-15 11:36:27 +02:00
" class LocalClass : ::copy_protected { \n "
" public: \n "
" LocalClass() : copy_protected(1), dataLength_(0) {} \n "
" std::streamsize dataLength_; \n "
" double bitsInData_; \n "
" } obj; \n "
" }; " ) ;
2019-05-17 20:24:41 +02:00
ASSERT_EQUALS (
" [test.cpp:5]: (warning) Member variable 'LocalClass::bitsInData_' is not initialized in the constructor. \n " ,
errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVar21 ( ) { // ticket #2947
2012-05-15 11:36:27 +02:00
check ( " class Fred { \n "
" private: \n "
" int a[23]; \n "
" public: \n "
2012-12-06 19:19:22 +01:00
" Fred(); \n "
2012-05-15 11:36:27 +02:00
" }; \n "
" Fred::Fred() { \n "
" a[x::y] = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar22 ( ) { // ticket #3043
2012-05-15 11:36:27 +02:00
check ( " class Fred { \n "
" public: \n "
" Fred & operator=(const Fred &); \n "
" virtual Fred & clone(const Fred & other); \n "
" int x; \n "
" }; \n "
" Fred & Fred::operator=(const Fred & other) { \n "
" return clone(other); \n "
" } \n "
" Fred & Fred::clone(const Fred & other) { \n "
" x = 0; \n "
" return *this; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred { \n "
" public: \n "
" Fred & operator=(const Fred &); \n "
" virtual Fred & clone(const Fred & other); \n "
" int x; \n "
" }; \n "
" Fred & Fred::operator=(const Fred & other) { \n "
" return clone(other); \n "
" } \n "
" Fred & Fred::clone(const Fred & other) { \n "
" return *this; \n "
" } " ) ;
2012-07-09 11:11:05 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'Fred::x' is not assigned a value in 'Fred::operator='. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVar23 ( ) { // ticket #3702
2012-11-26 16:34:44 +01:00
check ( " class Fred { \n "
" int x; \n "
" public: \n "
" Fred(struct A a, struct B b); \n "
" Fred(C c, struct D d); \n "
" Fred(struct E e, F f); \n "
" Fred(struct G, struct H); \n "
" Fred(I, J); \n "
" }; \n "
" Fred::Fred(A a, B b) { } \n "
" Fred::Fred(struct C c, D d) { } \n "
" Fred::Fred(E e, struct F f) { } \n "
" Fred::Fred(G g, H h) { } \n "
2021-02-20 12:58:42 +01:00
" Fred::Fred(struct I i, struct J j) { } " ) ;
2012-11-26 16:34:44 +01:00
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Member variable 'Fred::x' is not initialized in the constructor. \n "
" [test.cpp:11]: (warning) Member variable 'Fred::x' is not initialized in the constructor. \n "
" [test.cpp:12]: (warning) Member variable 'Fred::x' is not initialized in the constructor. \n "
" [test.cpp:13]: (warning) Member variable 'Fred::x' is not initialized in the constructor. \n "
" [test.cpp:14]: (warning) Member variable 'Fred::x' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar24 ( ) { // ticket #3190
2012-11-30 06:03:58 +01:00
check ( " class Foo; \n "
" class Bar; \n "
" class Sub; \n "
" class Foo { class Sub; }; \n "
" class Bar { class Sub; }; \n "
" class Bar::Sub { \n "
" int b; \n "
" public: \n "
" Sub() { } \n "
" Sub(int); \n "
" }; \n "
" Bar::Sub::Sub(int) { }; \n "
" class ::Foo::Sub { \n "
" int f; \n "
" public: \n "
" ~Sub(); \n "
" Sub(); \n "
" }; \n "
" ::Foo::Sub::~Sub() { } \n "
" ::Foo::Sub::Sub() { } \n "
" class Foo; \n "
" class Bar; \n "
2013-03-09 09:20:48 +01:00
" class Sub; \n " , true ) ;
2012-11-30 06:03:58 +01:00
2013-03-09 09:20:48 +01:00
ASSERT_EQUALS ( " [test.cpp:9]: (warning, inconclusive) Member variable 'Sub::b' is not initialized in the constructor. \n "
2013-03-05 13:33:38 +01:00
" [test.cpp:12]: (warning) Member variable 'Sub::b' is not initialized in the constructor. \n "
" [test.cpp:20]: (warning) Member variable 'Sub::f' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2013-05-18 18:33:24 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVar25 ( ) { // ticket #4789
2013-05-18 18:33:24 +02:00
check ( " struct A { \n "
" int a; \n "
" int b; \n "
" int c; \n "
" A(int x = 0, int y = 0, int z = 0); \n "
" }; \n "
2021-02-20 12:58:42 +01:00
" A::A(int x = 0, int y = 0, int z = 0) { } \n "
2013-05-18 18:33:24 +02:00
" struct B { \n "
" int a; \n "
" int b; \n "
" int c; \n "
" B(int x = 0, int y = 0, int z = 0); \n "
" }; \n "
2021-02-20 12:58:42 +01:00
" B::B(int x, int y, int z) { } \n "
2013-05-18 18:33:24 +02:00
" struct C { \n "
" int a; \n "
" int b; \n "
" int c; \n "
" C(int, int, int); \n "
" }; \n "
2021-02-20 12:58:42 +01:00
" C::C(int x = 0, int y = 0, int z = 0) { } \n "
2013-05-18 18:33:24 +02:00
" struct D { \n "
" int a; \n "
" int b; \n "
" int c; \n "
" D(int, int, int); \n "
" }; \n "
2021-02-20 12:58:42 +01:00
" D::D(int x, int y, int z) { } \n "
2013-05-18 18:33:24 +02:00
" struct E { \n "
" int a; \n "
" int b; \n "
" int c; \n "
" E(int x, int y, int z); \n "
" }; \n "
2021-02-20 12:58:42 +01:00
" E::E(int, int, int) { } \n "
2013-05-18 18:33:24 +02:00
" struct F { \n "
" int a; \n "
" int b; \n "
" int c; \n "
" F(int x = 0, int y = 0, int z = 0); \n "
" }; \n "
2013-05-21 16:59:58 +02:00
" F::F(int, int, int) { } \n " , true ) ;
2013-05-18 18:33:24 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'A::a' is not initialized in the constructor. \n "
" [test.cpp:7]: (warning) Member variable 'A::b' is not initialized in the constructor. \n "
" [test.cpp:7]: (warning) Member variable 'A::c' is not initialized in the constructor. \n "
" [test.cpp:14]: (warning) Member variable 'B::a' is not initialized in the constructor. \n "
" [test.cpp:14]: (warning) Member variable 'B::b' is not initialized in the constructor. \n "
" [test.cpp:14]: (warning) Member variable 'B::c' is not initialized in the constructor. \n "
" [test.cpp:21]: (warning) Member variable 'C::a' is not initialized in the constructor. \n "
" [test.cpp:21]: (warning) Member variable 'C::b' is not initialized in the constructor. \n "
" [test.cpp:21]: (warning) Member variable 'C::c' is not initialized in the constructor. \n "
" [test.cpp:28]: (warning) Member variable 'D::a' is not initialized in the constructor. \n "
" [test.cpp:28]: (warning) Member variable 'D::b' is not initialized in the constructor. \n "
" [test.cpp:28]: (warning) Member variable 'D::c' is not initialized in the constructor. \n "
" [test.cpp:35]: (warning) Member variable 'E::a' is not initialized in the constructor. \n "
" [test.cpp:35]: (warning) Member variable 'E::b' is not initialized in the constructor. \n "
" [test.cpp:35]: (warning) Member variable 'E::c' is not initialized in the constructor. \n "
" [test.cpp:42]: (warning) Member variable 'F::a' is not initialized in the constructor. \n "
" [test.cpp:42]: (warning) Member variable 'F::b' is not initialized in the constructor. \n "
" [test.cpp:42]: (warning) Member variable 'F::c' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2012-11-30 06:03:58 +01:00
}
2014-11-20 14:20:09 +01:00
void uninitVar26 ( ) {
2013-06-14 06:32:26 +02:00
check ( " class A { \n "
" int * v; \n "
" int sz; \n "
" public: \n "
" A(int s) { \n "
" v = new int [sz = s]; \n "
" } \n "
" }; " ) ;
2013-12-15 14:07:29 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar27 ( ) {
2013-12-15 14:07:29 +01:00
check ( " class A { \n "
" double d; \n "
" public: \n "
" A() { \n "
" rtl::math::setNan(&d); \n "
" } \n "
" }; " ) ;
2014-01-01 18:34:39 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class A { \n "
" double d; \n "
" public: \n "
" A() { \n "
" ::rtl::math::setNan(&d); \n "
" } \n "
" }; " ) ;
2013-06-14 06:32:26 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar28 ( ) {
2014-11-17 16:04:44 +01:00
check ( " class Fred { \n "
" int i; \n "
" float f; \n "
" public: \n "
" Fred() { \n "
" foo(1); \n "
" foo(1.0f); \n "
" } \n "
" void foo(int a) { i = a; } \n "
" void foo(float a) { f = a; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVar29 ( ) {
2014-11-20 06:18:29 +01:00
check ( " class A { \n "
" int i; \n "
" public: \n "
" A() { foo(); } \n "
" void foo() const { }; \n "
" void foo() { i = 0; } \n "
" }; \n "
" class B { \n "
" int i; \n "
" public: \n "
" B() { foo(); } \n "
" void foo() { i = 0; } \n "
" void foo() const { } \n "
" }; \n "
" class C { \n "
" int i; \n "
" public: \n "
" C() { foo(); } \n "
" void foo() const { i = 0; } \n "
" void foo() { } \n "
" }; \n "
" class D { \n "
" int i; \n "
" public: \n "
" D() { foo(); } \n "
" void foo() { } \n "
" void foo() const { i = 0; } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:18]: (warning) Member variable 'C::i' is not initialized in the constructor. \n "
" [test.cpp:25]: (warning) Member variable 'D::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2015-01-12 06:11:22 +01:00
void uninitVar30 ( ) { // ticket #6417
check ( " namespace NS { \n "
" class MyClass { \n "
" public: \n "
" MyClass(); \n "
" ~MyClass(); \n "
" private: \n "
" bool SomeVar; \n "
" }; \n "
" } \n "
" using namespace NS; \n "
" MyClass::~MyClass() { } \n "
2021-02-20 12:58:42 +01:00
" MyClass::MyClass() : SomeVar(false) { } " ) ;
2015-01-12 06:11:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-11-11 21:28:24 +01:00
}
void uninitVar31 ( ) { // ticket #8271
check ( " void bar(); \n "
" class MyClass { \n "
" public: \n "
" MyClass(); \n "
" void Restart(); \n "
" protected: \n "
" int m_retCode; \n "
" }; \n "
" MyClass::MyClass() { \n "
" bar(),Restart(); \n "
" } \n "
" void MyClass::Restart() { \n "
" m_retCode = 0; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2017-11-11 21:28:24 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-01-12 06:11:22 +01:00
}
2018-11-06 06:44:08 +01:00
void uninitVar32 ( ) { // ticket #8835
check ( " class Foo { \n "
" friend class Bar; \n "
" int member; \n "
" public: \n "
" Foo() \n "
" { \n "
" if (1) {} \n "
" } \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2018-11-06 06:44:08 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " class Foo { \n "
" friend class Bar; \n "
" int member; \n "
" public: \n "
" Foo() \n "
" { \n "
" while (1) {} \n "
" } \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2018-11-06 06:44:08 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " class Foo { \n "
" friend class Bar; \n "
" int member; \n "
" public: \n "
" Foo() \n "
" { \n "
" for (;;) {} \n "
" } \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2018-11-06 06:44:08 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2021-05-24 19:32:15 +02:00
void uninitVar33 ( ) { // ticket #10295
check ( " namespace app { \n "
" class B { \n "
" public: \n "
" B(void); \n "
" int x; \n "
" }; \n "
" }; \n "
" app::B::B(void){} " ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (warning) Member variable 'B::x' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2022-03-16 20:35:55 +01:00
void uninitVar34 ( ) { // ticket #10841
check ( " struct A { void f() {} }; \n "
" struct B { \n "
" B() { a->f(); } \n "
" A* a; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'B::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray1 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" public: \n "
" John() {} \n "
" \n "
" private: \n "
" char name[255]; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'John::name' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " class John \n "
" { \n "
" public: \n "
" John() {John::name[0] = ' \\ 0';} \n "
" \n "
" private: \n "
" char name[255]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class John \n "
" { \n "
" public: \n "
2012-11-29 21:07:52 +01:00
" John() { strcpy(name, \" \" ); } \n "
2012-05-15 11:36:27 +02:00
" \n "
" private: \n "
" char name[255]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class John \n "
" { \n "
" public: \n "
" John() { } \n "
" \n "
" double operator[](const unsigned long i); \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class A; \n "
" class John \n "
" { \n "
" public: \n "
" John() { } \n "
" A a[5]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class A; \n "
" class John \n "
" { \n "
" public: \n "
" John() { } \n "
2015-08-25 21:19:19 +02:00
" A (*a)[5]; \n "
2012-05-15 11:36:27 +02:00
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'John::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray2 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" public: \n "
" John() { *name = 0; } \n "
" \n "
" private: \n "
" char name[255]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-29 16:49:20 +02:00
// #5754
check ( " class John \n "
" { \n "
" public: \n "
" John() {*this->name = ' \\ 0';} \n "
" \n "
" private: \n "
" char name[255]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVarArray3 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" private: \n "
" int a[100]; \n "
" int b[100]; \n "
" \n "
" public: \n "
" John() \n "
" { \n "
" memset(a,0,sizeof(a)); \n "
" memset(b,0,sizeof(b)); \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray4 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" private: \n "
" int a[100]; \n "
" int b[100]; \n "
" \n "
" public: \n "
" John() \n "
" { \n "
" if (snprintf(a,10, \" a \" )) { } \n "
" if (snprintf(b,10, \" b \" )) { } \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray5 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" private: \n "
" Bar bars[10]; \n "
" public: \n "
" Foo() \n "
" { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray6 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" public: \n "
" Foo(); \n "
" static const char STR[]; \n "
" }; \n "
" const char Foo::STR[] = \" abc \" ; \n "
" Foo::Foo() { } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray7 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" int array[10]; \n "
" public: \n "
" Foo() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Foo::array' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " class Foo \n "
" { \n "
" int array[10]; \n "
" public: \n "
" Foo() { memset(array, 0, sizeof(array)); } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Foo \n "
" { \n "
" int array[10]; \n "
" public: \n "
" Foo() { ::memset(array, 0, sizeof(array)); } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray8 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo { \n "
" char a[10]; \n "
" public: \n "
" Foo() { ::ZeroMemory(a); } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-08-28 23:06:39 +02:00
void uninitVarArray9 ( ) { // #6957
check ( " class BaseGDL; \n "
" struct IxExprListT { \n "
" private: \n "
" BaseGDL* eArr[3]; \n "
" public: \n "
" IxExprListT() {} \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'IxExprListT::eArr' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2015-08-29 13:11:00 +02:00
check ( " struct sRAIUnitDefBL { \n "
" sRAIUnitDefBL(); \n "
" ~sRAIUnitDefBL(); \n "
" }; \n "
" struct sRAIUnitDef { \n "
" sRAIUnitDef() {} \n "
" sRAIUnitDefBL *List[35]; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'sRAIUnitDef::List' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2015-08-28 23:06:39 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVarArray2D ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" public: \n "
" John() { a[0][0] = 0; } \n "
" \n "
" private: \n "
" char a[2][2]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarArray3D ( ) {
2012-05-15 11:36:27 +02:00
check ( " class John \n "
" { \n "
" private: \n "
" char a[2][2][2]; \n "
" public: \n "
" John() { a[0][0][0] = 0; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarCpp11Init1 ( ) {
2012-07-10 14:47:51 +02:00
check ( " class Foo { \n "
" std::vector<std::string> bar; \n "
" public: \n "
" Foo() \n "
" : bar({ \" a \" , \" b \" }) \n "
" {} \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarCpp11Init2 ( ) {
2012-12-27 17:21:30 +01:00
check ( " class Fred { \n "
" struct Foo { \n "
" int a; \n "
" bool b; \n "
" }; \n "
" Foo f; \n "
" float g; \n "
" public: \n "
" Fred() : f{0, true} { } \n "
2015-12-06 08:14:04 +01:00
" float get() const; \n "
2012-12-27 17:21:30 +01:00
" }; \n "
2013-03-20 15:36:16 +01:00
" float Fred::get() const { return g; } " ) ;
2012-12-27 17:21:30 +01:00
ASSERT_EQUALS ( " [test.cpp:9]: (warning) Member variable 'Fred::g' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarStruct1 ( ) { // ticket #2172
2012-05-15 11:36:27 +02:00
check ( " class A \n "
" { \n "
" private: \n "
" struct B { \n "
" std::string str1; \n "
" std::string str2; \n "
" } \n "
" struct B b; \n "
" public: \n "
" A() { \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class A \n "
" { \n "
" private: \n "
" struct B { \n "
" char *str1; \n "
" char *str2; \n "
" } \n "
" struct B b; \n "
" public: \n "
" A() { \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Member variable 'A::b' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " class A \n "
" { \n "
" private: \n "
" struct B { \n "
" char *str1; \n "
" char *str2; \n "
" B() : str1(NULL), str2(NULL) { } \n "
" } \n "
" struct B b; \n "
" public: \n "
" A() { \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarStruct2 ( ) { // ticket #838
2012-05-15 11:36:27 +02:00
check ( " struct POINT \n "
" { \n "
" int x; \n "
" int y; \n "
" }; \n "
" class Fred \n "
" { \n "
" private: \n "
" POINT p; \n "
" public: \n "
" Fred() \n "
" { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:11]: (warning) Member variable 'Fred::p' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct POINT \n "
" { \n "
" int x; \n "
" int y; \n "
" POINT(); \n "
" }; \n "
" class Fred \n "
" { \n "
" private: \n "
" POINT p; \n "
" public: \n "
" Fred() \n "
" { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct POINT \n "
" { \n "
" int x; \n "
" int y; \n "
" POINT() :x(0), y(0) { } \n "
" }; \n "
" class Fred \n "
" { \n "
" private: \n "
" POINT p; \n "
2014-10-16 21:57:43 +02:00
" public: \n "
" Fred() \n "
" { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// non static data-member initialization
check ( " struct POINT \n "
" { \n "
" int x=0; \n "
" int y=0; \n "
" }; \n "
" class Fred \n "
" { \n "
" private: \n "
" POINT p; \n "
2012-05-15 11:36:27 +02:00
" public: \n "
" Fred() \n "
" { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2022-03-23 07:35:41 +01:00
void uninitVarUnion1 ( ) {
check ( " class Fred \n " // ticket #3196
2012-05-15 11:36:27 +02:00
" { \n "
" private: \n "
" union { int a; int b; }; \n "
" public: \n "
" Fred() \n "
" { a = 0; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-03-23 07:35:41 +01:00
check ( " class Fred { \n "
" private: \n "
" union { int a{}; int b; }; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVarUnion2 ( ) {
2012-05-15 11:36:27 +02:00
// If the "data_type" is 0 it means union member "data" is invalid.
// So it's ok to not initialize "data".
// related forum: http://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=3&p=1806
check ( " union Data { int id; int *ptr; }; \n "
" \n "
" class Fred { \n "
" private: \n "
" int data_type; \n "
" Data data; \n "
" public: \n "
" Fred() : data_type(0) \n "
" { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitMissingFuncDef ( ) {
2012-05-15 11:36:27 +02:00
// Unknown member function
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { Init(); } \n "
" private: \n "
" void Init(); "
" int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Unknown non-member function (friend class)
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { Init(); } \n "
" private: \n "
" friend ABC; \n "
" int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Unknown non-member function (is Init a virtual function?)
check ( " class Fred : private ABC \n "
" { \n "
" public: \n "
" Fred() { Init(); } \n "
" private: \n "
" int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// Unknown non-member function
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { Init(); } \n "
" private: \n "
" int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// Unknown non-member function
check ( " class ABC { }; \n "
" class Fred : private ABC \n "
" { \n "
" public: \n "
" Fred() { Init(); } \n "
" private: \n "
" int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2018-05-30 15:40:16 +02:00
void uninitVarEnum1 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" enum abc {a,b,c}; \n "
" Fred() {} \n "
" private: \n "
" unsigned int i; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Member variable 'Fred::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2018-05-30 15:40:16 +02:00
void uninitVarEnum2 ( ) { // ticket #8146
check ( " enum E { E1 }; \n "
" struct X { E e = E1; }; \n "
" struct Y { \n "
" Y() {} \n "
" X x; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarStream ( ) {
2012-07-07 20:21:08 +02:00
check ( " class Foo \n "
2012-05-15 11:36:27 +02:00
" { \n "
" private: \n "
" int foo; \n "
" public: \n "
" explicit Foo(std::istream &in) \n "
" { \n "
" if(!(in >> foo)) \n "
" throw 0; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarTypedef ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" public: \n "
" typedef int * pointer; \n "
" Foo() : a(0) {} \n "
" pointer a; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitVarMemset ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" public: \n "
" int * pointer; \n "
" Foo() { memset(this, 0, sizeof(*this)); } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Foo \n "
" { \n "
" public: \n "
" int * pointer; \n "
" Foo() { ::memset(this, 0, sizeof(*this)); } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-10-01 13:46:58 +02:00
// Ticket #7068
check ( " struct Foo { \n "
" int * p; \n "
" char c; \n "
" Foo() { memset(p, 0, sizeof(int)); } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Foo::c' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct Foo { \n "
" int i; \n "
" char c; \n "
" Foo() { memset(&i, 0, sizeof(int)); } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Foo::c' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct Foo { int f; }; \n "
" struct Bar { int b; }; \n "
" struct FooBar { \n "
" FooBar() { \n "
" memset(&foo, 0, sizeof(foo)); \n "
" } \n "
" Foo foo; \n "
" Bar bar; \n "
" }; \n "
" int main() { \n "
" FooBar foobar; \n "
" return foobar.foo.f + foobar.bar.b; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct Foo { int f; }; \n "
" struct Bar { int b; }; \n "
" struct FooBar { \n "
" FooBar() { \n "
" memset(&this->foo, 0, sizeof(this->foo)); \n "
" } \n "
" Foo foo; \n "
" Bar bar; \n "
" }; \n "
" int main() { \n "
" FooBar foobar; \n "
" return foobar.foo.f + foobar.bar.b; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2016-10-16 22:21:33 +02:00
// #7755
check ( " struct A { \n "
" A() { \n "
" memset(this->data, 0, 42); \n "
" } \n "
" char data[42]; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void privateCtor1 ( ) {
2018-04-02 15:26:15 +02:00
settings . standards . cpp = Standards : : CPP03 ;
2012-05-15 11:36:27 +02:00
check ( " class Foo { \n "
" int foo; \n "
" Foo() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-02 15:26:15 +02:00
settings . standards . cpp = Standards : : CPP11 ;
check ( " class Foo { \n "
" int foo; \n "
" Foo() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void privateCtor2 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Foo \n "
" { \n "
" private: \n "
" int foo; \n "
" Foo() { } \n "
" public: \n "
" explicit Foo(int _i) { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'Foo::foo' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void function ( ) {
2012-05-15 11:36:27 +02:00
check ( " class A \n "
" { \n "
" public: \n "
" A(); \n "
" int* f(int*); \n "
" }; \n "
" \n "
" A::A() \n "
" { \n "
" } \n "
" \n "
" int* A::f(int* p) \n "
" { \n "
" return p; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// Borland C++: No FP for published pointers - they are automatically initialized
2014-11-20 14:20:09 +01:00
void uninitVarPublished ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" __published: \n "
" int *i; \n "
" public: \n "
" Fred() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-06-13 18:56:04 +02:00
void uninitVarInheritClassInit ( ) {
Settings s ;
s . libraries . emplace_back ( " vcl " ) ;
check ( " class Fred: public TObject \n "
" { \n "
" public: \n "
" Fred() { } \n "
" private: \n "
" int x; \n "
" }; " , s ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitOperator ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { } \n "
" int *operator [] (int index) { return 0; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitFunction1 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { init(*this); } \n "
" \n "
" static void init(Fred &f) \n "
" { f.d = 0; } \n "
" \n "
" double d; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { init(*this); } \n "
" \n "
" static void init(Fred &f) \n "
" { } \n "
" \n "
" double d; \n "
" }; " ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::d' is not initialized in the constructor. \n " , " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitFunction2 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { if (!init(*this)); } \n "
" \n "
" static bool init(Fred &f) \n "
" { f.d = 0; return true; } \n "
" \n "
" double d; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { if (!init(*this)); } \n "
" \n "
" static bool init(Fred &f) \n "
" { return true; } \n "
" \n "
" double d; \n "
" }; " ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::d' is not initialized in the constructor. \n " , " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitFunction3 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { if (!init()); } \n "
" \n "
" bool init() \n "
" { d = 0; return true; } \n "
" \n "
" double d; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { if (!init()); } \n "
" \n "
" bool init() \n "
" { return true; } \n "
" \n "
" double d; \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::d' is not initialized in the constructor. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitFunction4 ( ) {
2012-05-15 11:36:27 +02:00
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { init(this); } \n "
" \n "
" init(Fred *f) \n "
" { f.d = 0; } \n "
" \n "
" double d; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Fred \n "
" { \n "
" public: \n "
" Fred() { init(this); } \n "
" \n "
" init(Fred *f) \n "
" { } \n "
" \n "
" double d; \n "
" }; " ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'Fred::d' is not initialized in the constructor. \n " , " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitFunction5 ( ) { // #4072 - FP about struct that is initialized in function
2012-09-23 18:29:05 +02:00
check ( " struct Structure { \n "
" int C; \n "
" }; \n "
" \n "
" class A { \n "
" Structure B; \n "
" public: \n "
" A() { Init( B ); }; \n "
" void Init( Structure& S ) { S.C = 0; }; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitSameClassName ( ) {
2012-05-15 11:36:27 +02:00
check ( " class B \n "
" { \n "
" public: \n "
" B(); \n "
" int j; \n "
" }; \n "
" \n "
" class A \n "
" { \n "
" class B \n "
" { \n "
" public: \n "
" B(); \n "
" int i; \n "
" }; \n "
" }; \n "
" \n "
" A::B::B() \n "
" { \n "
" i = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class B \n "
" { \n "
" public: \n "
" B(); \n "
" int j; \n "
" }; \n "
" \n "
" class A \n "
" { \n "
" class B \n "
" { \n "
" public: \n "
" B(); \n "
" int i; \n "
" }; \n "
" }; \n "
" \n "
" B::B() \n "
" { \n "
" } \n "
" \n "
" A::B::B() \n "
" { \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:18]: (warning) Member variable 'B::j' is not initialized in the constructor. \n "
" [test.cpp:22]: (warning) Member variable 'B::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
// Ticket #1700
check ( " namespace n1 \n "
" { \n "
" class Foo { "
" public: \n "
" Foo() : i(0) { } \n "
" private: \n "
" int i; \n "
" }; \n "
" } \n "
" \n "
" namespace n2 \n "
" { \n "
" class Foo { "
" public: \n "
" Foo() { } \n "
" }; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " namespace n1 \n "
" { \n "
" class Foo { \n "
" public: \n "
" Foo(); \n "
" private: \n "
" int i; \n "
" }; \n "
" } \n "
" \n "
" n1::Foo::Foo() \n "
" { } \n "
" \n "
" namespace n2 \n "
" { \n "
" class Foo { \n "
" public: \n "
" Foo() { } \n "
" }; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:11]: (warning) Member variable 'Foo::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " namespace n1 \n "
" { \n "
" class Foo { "
" public: \n "
" Foo(); \n "
" private: \n "
" int i; \n "
" }; \n "
" } \n "
" \n "
" n1::Foo::Foo() : i(0) \n "
" { } \n "
" \n "
" namespace n2 \n "
" { \n "
" class Foo { "
" public: \n "
" Foo() { } \n "
" }; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void uninitFunctionOverload ( ) {
2012-05-15 11:36:27 +02:00
// Ticket #1783 - overloaded "init" functions
check ( " class A \n "
" { \n "
" private: \n "
" int i; \n "
" \n "
" public: \n "
" A() \n "
" { \n "
" init(); \n "
" } \n "
" \n "
" void init() { init(0); } \n "
" \n "
" void init(int value) \n "
" { i = value; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class A \n "
" { \n "
" private: \n "
" int i; \n "
" \n "
" public: \n "
" A() \n "
" { \n "
" init(); \n "
" } \n "
" \n "
" void init() { init(0); } \n "
" \n "
" void init(int value) \n "
" { } \n "
" }; " ) ;
2013-01-01 09:53:40 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Member variable 'A::i' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2022-01-07 12:34:57 +01:00
check ( " class bar { \n " // #9887
" int length; \n "
" bar() { length = 0; } \n "
" }; \n "
" class foo { \n "
" int x; \n "
" foo() { Set(bar()); } \n "
" void Set(int num) { x = 1; } \n "
" void Set(bar num) { x = num.length; } \n "
" }; \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVarOperatorEqual ( ) { // ticket #2415
2012-05-15 11:36:27 +02:00
check ( " struct A { \n "
" int a; \n "
" A() { a=0; } \n "
" A(A const &a) { operator=(a); } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" int a; \n "
" A() { a=0; } \n "
" A(A const &a) { operator=(a); } \n "
" A & operator = (const A & rhs) { \n "
" a = rhs.a; \n "
" return *this; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" int a; \n "
" A() { a=0; } \n "
" A(A const &a) { operator=(a); } \n "
" A & operator = (const A & rhs) { \n "
" return *this; \n "
" } \n "
" }; " ) ;
2022-03-13 17:33:31 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'A::a' is not initialized in the copy constructor. \n "
2012-07-09 11:11:05 +02:00
" [test.cpp:5]: (warning) Member variable 'A::a' is not assigned a value in 'A::operator='. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitVarPointer ( ) { // #3801
2012-05-15 11:36:27 +02:00
check ( " struct A { \n "
" int a; \n "
" }; \n "
" struct B { \n "
" A* a; \n "
" B() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Member variable 'B::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct A; \n "
" struct B { \n "
" A* a; \n "
" B() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct A; \n "
" struct B { \n "
" const A* a; \n "
" B() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct A; \n "
" struct B { \n "
" A* const a; \n "
" B() { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor. \n " , errout . str ( ) ) ;
2022-01-07 12:34:57 +01:00
check ( " class Test { \n " // #8498
" public: \n "
" Test() {} \n "
" std::map<int, int>* pMap = nullptr; \n "
" std::string* pStr = nullptr; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " template <typename U> \n "
" class C1 {}; \n "
" template <typename U, typename V> \n "
" class C2 {}; \n "
" namespace A { \n "
" template <typename U> \n "
" class D1 {}; \n "
" template <typename U, typename V> \n "
" class D2 {}; \n "
" } \n "
" class Test { \n "
" public: \n "
" Test() {} \n "
" C1<int>* c1 = nullptr; \n "
" C2<int, int >* c2 = nullptr; \n "
" A::D1<int>* d1 = nullptr; \n "
" A::D2<int, int >* d2 = nullptr; \n "
" std::map<int, int>* pMap = nullptr; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
}
2014-11-20 14:20:09 +01:00
void uninitConstVar ( ) {
2012-05-15 11:36:27 +02:00
check ( " struct A; \n "
" struct B { \n "
" A* const a; \n "
" B() { } \n "
" B(B& b) { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor. \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:5]: (warning) Member variable 'B::a' is not initialized in the copy constructor. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
check ( " struct A; \n "
" struct B { \n "
" A* const a; \n "
" B& operator=(const B& r) { } \n "
" }; " ) ;
2012-07-16 07:28:59 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // #3804
2012-05-15 11:36:27 +02:00
check ( " struct B { \n "
" const int a; \n "
" B() { } \n "
" B(B& b) { } \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Member variable 'B::a' is not initialized in the constructor. \n "
2022-03-13 17:33:31 +01:00
" [test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the copy constructor. \n " , errout . str ( ) ) ;
2012-05-15 11:36:27 +02:00
check ( " struct B { \n "
" const int a; \n "
" B& operator=(const B& r) { } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-04-04 22:13:51 +02:00
// Ticket #5641 "Regression. Crash for 'C() _STLP_NOTHROW {}'"
2014-11-20 14:20:09 +01:00
void constructors_crash1 ( ) {
2014-04-04 22:13:51 +02:00
check ( " class C { \n "
2014-04-05 08:03:24 +02:00
" public: \n "
" C() _STLP_NOTHROW {} \n "
" C(const C&) _STLP_NOTHROW {} \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2014-04-04 22:13:51 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-07-22 13:48:30 +02:00
void classWithOperatorInName ( ) { // ticket #2827
check ( " class operatorX { \n "
" int mValue; \n "
" public: \n "
" operatorX() : mValue(0) {} \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2017-04-09 16:01:07 +02:00
void templateConstructor ( ) { // ticket #7942
2019-01-06 17:15:57 +01:00
check ( " template <class T> struct Container { \n "
" Container(); \n "
2017-04-09 16:01:07 +02:00
" T* mElements; \n "
" }; \n "
2019-01-06 17:15:57 +01:00
" template <class T> Container<T>::Container() : mElements(nullptr) {} \n "
" Container<int> intContainer; " ) ;
2017-04-09 16:01:07 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2017-05-28 16:00:06 +02:00
2018-01-24 09:51:22 +01:00
void typedefArray ( ) { // ticket #5766
check ( " typedef float rvec[3]; \n "
" class SelectionPosition { \n "
" public: \n "
" SelectionPosition() {} \n "
" const rvec &x() const; \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2017-05-28 16:00:06 +02:00
void uninitAssignmentWithOperator ( ) {
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = false; \n "
" b = b && SetValue(); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " , true ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor. \n " ,
" [test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = false; \n "
" b = true || SetValue(); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " , true ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Member variable 'C::x' is not initialized in the constructor. \n " ,
" [test.cpp:3]: (warning) Member variable 'C::x' is not initialized in the constructor. \n " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = true; \n "
" b = b & SetValue(); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = false; \n "
" b = true | SetValue(); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i * SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i / SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i % SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i + SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i - SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i << SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i >> SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i = i ^ SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void uninitCompoundAssignment ( ) {
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = true; \n "
" b &= SetValue(); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = false; \n "
" b |= SetValue(); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i *= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i /= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i %= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i += SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i -= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i <<= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i >>= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" int i = 0; \n "
" i ^= SetValue(); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void uninitComparisonAssignment ( ) {
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = true; \n "
" b = (true == SetValue()); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = false; \n "
" b |= (true != SetValue()); \n "
" } \n "
" bool SetValue() { \n "
" x = 1; \n "
" return true; \n "
" } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = (0 < SetValue()); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = (0 <= SetValue()); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = (0 > SetValue()); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct C { \n "
" int x; \n "
" C() { \n "
" bool b = (0 >= SetValue()); \n "
" } \n "
" int SetValue() { return x = 1; } \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-05-17 20:24:41 +02:00
void uninitTemplate1 ( ) {
check ( " template <class A, class T> class C; \n "
" template <class A> \n "
" class C<A, void> { \n "
" public: \n "
" C() : b(0) { } \n "
" C(A* a) : b(a) { } \n "
" private: \n "
" A* b; \n "
" }; \n "
" template <class A, class T> \n "
" class C { \n "
" private: \n "
" A* b; \n "
2021-02-20 12:58:42 +01:00
" }; " ) ;
2019-05-17 20:24:41 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " template<class T> class A{}; \n "
" template<class T1, class T2> class B{}; \n "
" template<class T1, class T2> \n "
" class A<B<T1, T2>> { \n "
" public: \n "
" A(); \n "
" bool m_value; \n "
" }; \n "
" template<class T1, class T2> \n "
2021-02-20 12:58:42 +01:00
" A<B<T1, T2>>::A() : m_value(false) {} " ) ;
2019-06-19 22:27:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-05-17 20:24:41 +02:00
}
2021-02-13 13:41:12 +01:00
void unknownTemplateType ( ) {
check ( " template <typename T> class A { \n "
" private: \n "
" T m; \n "
" public: \n "
" A& operator=() { return *this; } \n "
" }; \n "
" A<decltype(SOMETHING)> a; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2008-12-18 22:28:57 +01:00
} ;
2009-01-05 16:49:57 +01:00
REGISTER_TEST ( TestConstructors )