1 | // RUN: %check_clang_tidy %s modernize-use-bool-literals %t -- \ |
2 | // RUN: -config="{CheckOptions: \ |
3 | // RUN: {modernize-use-bool-literals.IgnoreMacros: \ |
4 | // RUN: true}}" |
5 | |
6 | bool IntToTrue = 1; |
7 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: converting integer literal to bool, use bool literal instead [modernize-use-bool-literals] |
8 | // CHECK-FIXES: {{^}}bool IntToTrue = true;{{$}} |
9 | |
10 | bool IntToFalse(0); |
11 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: converting integer literal to bool |
12 | // CHECK-FIXES: {{^}}bool IntToFalse(false);{{$}} |
13 | |
14 | bool LongLongToTrue{0x1LL}; |
15 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: converting integer literal to bool |
16 | // CHECK-FIXES: {{^}}bool LongLongToTrue{true};{{$}} |
17 | |
18 | bool ExplicitCStyleIntToFalse = (bool)0; |
19 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: converting integer literal to bool |
20 | // CHECK-FIXES: {{^}}bool ExplicitCStyleIntToFalse = false;{{$}} |
21 | |
22 | bool ExplicitFunctionalIntToFalse = bool(0); |
23 | // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: converting integer literal to bool |
24 | // CHECK-FIXES: {{^}}bool ExplicitFunctionalIntToFalse = false;{{$}} |
25 | |
26 | bool ExplicitStaticIntToFalse = static_cast<bool>(0); |
27 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: converting integer literal to bool |
28 | // CHECK-FIXES: {{^}}bool ExplicitStaticIntToFalse = false;{{$}} |
29 | |
30 | #define TRUE_MACRO 1 |
31 | // CHECK-FIXES: {{^}}#define TRUE_MACRO 1{{$}} |
32 | |
33 | bool MacroIntToTrue = TRUE_MACRO; |
34 | // CHECK-FIXES: {{^}}bool MacroIntToTrue = TRUE_MACRO;{{$}} |
35 | |
36 | #define FALSE_MACRO bool(0) |
37 | // CHECK-FIXES: {{^}}#define FALSE_MACRO bool(0){{$}} |
38 | |
39 | bool TrueBool = true; // OK |
40 | |
41 | bool FalseBool = bool(FALSE_MACRO); |
42 | // CHECK-FIXES: {{^}}bool FalseBool = bool(FALSE_MACRO);{{$}} |
43 | |
44 | void boolFunction(bool bar) { |
45 | |
46 | } |
47 | |
48 | char Character = 0; // OK |
49 | |
50 | unsigned long long LongInteger = 1; // OK |
51 | |
52 | #define MACRO_DEPENDENT_CAST(x) static_cast<bool>(x) |
53 | // CHECK-FIXES: {{^}}#define MACRO_DEPENDENT_CAST(x) static_cast<bool>(x){{$}} |
54 | |
55 | bool MacroDependentBool = MACRO_DEPENDENT_CAST(0); |
56 | // CHECK-FIXES: {{^}}bool MacroDependentBool = MACRO_DEPENDENT_CAST(0);{{$}} |
57 | |
58 | bool ManyMacrosDependent = MACRO_DEPENDENT_CAST(FALSE_MACRO); |
59 | // CHECK-FIXES: {{^}}bool ManyMacrosDependent = MACRO_DEPENDENT_CAST(FALSE_MACRO);{{$}} |
60 | |
61 | class FooClass { |
62 | public: |
63 | FooClass() : JustBool(0) {} |
64 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: converting integer literal to bool |
65 | // CHECK-FIXES: {{^ *}}FooClass() : JustBool(false) {}{{$}} |
66 | FooClass(int) : JustBool{0} {} |
67 | // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: converting integer literal to bool |
68 | // CHECK-FIXES: {{^ *}}FooClass(int) : JustBool{false} {}{{$}} |
69 | private: |
70 | bool JustBool; |
71 | bool BoolWithBraces{0}; |
72 | // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: converting integer literal to bool |
73 | // CHECK-FIXES: {{^ *}}bool BoolWithBraces{false};{{$}} |
74 | bool BoolFromInt = 0; |
75 | // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: converting integer literal to bool |
76 | // CHECK-FIXES: {{^ *}}bool BoolFromInt = false;{{$}} |
77 | bool SimpleBool = true; // OK |
78 | }; |
79 | |
80 | template<typename type> |
81 | void templateFunction(type) { |
82 | type TemplateType = 0; |
83 | // CHECK-FIXES: {{^ *}}type TemplateType = 0;{{$}} |
84 | } |
85 | |
86 | template<int c> |
87 | void valueDependentTemplateFunction() { |
88 | bool Boolean = c; |
89 | // CHECK-FIXES: {{^ *}}bool Boolean = c;{{$}} |
90 | } |
91 | |
92 | template<typename type> |
93 | void anotherTemplateFunction(type) { |
94 | bool JustBool = 0; |
95 | // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: converting integer literal to bool |
96 | // CHECK-FIXES: {{^ *}}bool JustBool = false;{{$}} |
97 | } |
98 | |
99 | int main() { |
100 | boolFunction(bar: 1); |
101 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: converting integer literal to bool |
102 | // CHECK-FIXES: {{^ *}}boolFunction(true);{{$}} |
103 | |
104 | boolFunction(bar: false); |
105 | |
106 | templateFunction(0); |
107 | |
108 | templateFunction(false); |
109 | |
110 | valueDependentTemplateFunction<1>(); |
111 | |
112 | anotherTemplateFunction(1); |
113 | |
114 | IntToTrue = 1; |
115 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: converting integer literal to bool |
116 | // CHECK-FIXES: {{^ *}}IntToTrue = true;{{$}} |
117 | } |
118 | |
119 | static int Value = 1; |
120 | |
121 | bool Function1() { |
122 | bool Result = Value == 1 ? 1 : 0; |
123 | // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: converting integer literal to bool |
124 | // CHECK-MESSAGES: :[[@LINE-2]]:34: warning: converting integer literal to bool |
125 | // CHECK-FIXES: {{^ *}}bool Result = Value == 1 ? true : false;{{$}} |
126 | return Result; |
127 | } |
128 | |
129 | bool Function2() { |
130 | return Value == 1 ? 1 : 0; |
131 | // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: converting integer literal to bool |
132 | // CHECK-MESSAGES: :[[@LINE-2]]:27: warning: converting integer literal to bool |
133 | // CHECK-FIXES: {{^ *}}return Value == 1 ? true : false;{{$}} |
134 | } |
135 | |
136 | void foo() { |
137 | bool Result; |
138 | Result = Value == 1 ? true : 0; |
139 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: converting integer literal to bool |
140 | // CHECK-FIXES: {{^ *}}Result = Value == 1 ? true : false;{{$}} |
141 | Result = Value == 1 ? false : bool(0); |
142 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: converting integer literal to bool |
143 | // CHECK-FIXES: {{^ *}}Result = Value == 1 ? false : false;{{$}} |
144 | Result = Value == 1 ? (bool)0 : false; |
145 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: converting integer literal to bool |
146 | // CHECK-FIXES: {{^ *}}Result = Value == 1 ? false : false;{{$}} |
147 | } |
148 | |