1 // RUN: %check_clang_tidy %s cppcoreguidelines-init-variables -fix-errors %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 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> 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 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 = false;{{$}} 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> 84 void f(RANGE r) { 85 for (char c : r) { 86 } 87 } 88 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 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 128 void test_clang_diagnostic_error() { 129 int a; 130 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'a' is not initialized [cppcoreguidelines-init-variables] 131 // CHECK-FIXES: {{^}} int a = 0;{{$}} 132 133 UnknownType b; 134 // CHECK-MESSAGES: :[[@LINE-1]]:3: error: unknown type name 'UnknownType' [clang-diagnostic-error] 135 // CHECK-FIXES-NOT: {{^}} UnknownType b = 0;{{$}} 136 } 137 138 namespace gh112089 { 139 void foo(void*); 140 using FPtr = void(*)(void*); 141 void test() { 142 void(*a1)(void*); 143 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'a1' is not initialized [cppcoreguidelines-init-variables] 144 // CHECK-FIXES: void(*a1)(void*) = nullptr; 145 FPtr a2; 146 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'a2' is not initialized [cppcoreguidelines-init-variables] 147 // CHECK-FIXES: FPtr a2 = nullptr; 148 } 149 } // namespace gh112089 150 151