1 // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \
2 // RUN: -config='{CheckOptions: \
3 // RUN:  [{key: readability-implicit-bool-conversion.AllowIntegerConditions, value: 1}, \
4 // RUN:   {key: readability-implicit-bool-conversion.AllowPointerConditions, value: 1}]}'
5 
6 template<typename T>
7 void functionTaking(T);
8 
9 int functionReturningInt();
10 int* functionReturningPointer();
11 
12 struct Struct {
13   int member;
14   unsigned bitfield : 1;
15 };
16 
17 
regularImplicitConversionIntegerToBoolIsNotIgnored()18 void regularImplicitConversionIntegerToBoolIsNotIgnored() {
19   int integer = 0;
20   functionTaking<bool>(integer);
21   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'int' -> bool [readability-implicit-bool-conversion]
22   // CHECK-FIXES: functionTaking<bool>(integer != 0);
23 }
24 
implicitConversionIntegerToBoolInConditionalsIsAllowed()25 void implicitConversionIntegerToBoolInConditionalsIsAllowed() {
26   Struct s = {};
27   if (s.member) {}
28   if (!s.member) {}
29   if (s.bitfield) {}
30   if (!s.bitfield) {}
31   if (functionReturningInt()) {}
32   if (!functionReturningInt()) {}
33   if (functionReturningInt() && functionReturningPointer()) {}
34   if (!functionReturningInt() && !functionReturningPointer()) {}
35   for (; functionReturningInt(); ) {}
36   for (; functionReturningPointer(); ) {}
37   for (; functionReturningInt() && !functionReturningPointer() || (!functionReturningInt() && functionReturningPointer()); ) {}
38   while (functionReturningInt()) {}
39   while (functionReturningPointer()) {}
40   while (functionReturningInt() && !functionReturningPointer() || (!functionReturningInt() && functionReturningPointer())) {}
41   int value1 = functionReturningInt() ? 1 : 2;
42   int value2 = !functionReturningInt() ? 1 : 2;
43   int value3 = (functionReturningInt() && functionReturningPointer() || !functionReturningInt()) ? 1 : 2;
44   int value4 = functionReturningInt() ?: value3;
45   int *p1 = functionReturningPointer() ?: &value3;
46 }
47 
regularImplicitConversionPointerToBoolIsNotIgnored()48 void regularImplicitConversionPointerToBoolIsNotIgnored() {
49   int* pointer = nullptr;
50   functionTaking<bool>(pointer);
51   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'int *' -> bool
52   // CHECK-FIXES: functionTaking<bool>(pointer != nullptr);
53 
54   int Struct::* memberPointer = &Struct::member;
55   functionTaking<bool>(memberPointer);
56   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'int Struct::*' -> bool
57   // CHECK-FIXES: functionTaking<bool>(memberPointer != nullptr);
58 }
59 
implicitConversionPointerToBoolInConditionalsIsAllowed()60 void implicitConversionPointerToBoolInConditionalsIsAllowed() {
61   if (functionReturningPointer()) {}
62   if (not functionReturningPointer()) {}
63   int value1 = functionReturningPointer() ? 1 : 2;
64   int value2 = (not functionReturningPointer()) ? 1 : 2;
65 
66   int Struct::* memberPointer = &Struct::member;
67   if (memberPointer) {}
68   if (memberPointer) {}
69   int value3 = memberPointer ? 1 : 2;
70   int value4 = (not memberPointer) ? 1 : 2;
71 }
72