File: modernize-use-bool-literals.cpp

package info (click to toggle)
llvm-toolchain-3.9 1%3A3.9.1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 441,060 kB
  • ctags: 428,777
  • sloc: cpp: 2,546,577; ansic: 538,318; asm: 119,677; objc: 103,316; python: 102,148; sh: 27,847; pascal: 5,626; ml: 5,510; perl: 5,293; lisp: 4,801; makefile: 2,177; xml: 686; cs: 362; php: 212; csh: 117
file content (118 lines) | stat: -rw-r--r-- 3,726 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// RUN: %check_clang_tidy %s modernize-use-bool-literals %t

bool IntToTrue = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: converting integer literal to bool, use bool literal instead [modernize-use-bool-literals]
// CHECK-FIXES: {{^}}bool IntToTrue = true;{{$}}

bool IntToFalse(0);
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: {{.*}}
// CHECK-FIXES: {{^}}bool IntToFalse(false);{{$}}

bool LongLongToTrue{0x1LL};
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}
// CHECK-FIXES: {{^}}bool LongLongToTrue{true};{{$}}

bool ExplicitCStyleIntToFalse = (bool)0;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: {{.*}}
// CHECK-FIXES: {{^}}bool ExplicitCStyleIntToFalse = false;{{$}}

bool ExplicitFunctionalIntToFalse = bool(0);
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: {{.*}}
// CHECK-FIXES: {{^}}bool ExplicitFunctionalIntToFalse = false;{{$}}

bool ExplicitStaticIntToFalse = static_cast<bool>(0);
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: {{.*}}
// CHECK-FIXES: {{^}}bool ExplicitStaticIntToFalse = false;{{$}}

#define TRUE_MACRO 1
// CHECK-FIXES: {{^}}#define TRUE_MACRO 1{{$}}

bool MacroIntToTrue = TRUE_MACRO;
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: converting integer literal to bool, use bool literal instead [modernize-use-bool-literals]
// CHECK-FIXES: {{^}}bool MacroIntToTrue = TRUE_MACRO;{{$}}

#define FALSE_MACRO bool(0)
// CHECK-FIXES: {{^}}#define FALSE_MACRO bool(0){{$}}

bool TrueBool = true; // OK

bool FalseBool = bool(FALSE_MACRO);
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: {{.*}}
// CHECK-FIXES: {{^}}bool FalseBool = bool(FALSE_MACRO);{{$}}

void boolFunction(bool bar) {

}

char Character = 0; // OK

unsigned long long LongInteger = 1; // OK

#define MACRO_DEPENDENT_CAST(x) static_cast<bool>(x)
// CHECK-FIXES: {{^}}#define MACRO_DEPENDENT_CAST(x) static_cast<bool>(x){{$}}

bool MacroDependentBool = MACRO_DEPENDENT_CAST(0);
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: {{.*}}
// CHECK-FIXES: {{^}}bool MacroDependentBool = MACRO_DEPENDENT_CAST(0);{{$}}

bool ManyMacrosDependent = MACRO_DEPENDENT_CAST(FALSE_MACRO);
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: {{.*}}
// CHECK-FIXES: {{^}}bool ManyMacrosDependent = MACRO_DEPENDENT_CAST(FALSE_MACRO);{{$}}

class FooClass {
  public:
  FooClass() : JustBool(0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}FooClass() : JustBool(false) {}{{$}}
  FooClass(int) : JustBool{0} {}
  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}FooClass(int) : JustBool{false} {}{{$}}
  private:
  bool JustBool;
  bool BoolWithBraces{0};
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}bool BoolWithBraces{false};{{$}}
  bool BoolFromInt = 0;
  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}bool BoolFromInt = false;{{$}}
  bool SimpleBool = true; // OK
};

template<typename type>
void templateFunction(type) {
  type TemplateType = 0;
  // CHECK-FIXES: {{^ *}}type TemplateType = 0;{{$}}
}

template<int c>
void valueDependentTemplateFunction() {
  bool Boolean = c;
  // CHECK-FIXES: {{^ *}}bool Boolean = c;{{$}}
}

template<typename type>
void anotherTemplateFunction(type) {
  bool JustBool = 0;
  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}bool JustBool = false;{{$}}
}

int main() {
  boolFunction(1);
  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}boolFunction(true);{{$}}

  boolFunction(false);

  templateFunction(0);

  templateFunction(false);

  valueDependentTemplateFunction<1>();

  anotherTemplateFunction(1);

  IntToTrue = 1;
  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}
  // CHECK-FIXES: {{^ *}}IntToTrue = true;{{$}}
}