Skip to content

Commit df5e48e

Browse files
Update implicit-bool-conversion-check.cpp
Wrote basic level tests. (Similar to the tests in other check files).
1 parent fd1f7dc commit df5e48e

File tree

1 file changed

+8
-85
lines changed

1 file changed

+8
-85
lines changed
Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,16 @@
1-
// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t -- -- -std=c23
2-
// RUN: %check_clang_tidy -check-suffix=TO-BOOL %s readability-implicit-bool-conversion %t -- \
1+
// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t -- \
32
// RUN: -config='{CheckOptions: { \
43
// RUN: readability-implicit-bool-conversion.CheckConversionsToBool: true, \
54
// RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: true \
65
// RUN: }}' -- -std=c23
7-
// RUN: %check_clang_tidy -check-suffix=FROM-BOOL %s readability-implicit-bool-conversion %t -- \
8-
// RUN: -config='{CheckOptions: { \
9-
// RUN: readability-implicit-bool-conversion.CheckConversionsToBool: true, \
10-
// RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: true \
11-
// RUN: }}' -- -std=c23
12-
// RUN: %check_clang_tidy -check-suffix=TO-BOOL,FROM-BOOL %s readability-implicit-bool-conversion %t -- \
13-
// RUN: -config='{CheckOptions: { \
14-
// RUN: readability-implicit-bool-conversion.CheckConversionsToBool: true, \
15-
// RUN: readability-implicit-bool-conversion.CheckConversionsFromBool: true \
16-
// RUN: }}' -- -std=c23
17-
18-
19-
// ==========================================================
20-
// Test Case: Conversions to bool (CheckConversionsToBool=true)
21-
// ==========================================================
22-
void TestConversionsToBool() {
23-
int x = 42;
24-
if (x) // CHECK-MESSAGES-TO-BOOL: :[[@LINE]]:8: warning: implicit conversion 'int' -> 'bool'
25-
(void)0;
26-
27-
float f = 3.14;
28-
if (f) // CHECK-MESSAGES-TO-BOOL: :[[@LINE]]:8: warning: implicit conversion 'float' -> 'bool'
29-
(void)0;
30-
31-
int *p = nullptr;
32-
if (p) // CHECK-MESSAGES-TO-BOOL: :[[@LINE]]:8: warning: implicit conversion 'int *' -> 'bool'
33-
(void)0;
346

35-
// Pointer-to-member
36-
struct S {
37-
int member;
38-
};
39-
int S::*ptr = nullptr;
40-
if (ptr) // CHECK-MESSAGES-TO-BOOL: :[[@LINE]]:8: warning: implicit conversion 'int S::*' -> 'bool'
7+
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)
4111
(void)0;
42-
}
43-
44-
// ==========================================================
45-
// Test Case: Conversions from bool (CheckConversionsFromBool=true)
46-
// ==========================================================
47-
void TestConversionsFromBool() {
48-
bool b = true;
49-
50-
int x = b; // CHECK-MESSAGES-FROM-BOOL: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
51-
float f = b; // CHECK-MESSAGES-FROM-BOOL: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'float'
52-
}
53-
54-
// ==========================================================
55-
// Test Case: Mixed Configurations (ToBool=true, FromBool=true)
56-
// ==========================================================
57-
void TestMixedConfig() {
58-
int x = 42;
59-
if (x) // CHECK-MESSAGES-TO-BOOL: :[[@LINE]]:8: warning: implicit conversion 'int' -> 'bool'
60-
(void)0;
61-
62-
bool b = true;
63-
int y = b; // CHECK-MESSAGES-FROM-BOOL: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
64-
}
65-
66-
// ==========================================================
67-
// Test Case: No Diagnostics (ToBool=false, FromBool=false)
68-
// ==========================================================
69-
void TestNoDiagnostics() {
70-
// int x = 42;
71-
// if (x) // No warning: CheckConversionsToBool=false
72-
// (void)0;
73-
74-
// bool b = true;
75-
// int y = b; // No warning: CheckConversionsFromBool=false
76-
}
77-
78-
// ==========================================================
79-
// Test Case: Edge Cases and Complex Expressions
80-
// ==========================================================
81-
void TestEdgeCases() {
82-
bool b = true;
83-
84-
// Nested implicit casts
85-
int x = (b ? 1 : 0); // CHECK-MESSAGES-FROM-BOOL: :[[@LINE]]:13: warning: implicit conversion 'bool' -> 'int'
86-
87-
// Function returns implicit bool
88-
auto ReturnBool = []() -> bool { return true; };
89-
int y = ReturnBool(); // CHECK-MESSAGES-FROM-BOOL: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
9012

91-
// // Explicit casts (no diagnostics)
92-
// int z = static_cast<int>(b); // No warning: explicit cast
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);
9316
}

0 commit comments

Comments
 (0)