1ce6de98bSSockke // RUN: %check_clang_tidy %s cppcoreguidelines-init-variables -fix-errors %t -- -- -fno-delayed-template-parsing -fexceptions 289a1d03eSRichard // CHECK-FIXES: {{^}}#include <math.h> 389a1d03eSRichard 489a1d03eSRichard // Ensure that function declarations are not changed. 589a1d03eSRichard void some_func(int x, double d, bool b, const char *p); 689a1d03eSRichard 789a1d03eSRichard // Ensure that function arguments are not changed 889a1d03eSRichard int identity_function(int x) { 989a1d03eSRichard return x; 1089a1d03eSRichard } 1189a1d03eSRichard 1289a1d03eSRichard int do_not_modify_me; 1389a1d03eSRichard 1489a1d03eSRichard static int should_not_be_initialized; 1589a1d03eSRichard extern int should_not_be_initialized2; 1689a1d03eSRichard 1789a1d03eSRichard typedef struct { 1889a1d03eSRichard int unaltered1; 1989a1d03eSRichard int unaltered2; 2089a1d03eSRichard } UnusedStruct; 2189a1d03eSRichard 2289a1d03eSRichard typedef int my_int_type; 2389a1d03eSRichard #define MACRO_INT int 2489a1d03eSRichard #define FULL_DECLARATION() int macrodecl; 2589a1d03eSRichard 2689a1d03eSRichard template <typename T> 2789a1d03eSRichard void template_test_function() { 2889a1d03eSRichard T t; 2989a1d03eSRichard int uninitialized; 3089a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'uninitialized' is not initialized [cppcoreguidelines-init-variables] 3189a1d03eSRichard // CHECK-FIXES: {{^}} int uninitialized = 0;{{$}} 3289a1d03eSRichard } 3389a1d03eSRichard 3489a1d03eSRichard void init_unit_tests() { 3589a1d03eSRichard int x; 3689a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'x' is not initialized [cppcoreguidelines-init-variables] 3789a1d03eSRichard // CHECK-FIXES: {{^}} int x = 0;{{$}} 3889a1d03eSRichard my_int_type myint; 3989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'myint' is not initialized [cppcoreguidelines-init-variables] 4089a1d03eSRichard // CHECK-FIXES: {{^}} my_int_type myint = 0;{{$}} 4189a1d03eSRichard 4289a1d03eSRichard MACRO_INT macroint; 4389a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'macroint' is not initialized [cppcoreguidelines-init-variables] 4489a1d03eSRichard // CHECK-FIXES: {{^}} MACRO_INT macroint = 0;{{$}} 4589a1d03eSRichard FULL_DECLARATION(); 4689a1d03eSRichard 4789a1d03eSRichard int x0 = 1, x1, x2 = 2; 4889a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'x1' is not initialized [cppcoreguidelines-init-variables] 4989a1d03eSRichard // CHECK-FIXES: {{^}} int x0 = 1, x1 = 0, x2 = 2;{{$}} 5089a1d03eSRichard int y0, y1 = 1, y2; 5189a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'y0' is not initialized [cppcoreguidelines-init-variables] 5289a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: variable 'y2' is not initialized [cppcoreguidelines-init-variables] 5389a1d03eSRichard // CHECK-FIXES: {{^}} int y0 = 0, y1 = 1, y2 = 0;{{$}} 5489a1d03eSRichard int hasval = 42; 5589a1d03eSRichard 5689a1d03eSRichard float f; 5789a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'f' is not initialized [cppcoreguidelines-init-variables] 5889a1d03eSRichard // CHECK-FIXES: {{^}} float f = NAN;{{$}} 5989a1d03eSRichard float fval = 85.0; 6089a1d03eSRichard double d; 6189a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'd' is not initialized [cppcoreguidelines-init-variables] 6289a1d03eSRichard // CHECK-FIXES: {{^}} double d = NAN;{{$}} 6389a1d03eSRichard double dval = 99.0; 6489a1d03eSRichard 6589a1d03eSRichard bool b; 6689a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: variable 'b' is not initialized [cppcoreguidelines-init-variables] 6733e21295SDanny Mösch // CHECK-FIXES: {{^}} bool b = false;{{$}} 6889a1d03eSRichard bool bval = true; 6989a1d03eSRichard 7089a1d03eSRichard const char *ptr; 7189a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'ptr' is not initialized [cppcoreguidelines-init-variables] 7289a1d03eSRichard // CHECK-FIXES: {{^}} const char *ptr = nullptr;{{$}} 7389a1d03eSRichard const char *ptrval = "a string"; 7489a1d03eSRichard 7589a1d03eSRichard UnusedStruct u; 7689a1d03eSRichard 7789a1d03eSRichard static int does_not_need_an_initializer; 7889a1d03eSRichard extern int does_not_need_an_initializer2; 7989a1d03eSRichard int parens(42); 8089a1d03eSRichard int braces{42}; 8189a1d03eSRichard } 8289a1d03eSRichard 8389a1d03eSRichard template <typename RANGE> 8489a1d03eSRichard void f(RANGE r) { 8589a1d03eSRichard for (char c : r) { 8689a1d03eSRichard } 8789a1d03eSRichard } 8889a1d03eSRichard 8989a1d03eSRichard void catch_variable_decl() { 9089a1d03eSRichard // Expect no warning given here. 9189a1d03eSRichard try { 9289a1d03eSRichard } catch (int X) { 9389a1d03eSRichard } 9489a1d03eSRichard } 9589a1d03eSRichard 9689a1d03eSRichard enum Color { Red, 9789a1d03eSRichard Green, 9889a1d03eSRichard Blue }; 9989a1d03eSRichard 10089a1d03eSRichard enum Car { Benz, 10189a1d03eSRichard BMW = 20, 10289a1d03eSRichard Audi = BMW + 2 }; 10389a1d03eSRichard 10489a1d03eSRichard enum Gender : char { Male, 10589a1d03eSRichard Female }; 10689a1d03eSRichard 10789a1d03eSRichard enum class Direction { Up, 10889a1d03eSRichard Down, 10989a1d03eSRichard Left, 11089a1d03eSRichard Right }; 11189a1d03eSRichard 11289a1d03eSRichard enum class Fruit : int { Apple, 11389a1d03eSRichard Orange }; 11489a1d03eSRichard 11589a1d03eSRichard void uninitialized_enum() { 11689a1d03eSRichard Color color; 11789a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'color' is not initialized [cppcoreguidelines-init-variables] 11889a1d03eSRichard Car car; 11989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'car' is not initialized [cppcoreguidelines-init-variables] 12089a1d03eSRichard Gender gender; 12189a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'gender' is not initialized [cppcoreguidelines-init-variables] 12289a1d03eSRichard Direction direction; 12389a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'direction' is not initialized [cppcoreguidelines-init-variables] 12489a1d03eSRichard Fruit fruit; 12589a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'fruit' is not initialized [cppcoreguidelines-init-variables] 12689a1d03eSRichard } 127ce6de98bSSockke 128ce6de98bSSockke void test_clang_diagnostic_error() { 129ce6de98bSSockke int a; 130ce6de98bSSockke // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'a' is not initialized [cppcoreguidelines-init-variables] 131ce6de98bSSockke // CHECK-FIXES: {{^}} int a = 0;{{$}} 132ce6de98bSSockke 133ce6de98bSSockke UnknownType b; 134ce6de98bSSockke // CHECK-MESSAGES: :[[@LINE-1]]:3: error: unknown type name 'UnknownType' [clang-diagnostic-error] 135ce6de98bSSockke // CHECK-FIXES-NOT: {{^}} UnknownType b = 0;{{$}} 136ce6de98bSSockke } 137*028ea71fSJulian Schmidt 138*028ea71fSJulian Schmidt namespace gh112089 { 139*028ea71fSJulian Schmidt void foo(void*); 140*028ea71fSJulian Schmidt using FPtr = void(*)(void*); 141*028ea71fSJulian Schmidt void test() { 142*028ea71fSJulian Schmidt void(*a1)(void*); 143*028ea71fSJulian Schmidt // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'a1' is not initialized [cppcoreguidelines-init-variables] 144*028ea71fSJulian Schmidt // CHECK-FIXES: void(*a1)(void*) = nullptr; 145*028ea71fSJulian Schmidt FPtr a2; 146*028ea71fSJulian Schmidt // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'a2' is not initialized [cppcoreguidelines-init-variables] 147*028ea71fSJulian Schmidt // CHECK-FIXES: FPtr a2 = nullptr; 148*028ea71fSJulian Schmidt } 149*028ea71fSJulian Schmidt } // namespace gh112089 150*028ea71fSJulian Schmidt 151