| 1 | // RUN: %check_clang_tidy %s modernize-use-nullptr %t -- -- -std=c23 |
| 2 | |
| 3 | #define NULL 0 |
| 4 | |
| 5 | void test_assignment() { |
| 6 | int *p1 = 0; |
| 7 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr [modernize-use-nullptr] |
| 8 | // CHECK-FIXES: int *p1 = nullptr; |
| 9 | p1 = 0; |
| 10 | // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr |
| 11 | // CHECK-FIXES: p1 = nullptr; |
| 12 | |
| 13 | int *p2 = NULL; |
| 14 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr |
| 15 | // CHECK-FIXES: int *p2 = nullptr; |
| 16 | |
| 17 | p2 = p1; |
| 18 | // CHECK-FIXES: p2 = p1; |
| 19 | |
| 20 | const int null = 0; |
| 21 | int *p3 = &null; |
| 22 | |
| 23 | p3 = NULL; |
| 24 | // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr |
| 25 | // CHECK-FIXES: p3 = nullptr; |
| 26 | |
| 27 | int *p4 = p3; |
| 28 | |
| 29 | int i1 = 0; |
| 30 | |
| 31 | int i2 = NULL; |
| 32 | |
| 33 | int i3 = null; |
| 34 | |
| 35 | int *p5, *p6, *p7; |
| 36 | p5 = p6 = p7 = NULL; |
| 37 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr |
| 38 | // CHECK-FIXES: p5 = p6 = p7 = nullptr; |
| 39 | } |
| 40 | |
| 41 | void test_function(int *p) {} |
| 42 | |
| 43 | void test_function_no_ptr_param(int i) {} |
| 44 | |
| 45 | void test_function_call() { |
| 46 | test_function(p: 0); |
| 47 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr |
| 48 | // CHECK-FIXES: test_function(nullptr); |
| 49 | |
| 50 | test_function(NULL); |
| 51 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr |
| 52 | // CHECK-FIXES: test_function(nullptr); |
| 53 | |
| 54 | test_function_no_ptr_param(i: 0); |
| 55 | } |
| 56 | |
| 57 | char *test_function_return1() { |
| 58 | return 0; |
| 59 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr |
| 60 | // CHECK-FIXES: return nullptr; |
| 61 | } |
| 62 | |
| 63 | void *test_function_return2() { |
| 64 | return NULL; |
| 65 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr |
| 66 | // CHECK-FIXES: return nullptr; |
| 67 | } |
| 68 | |
| 69 | int test_function_return4() { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int test_function_return5() { |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | int *test_function_return_cast1() { |
| 78 | return(int)0; |
| 79 | // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr |
| 80 | // CHECK-FIXES: return nullptr; |
| 81 | } |
| 82 | |
| 83 | int *test_function_return_cast2() { |
| 84 | #define RET return |
| 85 | RET(int)0; |
| 86 | // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use nullptr |
| 87 | // CHECK-FIXES: RET nullptr; |
| 88 | #undef RET |
| 89 | } |
| 90 | |
| 91 | // Test parentheses expressions resulting in a nullptr. |
| 92 | int *test_parentheses_expression1() { |
| 93 | return(0); |
| 94 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr |
| 95 | // CHECK-FIXES: return(nullptr); |
| 96 | } |
| 97 | |
| 98 | int *test_parentheses_expression2() { |
| 99 | return((int)(0.0f)); |
| 100 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr |
| 101 | // CHECK-FIXES: return(nullptr); |
| 102 | } |
| 103 | |
| 104 | int *test_nested_parentheses_expression() { |
| 105 | return((((0)))); |
| 106 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr |
| 107 | // CHECK-FIXES: return((((nullptr)))); |
| 108 | } |
| 109 | |
| 110 | void test_const_pointers() { |
| 111 | const int *const_p1 = 0; |
| 112 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr |
| 113 | // CHECK-FIXES: const int *const_p1 = nullptr; |
| 114 | const int *const_p2 = NULL; |
| 115 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr |
| 116 | // CHECK-FIXES: const int *const_p2 = nullptr; |
| 117 | const int *const_p3 = (int)0; |
| 118 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr |
| 119 | // CHECK-FIXES: const int *const_p3 = nullptr; |
| 120 | const int *const_p4 = (int)0.0f; |
| 121 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr |
| 122 | // CHECK-FIXES: const int *const_p4 = nullptr; |
| 123 | } |
| 124 | |
| 125 | void test_nested_implicit_cast_expr() { |
| 126 | int func0(void*, void*); |
| 127 | int func1(int, void*, void*); |
| 128 | |
| 129 | (double)func1(0, 0, 0); |
| 130 | // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr |
| 131 | // CHECK-MESSAGES: :[[@LINE-2]]:23: warning: use nullptr |
| 132 | // CHECK-FIXES: (double)func1(0, nullptr, nullptr); |
| 133 | (double)func1(func0(0, 0), 0, 0); |
| 134 | // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use nullptr |
| 135 | // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: use nullptr |
| 136 | // CHECK-MESSAGES: :[[@LINE-3]]:30: warning: use nullptr |
| 137 | // CHECK-MESSAGES: :[[@LINE-4]]:33: warning: use nullptr |
| 138 | // CHECK-FIXES: (double)func1(func0(nullptr, nullptr), nullptr, nullptr); |
| 139 | } |
| 140 | |