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