| 1 | // RUN: %check_clang_tidy --match-partial-fixes -std=c++20 %s modernize-use-integer-sign-comparison %t |
| 2 | |
| 3 | // CHECK-FIXES: #include <utility> |
| 4 | |
| 5 | // The code that triggers the check |
| 6 | #define MAX_MACRO(a, b) (a < b) ? b : a |
| 7 | |
| 8 | unsigned int FuncParameters(int bla) { |
| 9 | unsigned int result = 0; |
| 10 | if (result == bla) |
| 11 | return 0; |
| 12 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 13 | // CHECK-FIXES: if (std::cmp_equal(result , bla)) |
| 14 | |
| 15 | return 1; |
| 16 | } |
| 17 | |
| 18 | template <typename T> |
| 19 | void TemplateFuncParameter(T val) { |
| 20 | unsigned long uL = 0; |
| 21 | if (val >= uL) |
| 22 | return; |
| 23 | // CHECK-MESSAGES-NOT: warning: |
| 24 | } |
| 25 | |
| 26 | template <typename T1, typename T2> |
| 27 | int TemplateFuncParameters(T1 val1, T2 val2) { |
| 28 | if (val1 >= val2) |
| 29 | return 0; |
| 30 | // CHECK-MESSAGES-NOT: warning: |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | int AllComparisons() { |
| 35 | unsigned int uVar = 42; |
| 36 | unsigned short uArray[7] = {0, 1, 2, 3, 9, 7, 9}; |
| 37 | |
| 38 | int sVar = -42; |
| 39 | short sArray[7] = {-1, -2, -8, -94, -5, -4, -6}; |
| 40 | |
| 41 | enum INT_TEST { |
| 42 | VAL1 = 0, |
| 43 | VAL2 = -1 |
| 44 | }; |
| 45 | |
| 46 | char ch = 'a'; |
| 47 | unsigned char uCh = 'a'; |
| 48 | signed char sCh = 'a'; |
| 49 | bool bln = false; |
| 50 | |
| 51 | if (bln == sVar) |
| 52 | return 0; |
| 53 | // CHECK-MESSAGES-NOT: warning: |
| 54 | |
| 55 | if (ch > uCh) |
| 56 | return 0; |
| 57 | // CHECK-MESSAGES-NOT: warning: |
| 58 | |
| 59 | if (sVar <= INT_TEST::VAL2) |
| 60 | return 0; |
| 61 | // CHECK-MESSAGES-NOT: warning: |
| 62 | |
| 63 | if (uCh < sCh) |
| 64 | return -1; |
| 65 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 66 | // CHECK-FIXES: if (std::cmp_less(uCh , sCh)) |
| 67 | |
| 68 | if ((int)uVar < sVar) |
| 69 | return 0; |
| 70 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 71 | // CHECK-FIXES: if (std::cmp_less(uVar, sVar)) |
| 72 | |
| 73 | (uVar != sVar) ? uVar = sVar |
| 74 | : sVar = uVar; |
| 75 | // CHECK-MESSAGES: :[[@LINE-2]]:6: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 76 | // CHECK-FIXES: (std::cmp_not_equal(uVar , sVar)) ? uVar = sVar |
| 77 | |
| 78 | while (uArray[0] <= sArray[0]) |
| 79 | return 0; |
| 80 | // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 81 | // CHECK-FIXES: while (std::cmp_less_equal(uArray[0] , sArray[0])) |
| 82 | |
| 83 | if (uArray[1] > sArray[1]) |
| 84 | return 0; |
| 85 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 86 | // CHECK-FIXES: if (std::cmp_greater(uArray[1] , sArray[1])) |
| 87 | |
| 88 | MAX_MACRO(uVar, sArray[0]); |
| 89 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 90 | |
| 91 | if (static_cast<unsigned int>(uArray[2]) < static_cast<int>(sArray[2])) |
| 92 | return 0; |
| 93 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 94 | // CHECK-FIXES: if (std::cmp_less(uArray[2],sArray[2])) |
| 95 | |
| 96 | if ((unsigned int)uArray[3] < (int)sArray[3]) |
| 97 | return 0; |
| 98 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 99 | // CHECK-FIXES: if (std::cmp_less(uArray[3],sArray[3])) |
| 100 | |
| 101 | if ((unsigned int)(uArray[4]) < (int)(sArray[4])) |
| 102 | return 0; |
| 103 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 104 | // CHECK-FIXES: if (std::cmp_less((uArray[4]),(sArray[4]))) |
| 105 | |
| 106 | if (uArray[5] > sArray[5]) |
| 107 | return 0; |
| 108 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 109 | // CHECK-FIXES: if (std::cmp_greater(uArray[5] , sArray[5])) |
| 110 | |
| 111 | #define VALUE sArray[6] |
| 112 | if (uArray[6] > VALUE) |
| 113 | return 0; |
| 114 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 115 | // CHECK-FIXES: if (std::cmp_greater(uArray[6] , VALUE)) |
| 116 | |
| 117 | |
| 118 | FuncParameters(bla: uVar); |
| 119 | TemplateFuncParameter(val: sVar); |
| 120 | TemplateFuncParameters(val1: uVar, val2: sVar); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | namespace PR127471 { |
| 126 | int getSignedValue(); |
| 127 | unsigned int getUnsignedValue(); |
| 128 | |
| 129 | void callExprTest() { |
| 130 | |
| 131 | if (getSignedValue() < getUnsignedValue()) |
| 132 | return; |
| 133 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 134 | // CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue())) |
| 135 | |
| 136 | int sVar = 0; |
| 137 | if (getUnsignedValue() > sVar) |
| 138 | return; |
| 139 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 140 | // CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar)) |
| 141 | |
| 142 | unsigned int uVar = 0; |
| 143 | if (getSignedValue() > uVar) |
| 144 | return; |
| 145 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 146 | // CHECK-FIXES: if (std::cmp_greater(getSignedValue() , uVar)) |
| 147 | |
| 148 | } |
| 149 | |
| 150 | // Add a class with member functions for testing member function calls |
| 151 | class TestClass { |
| 152 | public: |
| 153 | int getSignedValue() { return -5; } |
| 154 | unsigned int getUnsignedValue() { return 5; } |
| 155 | }; |
| 156 | |
| 157 | void memberFunctionTests() { |
| 158 | TestClass obj; |
| 159 | |
| 160 | if (obj.getSignedValue() < obj.getUnsignedValue()) |
| 161 | return; |
| 162 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 163 | // CHECK-FIXES: if (std::cmp_less(obj.getSignedValue() , obj.getUnsignedValue())) |
| 164 | } |
| 165 | |
| 166 | void castFunctionTests() { |
| 167 | // C-style casts with function calls |
| 168 | if ((int)getUnsignedValue() < (unsigned int)getSignedValue()) |
| 169 | return; |
| 170 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 171 | // CHECK-FIXES: if (std::cmp_less(getUnsignedValue(),getSignedValue())) |
| 172 | |
| 173 | |
| 174 | // Static casts with function calls |
| 175 | if (static_cast<int>(getUnsignedValue()) < static_cast<unsigned int>(getSignedValue())) |
| 176 | return; |
| 177 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 178 | // CHECK-FIXES: if (std::cmp_less(getUnsignedValue(),getSignedValue())) |
| 179 | } |
| 180 | |
| 181 | // Define tests |
| 182 | #define SIGNED_FUNC getSignedValue() |
| 183 | #define UNSIGNED_FUNC getUnsignedValue() |
| 184 | |
| 185 | void defineTests() { |
| 186 | if (SIGNED_FUNC < UNSIGNED_FUNC) |
| 187 | return; |
| 188 | // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] |
| 189 | // CHECK-FIXES: if (std::cmp_less(SIGNED_FUNC , UNSIGNED_FUNC)) |
| 190 | } |
| 191 | |
| 192 | // Template tests (should not warn) |
| 193 | template <typename T1> |
| 194 | void templateFunctionTest(T1 value) { |
| 195 | if (value() < getUnsignedValue()) |
| 196 | return; |
| 197 | |
| 198 | if (value() < (getSignedValue() || getUnsignedValue())) |
| 199 | return; |
| 200 | } |
| 201 | } // namespace PR127471 |
| 202 | |