171 lines · cpp
1// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables -fix-errors %t -- -- -fno-delayed-template-parsing -fexceptions2// CHECK-FIXES: #include <math.h>3 4// Ensure that function declarations are not changed.5void some_func(int x, double d, bool b, const char *p);6 7// Ensure that function arguments are not changed8int identity_function(int x) {9 return x;10}11 12int do_not_modify_me;13 14static int should_not_be_initialized;15extern int should_not_be_initialized2;16 17typedef struct {18 int unaltered1;19 int unaltered2;20} UnusedStruct;21 22typedef int my_int_type;23#define MACRO_INT int24#define FULL_DECLARATION() int macrodecl;25 26template <typename T>27void 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 34void 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 83template <typename RANGE>84void f(RANGE r) {85 for (char c : r) {86 }87}88 89void catch_variable_decl() {90 // Expect no warning given here.91 try {92 } catch (int X) {93 }94}95 96enum Color { Red,97 Green,98 Blue };99 100enum Car { Benz,101 BMW = 20,102 Audi = BMW + 2 };103 104enum Gender : char { Male,105 Female };106 107enum class Direction { Up,108 Down,109 Left,110 Right };111 112enum class Fruit : int { Apple,113 Orange };114 115void 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 128void 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 138namespace 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 gh112089150 151namespace gh161978 {152 void test() {153 bool (*fp1)(int);154 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables]155 // CHECK-FIXES: bool (*fp1)(int) = nullptr;156 bool (*fp2)(int, int);157 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp2' is not initialized [cppcoreguidelines-init-variables]158 // CHECK-FIXES: bool (*fp2)(int, int) = nullptr;159 bool (*fp3)(int, int, int);160 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp3' is not initialized [cppcoreguidelines-init-variables]161 // CHECK-FIXES: bool (*fp3)(int, int, int) = nullptr;162 bool (*fp4)(int, int, int, ...);163 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables]164 // CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr;165 bool (*fp5)(int, int), (*fp6)(int, int);166 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp5' is not initialized [cppcoreguidelines-init-variables]167 // CHECK-MESSAGES: :[[@LINE-2]]:30: warning: variable 'fp6' is not initialized [cppcoreguidelines-init-variables]168 // CHECK-FIXES: bool (*fp5)(int, int) = nullptr, (*fp6)(int, int) = nullptr;169 }170}171