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