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