forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestconstructors.cpp
More file actions
4436 lines (4057 loc) · 146 KB
/
testconstructors.cpp
File metadata and controls
4436 lines (4057 loc) · 146 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "checkclass.h"
#include "errortypes.h"
#include "standards.h"
#include "settings.h"
#include "fixture.h"
#include "tokenize.h"
#include <list>
#include <sstream> // IWYU pragma: keep
#include <string>
class TestConstructors : public TestFixture {
public:
TestConstructors() : TestFixture("TestConstructors") {}
private:
Settings settings;
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], bool inconclusive = false) {
// Clear the error buffer..
errout.str("");
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
// Check class constructors..
CheckClass checkClass(&tokenizer, &settings, this);
checkClass.constructors();
}
void check_(const char* file, int line, const char code[], const Settings &s) {
// Clear the error buffer..
errout.str("");
// Tokenize..
Tokenizer tokenizer(&s, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
// Check class constructors..
CheckClass checkClass(&tokenizer, &s, this);
checkClass.constructors();
}
void run() override {
settings.severity.enable(Severity::style);
settings.severity.enable(Severity::warning);
TEST_CASE(simple1);
TEST_CASE(simple2);
TEST_CASE(simple3);
TEST_CASE(simple4);
TEST_CASE(simple5); // ticket #2560
TEST_CASE(simple6); // ticket #4085 - uninstantiated template class
TEST_CASE(simple7); // ticket #4531
TEST_CASE(simple8);
TEST_CASE(simple9); // ticket #4574
TEST_CASE(simple10); // ticket #4388
TEST_CASE(simple11); // ticket #4536, #6214
TEST_CASE(simple12); // ticket #4620
TEST_CASE(simple13); // #5498 - no constructor, c++11 assignments
TEST_CASE(simple14); // #6253 template base
TEST_CASE(simple15); // #8942 multiple arguments, decltype
TEST_CASE(simple16); // copy members with c++11 init
TEST_CASE(simple17); // #10360
TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2);
TEST_CASE(noConstructor3);
TEST_CASE(noConstructor4);
TEST_CASE(noConstructor5);
TEST_CASE(noConstructor6); // ticket #4386
TEST_CASE(noConstructor7); // ticket #4391
TEST_CASE(noConstructor8); // ticket #4404
TEST_CASE(noConstructor9); // ticket #4419
TEST_CASE(noConstructor10); // ticket #6614
TEST_CASE(noConstructor11); // ticket #3552
TEST_CASE(noConstructor12); // #8951 - member initialization
TEST_CASE(noConstructor13); // #9998
TEST_CASE(noConstructor14); // #10770
TEST_CASE(noConstructor15); // #5499
TEST_CASE(forwardDeclaration); // ticket #4290/#3190
TEST_CASE(initvar_with_this); // BUG 2190300
TEST_CASE(initvar_if); // BUG 2190290
TEST_CASE(initvar_operator_eq1); // BUG 2190376
TEST_CASE(initvar_operator_eq2); // BUG 2190376
TEST_CASE(initvar_operator_eq3);
TEST_CASE(initvar_operator_eq4); // ticket #2204
TEST_CASE(initvar_operator_eq5); // ticket #4119
TEST_CASE(initvar_operator_eq6);
TEST_CASE(initvar_operator_eq7);
TEST_CASE(initvar_operator_eq8);
TEST_CASE(initvar_same_classname); // BUG 2208157
TEST_CASE(initvar_chained_assign); // BUG 2270433
TEST_CASE(initvar_2constructors); // BUG 2270353
TEST_CASE(initvar_constvar);
TEST_CASE(initvar_staticvar);
TEST_CASE(initvar_brace_init);
TEST_CASE(initvar_union);
TEST_CASE(initvar_delegate); // ticket #4302
TEST_CASE(initvar_delegate2);
TEST_CASE(initvar_derived_class);
TEST_CASE(initvar_derived_pod_struct_with_union); // #11101
TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor
TEST_CASE(initvar_copy_constructor); // ticket #1611
TEST_CASE(initvar_nested_constructor); // ticket #1375
TEST_CASE(initvar_nocopy1); // ticket #2474
TEST_CASE(initvar_nocopy2); // ticket #2484
TEST_CASE(initvar_nocopy3); // ticket #3611
TEST_CASE(initvar_nocopy4); // ticket #9247
TEST_CASE(initvar_with_member_function_this); // ticket #4824
TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor
TEST_CASE(initvar_func_ret_func_ptr); // ticket #4449
TEST_CASE(initvar_alias); // #6921
TEST_CASE(initvar_templateMember); // #7205
TEST_CASE(initvar_smartptr); // #10237
TEST_CASE(operatorEqSTL);
TEST_CASE(uninitVar1);
TEST_CASE(uninitVar2);
TEST_CASE(uninitVar3);
TEST_CASE(uninitVar4);
TEST_CASE(uninitVar5);
TEST_CASE(uninitVar6);
TEST_CASE(uninitVar7);
TEST_CASE(uninitVar8);
TEST_CASE(uninitVar9); // ticket #1730
TEST_CASE(uninitVar10); // ticket #1993
TEST_CASE(uninitVar11);
TEST_CASE(uninitVar12); // ticket #2078
TEST_CASE(uninitVar13); // ticket #1195
TEST_CASE(uninitVar14); // ticket #2149
TEST_CASE(uninitVar15);
TEST_CASE(uninitVar16);
TEST_CASE(uninitVar17);
TEST_CASE(uninitVar18); // ticket #2465
TEST_CASE(uninitVar19); // ticket #2792
TEST_CASE(uninitVar20); // ticket #2867
TEST_CASE(uninitVar21); // ticket #2947
TEST_CASE(uninitVar22); // ticket #3043
TEST_CASE(uninitVar23); // ticket #3702
TEST_CASE(uninitVar24); // ticket #3190
TEST_CASE(uninitVar25); // ticket #4789
TEST_CASE(uninitVar26);
TEST_CASE(uninitVar27); // ticket #5170 - rtl::math::setNan(&d)
TEST_CASE(uninitVar28); // ticket #6258
TEST_CASE(uninitVar29);
TEST_CASE(uninitVar30); // ticket #6417
TEST_CASE(uninitVar31); // ticket #8271
TEST_CASE(uninitVar32); // ticket #8835
TEST_CASE(uninitVar33); // ticket #10295
TEST_CASE(uninitVar34); // ticket #10841
TEST_CASE(uninitVarEnum1);
TEST_CASE(uninitVarEnum2); // ticket #8146
TEST_CASE(uninitVarStream);
TEST_CASE(uninitVarTypedef);
TEST_CASE(uninitVarMemset);
TEST_CASE(uninitVarArray1);
TEST_CASE(uninitVarArray2);
TEST_CASE(uninitVarArray3);
TEST_CASE(uninitVarArray4);
TEST_CASE(uninitVarArray5);
TEST_CASE(uninitVarArray6);
TEST_CASE(uninitVarArray7);
TEST_CASE(uninitVarArray8);
TEST_CASE(uninitVarArray9); // ticket #6957, #6959
TEST_CASE(uninitVarArray2D);
TEST_CASE(uninitVarArray3D);
TEST_CASE(uninitVarCpp11Init1);
TEST_CASE(uninitVarCpp11Init2);
TEST_CASE(uninitVarStruct1); // ticket #2172
TEST_CASE(uninitVarStruct2); // ticket #838
TEST_CASE(uninitVarUnion1); // ticket #3196
TEST_CASE(uninitVarUnion2);
TEST_CASE(uninitMissingFuncDef); // can't expand function in constructor
TEST_CASE(privateCtor1); // If constructor is private..
TEST_CASE(privateCtor2); // If constructor is private..
TEST_CASE(function); // Function is not variable
TEST_CASE(uninitVarPublished); // Borland C++: Variables in the published section are auto-initialized
TEST_CASE(uninitVarInheritClassInit); // Borland C++: if class inherits from TObject, all variables are initialized
TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]'
TEST_CASE(uninitFunction1); // No FP when initialized in function
TEST_CASE(uninitFunction2); // No FP when initialized in function
TEST_CASE(uninitFunction3); // No FP when initialized in function
TEST_CASE(uninitFunction4);
TEST_CASE(uninitFunction5);
TEST_CASE(uninitSameClassName); // No FP when two classes have the same name
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
TEST_CASE(uninitVarOperatorEqual); // ticket #2415
TEST_CASE(uninitVarPointer); // ticket #3801
TEST_CASE(uninitConstVar);
TEST_CASE(constructors_crash1); // ticket #5641
TEST_CASE(classWithOperatorInName);// ticket #2827
TEST_CASE(templateConstructor); // ticket #7942
TEST_CASE(typedefArray); // ticket #5766
TEST_CASE(uninitAssignmentWithOperator); // ticket #7429
TEST_CASE(uninitCompoundAssignment); // ticket #7429
TEST_CASE(uninitComparisonAssignment); // ticket #7429
TEST_CASE(uninitTemplate1); // ticket #7372
TEST_CASE(unknownTemplateType);
}
void simple1() {
check("class Fred\n"
"{\n"
"public:\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
check("class Fred\n"
"{\n"
"private:\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' does not declare a constructor although it has private member variables which likely require initialization.\n", errout.str());
check("struct Fred\n"
"{\n"
"private:\n"
" int i;\n"
"};");
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());
}
void simple2() {
check("class Fred\n"
"{\n"
"public:\n"
" Fred() : i(0) { }\n"
" Fred(Fred const & other) : i(other.i) {}\n"
" Fred(Fred && other) : i(other.i) {}\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { i = 0; }\n"
" Fred(Fred const & other) {i=other.i}\n"
" Fred(Fred && other) {i=other.i}\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { }\n"
" Fred(Fred const & other) {}\n"
" Fred(Fred && other) {}\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n"
"[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());
check("struct Fred\n"
"{\n"
" Fred() { }\n"
" Fred(Fred const & other) {}\n"
" Fred(Fred && other) {}\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n"
"[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());
}
void simple3() {
check("struct Fred\n"
"{\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred() :i(0)\n"
"{ }");
ASSERT_EQUALS("", errout.str());
check("struct Fred\n"
"{\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ i = 0; }");
ASSERT_EQUALS("", errout.str());
check("struct Fred\n"
"{\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
}
void simple4() {
check("struct Fred\n"
"{\n"
" Fred();\n"
" explicit Fred(int _i);\n"
" Fred(Fred const & other);\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ }\n"
"Fred::Fred(int _i)\n"
"{\n"
" i = _i;\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:8]: (warning, inconclusive) Member variable 'Fred::i' is not initialized in the constructor.\n", errout.str());
}
void simple5() { // ticket #2560
check("namespace Nsp\n"
"{\n"
" class B { };\n"
"}\n"
"class Altren : public Nsp::B\n"
"{\n"
"public:\n"
" Altren () : Nsp::B(), mValue(0)\n"
" {\n"
" }\n"
"private:\n"
" int mValue;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void simple6() { // ticket #4085 - uninstantiated template class
check("template <class T> struct A {\n"
" A<T>() { x = 0; }\n"
" A<T>(const T & t) { x = t.x; }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
check("template <class T> struct A {\n"
" A<T>() : x(0) { }\n"
" A<T>(const T & t) : x(t.x) { }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
check("template <class T> struct A {\n"
" A<T>() : x(0) { }\n"
" A<T>(const T & t) : x(t.x) { }\n"
"private:\n"
" int x;\n"
" int y;\n"
"};");
ASSERT_EQUALS("[test.cpp:2]: (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());
}
void simple7() { // ticket #4531
check("class Fred;\n"
"struct Fred {\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void simple8() {
check("struct Fred { int x; };\n"
"class Barney { Fred fred; };\n"
"class Wilma { struct Betty { int x; } betty; };");
ASSERT_EQUALS("[test.cpp:2]: (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());
}
void simple9() { // ticket #4574
check("class Unknown::Fred {\n"
"public:\n"
" Fred() : x(0) { }\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void simple10() {
check("class Fred {\n" // ticket #4388
"public:\n"
" Fred() = default;\n"
"private:\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n", errout.str());
check("struct S {\n" // #9391
" S() = default;\n"
" ~S() = default;\n"
" S(const S&) = default;\n"
" S(S&&) = default;\n"
" S& operator=(const S&) = default;\n"
" S& operator=(S&&) = default;\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Member variable 'S::i' is not initialized in the constructor.\n", errout.str());
}
void simple11() { // ticket #4536, #6214
check("class Fred {\n"
"public:\n"
" Fred() {}\n"
"private:\n"
" int x = 0;\n"
" int y = f();\n"
" int z{0};\n"
" int (*pf[2])(){nullptr, nullptr};\n"
" int a[2][3] = {{1,2,3},{4,5,6}};\n"
" int b{1}, c{2};\n"
" int d, e{3};\n"
" int f{4}, g;\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (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());
}
void simple12() { // ticket #4620
check("class Fred {\n"
" int x;\n"
"public:\n"
" Fred() { Init(); }\n"
" void Init(int i = 0);\n"
"};\n"
"void Fred::Init(int i) { x = i; }");
ASSERT_EQUALS("", errout.str());
check("class Fred {\n"
" int x;\n"
" int y;\n"
"public:\n"
" Fred() { Init(0); }\n"
" void Init(int i, int j = 0);\n"
"};\n"
"void Fred::Init(int i, int j) { x = i; y = j; }");
ASSERT_EQUALS("", errout.str());
}
void simple13() { // #5498
check("class Fred {\n"
" int x=1;\n"
" int *y=0;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void simple14() { // #6253 template base
check("class Fred : public Base<A, B> {"
"public:"
" Fred()\n"
" :Base<A, B>(1),\n"
" x(1)\n"
" {}\n"
"private:\n"
" int x;\n"
"};");
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"
"};");
ASSERT_EQUALS("", errout.str());
}
void simple16() {
check("struct S {\n"
" int i{};\n"
" S() = default;\n"
" S(const S& s) {}\n"
" S& operator=(const S& s) { return *this; }\n"
"};\n", /*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");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'S::i' is not initialized in the copy constructor.\n"
"[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());
}
void simple15() { // #8942
check("class A\n"
"{\n"
"public:\n"
" int member;\n"
"};\n"
"class B\n"
"{\n"
"public:\n"
" B(const decltype(A::member)& x, const decltype(A::member)& y) : x(x), y(y) {}\n"
"private:\n"
" const decltype(A::member)& x;\n"
" const decltype(A::member)& y;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void noConstructor1() {
// There are nonstatic member variables - constructor is needed
check("class Fred\n"
"{\n"
" int i;\n"
"};");
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());
}
void noConstructor2() {
check("class Fred\n"
"{\n"
"public:\n"
" static void foobar();\n"
"};\n"
"\n"
"void Fred::foobar()\n"
"{ }");
ASSERT_EQUALS("", errout.str());
}
void noConstructor3() {
check("class Fred\n"
"{\n"
"private:\n"
" static int foobar;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noConstructor4() {
check("class Fred\n"
"{\n"
"public:\n"
" int foobar;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noConstructor5() {
check("namespace Foo\n"
"{\n"
" int i;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void noConstructor6() {
// ticket #4386
check("class Ccpucycles {\n"
" friend class foo::bar;\n"
" Ccpucycles() :\n"
" m_v(0), m_b(true)\n"
" {}\n"
"private:\n"
" cpucyclesT m_v;\n"
" bool m_b;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noConstructor7() {
// ticket #4391
check("short bar;\n"
"class foo;");
ASSERT_EQUALS("", errout.str());
}
void noConstructor8() {
// ticket #4404
check("class LineSegment;\n"
"class PointArray { };\n"
"void* tech_ = NULL;");
ASSERT_EQUALS("", errout.str());
}
void noConstructor9() {
// ticket #4419
check("class CGreeting : public CGreetingBase<char> {\n"
"public:\n"
" CGreeting() : CGreetingBase<char>(), MessageSet(false) {}\n"
"private:\n"
" bool MessageSet;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noConstructor10() {
// ticket #6614
check("class A : public wxDialog\n"
"{\n"
"private:\n"
" DECLARE_EVENT_TABLE()\n"
"public:\n"
" A(wxWindow *parent,\n"
" wxWindowID id = 1,\n"
" const wxString &title = wxT(" "),\n"
" const wxPoint& pos = wxDefaultPosition,\n"
" const wxSize& size = wxDefaultSize,\n"
" long style = wxDIALOG_NO_PARENT | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxCLOSE_BOX);\n"
" virtual ~A();\n"
"private:\n"
" wxTimer *WxTimer1;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void noConstructor11() { // #3552
check("class Fred { int x; };\n"
"union U { int y; Fred fred; };");
ASSERT_EQUALS("", errout.str());
}
void noConstructor12() { // #8951
check("class Fred { int x{0}; };");
ASSERT_EQUALS("", errout.str());
check("class Fred { int x=0; };");
ASSERT_EQUALS("", errout.str());
check("class Fred { int x[1]={0}; };"); // #8850
ASSERT_EQUALS("", errout.str());
check("class Fred { int x[1]{0}; };");
ASSERT_EQUALS("", errout.str());
}
void noConstructor13() { // #9998
check("struct C { int v; };\n"
"struct B { C c[5] = {}; };\n"
"struct A {\n"
" A() {}\n"
" B b;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void noConstructor14() { // #10770
check("typedef void (*Func)();\n"
"class C {\n"
"public:\n"
" void f();\n"
"private:\n"
" Func fp = nullptr;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void noConstructor15() { // #5499
check("class C {\n"
"private:\n"
" int i1 = 0;\n"
" int i2;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (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());
}
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
// ticket #3190 "SymbolDatabase: Parse of sub class constructor fails"
void forwardDeclaration() {
check("class foo;\n"
"int bar;");
ASSERT_EQUALS("", errout.str());
check("class foo;\n"
"class foo;");
ASSERT_EQUALS("", errout.str());
check("class foo{};\n"
"class foo;");
ASSERT_EQUALS("", errout.str());
}
void initvar_with_this() {
check("struct Fred\n"
"{\n"
" Fred()\n"
" { this->i = 0; }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void initvar_if() {
check("struct Fred\n"
"{\n"
" Fred()\n"
" {\n"
" if (true)\n"
" i = 0;\n"
" else\n"
" i = 1;\n"
" }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq1() {
// Bug 2190376 and #3820 - False positive, Uninitialized member variable with operator=
check("struct Fred\n"
"{\n"
" int i;\n"
"\n"
" Fred()\n"
" { i = 0; }\n"
"\n"
" Fred(const Fred &fred)\n"
" { *this = fred; }\n"
"\n"
" const Fred & operator=(const Fred &fred)\n"
" { i = fred.i; return *this; }\n"
"};");
ASSERT_EQUALS("", errout.str());
check("struct Fred {\n"
" int i;\n"
"\n"
" Fred(const Fred &fred)\n"
" { (*this) = fred; }\n"
"\n"
" const Fred & operator=(const Fred &fred)\n"
" { i = fred.i; return *this; }\n"
"};");
ASSERT_EQUALS("", errout.str());
check("struct A\n"
"{\n"
" A() : i(0), j(0) {}\n"
"\n"
" A &operator=(const int &value)\n"
" {\n"
" i = value;\n"
" return (*this);\n"
" }\n"
"\n"
" int i;\n"
" int j;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq2() {
check("struct Fred\n"
"{\n"
" Fred() { i = 0; }\n"
" void operator=(const Fred &fred) { }\n"
" int i;\n"
"};");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='.\n", errout.str());
}
void initvar_operator_eq3() {
check("struct Fred\n"
"{\n"
" Fred() { Init(); }\n"
" void operator=(const Fred &fred) { Init(); }\n"
"private:\n"
" void Init() { i = 0; }\n"
" int i;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq4() {
check("class Fred\n"
"{\n"
" int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='.\n", errout.str());
check("class Fred\n"
"{\n"
" int * i;\n"
"public:\n"
" Fred() : i(NULL) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='.\n", errout.str());
check("class Fred\n"
"{\n"
" const int * i;\n"
"public:\n"
" Fred() : i(NULL) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='.\n", errout.str());
check("class Fred\n"
"{\n"
" const int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq5() { // #4119 - false positive when using swap to assign variables
check("class Fred {\n"
" int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" ~Fred() { }\n"
" Fred(const Fred &fred) : i(fred.i) { }\n"
" Fred & operator=(const Fred &rhs) {\n"
" Fred(rhs).swap(*this);\n"
" return *this;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq6() { // std::vector
check("struct Fred {\n"
" uint8_t data;\n"
" Fred & operator=(const Fred &rhs) {\n"
" return *this;\n"
" }\n"
"};",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());
}
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());
}
void initvar_operator_eq8() {
check("struct B {\n"
" int b;\n"
"};\n"
"struct D1 : B {\n"
" D1& operator=(const D1& src);\n"
" int d1;\n"
"};\n"
"struct D2 : D1 {\n"
" D2& operator=(const D2& src);\n"
" int d2;\n"
"};\n"
"struct D3 : D2 {\n"
" D3& operator=(const D3& src) {\n"
" D1::operator=(src);\n"
" d3_1 = src.d3_1;\n"