1 // RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- -- -fno-delayed-template-parsing -fexceptions
2 // CHECK-FIXES: {{^}}#include <math.h>
3 
4 // Ensure that function declarations are not changed.
5 void some_func(int x, double d, bool b, const char *p);
6 
7 // Ensure that function arguments are not changed
identity_function(int x)8 int identity_function(int x) {
9   return x;
10 }
11 
12 int do_not_modify_me;
13 
14 static int should_not_be_initialized;
15 extern int should_not_be_initialized2;
16 
17 typedef struct {
18   int unaltered1;
19   int unaltered2;
20 } UnusedStruct;
21 
22 typedef int my_int_type;
23 #define MACRO_INT int
24 #define FULL_DECLARATION() int macrodecl;
25 
26 template <typename T>
template_test_function()27 void template_test_function() {
28   T t;
29   int uninitialized;
30   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'uninitialized' is not initialized [cppcoreguidelines-init-variables]
31   // CHECK-FIXES: {{^}}  int uninitialized = 0;{{$}}
32 }
33 
init_unit_tests()34 void init_unit_tests() {
35   int x;
36   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'x' is not initialized [cppcoreguidelines-init-variables]
37   // CHECK-FIXES: {{^}}  int x = 0;{{$}}
38   my_int_type myint;
39   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'myint' is not initialized [cppcoreguidelines-init-variables]
40   // CHECK-FIXES: {{^}}  my_int_type myint = 0;{{$}}
41 
42   MACRO_INT macroint;
43   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'macroint' is not initialized [cppcoreguidelines-init-variables]
44   // CHECK-FIXES: {{^}}  MACRO_INT macroint = 0;{{$}}
45   FULL_DECLARATION();
46 
47   int x0 = 1, x1, x2 = 2;
48   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'x1' is not initialized [cppcoreguidelines-init-variables]
49   // CHECK-FIXES: {{^}}  int x0 = 1, x1 = 0, x2 = 2;{{$}}
50   int y0, y1 = 1, y2;
51   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'y0' is not initialized [cppcoreguidelines-init-variables]
52   // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: variable 'y2' is not initialized [cppcoreguidelines-init-variables]
53   // CHECK-FIXES: {{^}}  int y0 = 0, y1 = 1, y2 = 0;{{$}}
54   int hasval = 42;
55 
56   float f;
57   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'f' is not initialized [cppcoreguidelines-init-variables]
58   // CHECK-FIXES: {{^}}  float f = NAN;{{$}}
59   float fval = 85.0;
60   double d;
61   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'd' is not initialized [cppcoreguidelines-init-variables]
62   // CHECK-FIXES: {{^}}  double d = NAN;{{$}}
63   double dval = 99.0;
64 
65   bool b;
66   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: variable 'b' is not initialized [cppcoreguidelines-init-variables]
67   // CHECK-FIXES: {{^}}  bool b = 0;{{$}}
68   bool bval = true;
69 
70   const char *ptr;
71   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'ptr' is not initialized [cppcoreguidelines-init-variables]
72   // CHECK-FIXES: {{^}}  const char *ptr = nullptr;{{$}}
73   const char *ptrval = "a string";
74 
75   UnusedStruct u;
76 
77   static int does_not_need_an_initializer;
78   extern int does_not_need_an_initializer2;
79   int parens(42);
80   int braces{42};
81 }
82 
83 template <typename RANGE>
f(RANGE r)84 void f(RANGE r) {
85   for (char c : r) {
86   }
87 }
88 
catch_variable_decl()89 void catch_variable_decl() {
90   // Expect no warning given here.
91   try {
92   } catch (int X) {
93   }
94 }
95 
96 enum Color { Red,
97              Green,
98              Blue };
99 
100 enum Car { Benz,
101            BMW = 20,
102            Audi = BMW + 2 };
103 
104 enum Gender : char { Male,
105                      Female };
106 
107 enum class Direction { Up,
108                        Down,
109                        Left,
110                        Right };
111 
112 enum class Fruit : int { Apple,
113                          Orange };
114 
uninitialized_enum()115 void uninitialized_enum() {
116   Color color;
117   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'color' is not initialized [cppcoreguidelines-init-variables]
118   Car car;
119   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'car' is not initialized [cppcoreguidelines-init-variables]
120   Gender gender;
121   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'gender' is not initialized [cppcoreguidelines-init-variables]
122   Direction direction;
123   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'direction' is not initialized [cppcoreguidelines-init-variables]
124   Fruit fruit;
125   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'fruit' is not initialized [cppcoreguidelines-init-variables]
126 }
127