|
1 | | -// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t -- \ |
| 1 | +// RUN: %check_clang_tidy -check-suffix=FROM %s readability-implicit-bool-conversion %t -- \ |
| 2 | +// RUN: -config='{CheckOptions: { \ |
| 3 | +// RUN: readability-implicit-bool-conversion.CheckConversionsToBool: false, \ |
| 4 | +// RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: true \ |
| 5 | +// RUN: }}' -- -std=c23 |
| 6 | +// RUN: %check_clang_tidy -check-suffix=TO %s readability-implicit-bool-conversion %t -- \ |
| 7 | +// RUN: -config='{CheckOptions: { \ |
| 8 | +// RUN: readability-implicit-bool-conversion.CheckConversionsToBool: true, \ |
| 9 | +// RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: false \ |
| 10 | +// RUN: }}' -- -std=c23 |
| 11 | +// RUN: %check_clang_tidy -check-suffix=NORMAL %s readability-implicit-bool-conversion %t -- \ |
| 12 | +// RUN: -config='{CheckOptions: { \ |
| 13 | +// RUN: readability-implicit-bool-conversion.CheckConversionsToBool: false, \ |
| 14 | +// RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: false \ |
| 15 | +// RUN: }}' -- -std=c23 |
| 16 | +// RUN: %check_clang_tidy -check-suffix=TO,FROM %s readability-implicit-bool-conversion %t -- \ |
2 | 17 | // RUN: -config='{CheckOptions: { \ |
3 | 18 | // RUN: readability-implicit-bool-conversion.CheckConversionsToBool: true, \ |
4 | 19 | // RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: true \ |
5 | 20 | // RUN: }}' -- -std=c23 |
6 | 21 |
|
| 22 | +// Test various implicit bool conversions in different contexts |
7 | 23 | void TestImplicitBoolConversion() { |
8 | | - int intValue = 10; |
9 | | - if (intValue) // CHECK-MESSAGES: :[[@LINE]]:7: warning: implicit conversion 'int' -> 'bool' [readability-implicit-bool-conversion] |
10 | | - // CHECK-FIXES: if (intValue != 0) |
11 | | - (void)0; |
| 24 | + // Basic type conversions to bool |
| 25 | + int intValue = 42; |
| 26 | + if (intValue) // CHECK-MESSAGES-TO: :[[@LINE]]:9: warning: implicit conversion 'int' -> 'bool' [readability-implicit-bool-conversion] |
| 27 | + // CHECK-FIXES-TO: if (intValue != 0) |
| 28 | + (void)0; |
| 29 | + |
| 30 | + float floatValue = 3.14f; |
| 31 | + while (floatValue) // CHECK-MESSAGES-TO: :[[@LINE]]:12: warning: implicit conversion 'float' -> 'bool' [readability-implicit-bool-conversion] |
| 32 | + // CHECK-FIXES-TO: while (floatValue != 0.0f) |
| 33 | + break; |
| 34 | + |
| 35 | + char charValue = 'a'; |
| 36 | + do { |
| 37 | + break; |
| 38 | + } while (charValue); // CHECK-MESSAGES-TO: :[[@LINE]]:14: warning: implicit conversion 'char' -> 'bool' [readability-implicit-bool-conversion] |
| 39 | + // CHECK-FIXES-TO: } while (charValue != 0); |
| 40 | + |
| 41 | + // Pointer conversions to bool |
| 42 | + int* ptrValue = &intValue; |
| 43 | + if (ptrValue) // CHECK-MESSAGES-TO: :[[@LINE]]:9: warning: implicit conversion 'int *' -> 'bool' [readability-implicit-bool-conversion] |
| 44 | + // CHECK-FIXES-TO: if (ptrValue != nullptr) |
| 45 | + (void)0; |
| 46 | + |
| 47 | + // Conversions from bool to other types |
| 48 | + bool boolValue = true; |
| 49 | + int intFromBool = boolValue; // CHECK-MESSAGES-FROM: :[[@LINE]]:23: warning: implicit conversion 'bool' -> 'int' [readability-implicit-bool-conversion] |
| 50 | + // CHECK-FIXES-FROM: int intFromBool = static_cast<int>(boolValue); |
| 51 | + |
| 52 | + float floatFromBool = boolValue; // CHECK-MESSAGES-FROM: :[[@LINE]]:27: warning: implicit conversion 'bool' -> 'float' [readability-implicit-bool-conversion] |
| 53 | + // CHECK-FIXES-FROM: float floatFromBool = static_cast<float>(boolValue); |
12 | 54 |
|
13 | | - bool boolValue = true; |
14 | | - int newIntValue = boolValue; // CHECK-MESSAGES: :[[@LINE]]:21: warning: implicit conversion 'bool' -> 'int' [readability-implicit-bool-conversion] |
15 | | - // CHECK-FIXES: int newIntValue = static_cast<int>(boolValue); |
| 55 | + char charFromBool = boolValue; // CHECK-MESSAGES-FROM: :[[@LINE]]:25: warning: implicit conversion 'bool' -> 'char' [readability-implicit-bool-conversion] |
| 56 | + // CHECK-FIXES-FROM: char charFromBool = static_cast<char>(boolValue); |
16 | 57 | } |
0 commit comments