| 1 |
// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-non-zero-enum-to-bool-conversion %t -- \ |
| 2 |
// RUN: -config="{CheckOptions: {bugprone-non-zero-enum-to-bool-conversion.EnumIgnoreList: '::without::issue::IgnoredEnum;IgnoredSecondEnum'}}" |
| 3 |
|
| 4 |
namespace with::issue { |
| 5 |
|
| 6 |
typedef enum EStatus { |
| 7 |
SUCCESS = 1, |
| 8 |
FAILURE = 2, |
| 9 |
INVALID_PARAM = 3, |
| 10 |
UNKNOWN = 4 |
| 11 |
} Status; |
| 12 |
|
| 13 |
bool testEnumConversion(EStatus value) { |
| 14 |
// CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EStatus' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion] |
| 15 |
return value; |
| 16 |
} |
| 17 |
|
| 18 |
bool testTypedefConversion(Status value) { |
| 19 |
// CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EStatus' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion] |
| 20 |
return value; |
| 21 |
} |
| 22 |
|
| 23 |
bool testExplicitConversion(EStatus value) { |
| 24 |
// CHECK-MESSAGES: :[[@LINE+1]]:28: warning: conversion of 'EStatus' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion] |
| 25 |
return static_cast<bool>(value); |
| 26 |
} |
| 27 |
|
| 28 |
bool testInIfConversion(EStatus value) { |
| 29 |
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: conversion of 'EStatus' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion] |
| 30 |
if (value) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
bool testWithNegation(EStatus value) { |
| 37 |
// CHECK-MESSAGES: :[[@LINE+1]]:14: warning: conversion of 'EStatus' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion] |
| 38 |
return not value; |
| 39 |
} |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
namespace without::issue { |
| 44 |
|
| 45 |
enum StatusWithZero { |
| 46 |
UNK = 0, |
| 47 |
OK = 1, |
| 48 |
NOT_OK = 2 |
| 49 |
}; |
| 50 |
|
| 51 |
bool testEnumConversion(StatusWithZero value) { |
| 52 |
return value; |
| 53 |
} |
| 54 |
|
| 55 |
enum WithDefault { |
| 56 |
Value0, |
| 57 |
Value1 |
| 58 |
}; |
| 59 |
|
| 60 |
bool testEnumConversion(WithDefault value) { |
| 61 |
return value; |
| 62 |
} |
| 63 |
|
| 64 |
enum WithNegative : int { |
| 65 |
Nen2 = -2, |
| 66 |
Nen1, |
| 67 |
Nen0 |
| 68 |
}; |
| 69 |
|
| 70 |
bool testEnumConversion(WithNegative value) { |
| 71 |
return value; |
| 72 |
} |
| 73 |
|
| 74 |
enum EStatus { |
| 75 |
SUCCESS = 1, |
| 76 |
FAILURE, |
| 77 |
INVALID_PARAM, |
| 78 |
UNKNOWN |
| 79 |
}; |
| 80 |
|
| 81 |
bool explicitCompare(EStatus value) { |
| 82 |
return value == SUCCESS; |
| 83 |
} |
| 84 |
|
| 85 |
bool explicitBitUsage1(EStatus value) { |
| 86 |
return (value & SUCCESS); |
| 87 |
} |
| 88 |
|
| 89 |
bool explicitBitUsage2(EStatus value) { |
| 90 |
return (value | SUCCESS); |
| 91 |
} |
| 92 |
|
| 93 |
bool testEnumeratorCompare() { |
| 94 |
return SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
enum IgnoredEnum { |
| 98 |
IGNORED_VALUE_1 = 1, |
| 99 |
IGNORED_VALUE_2 |
| 100 |
}; |
| 101 |
|
| 102 |
enum IgnoredSecondEnum { |
| 103 |
IGNORED_SECOND_VALUE_1 = 1, |
| 104 |
IGNORED_SECOND_VALUE_2 |
| 105 |
}; |
| 106 |
|
| 107 |
bool testIgnored(IgnoredEnum value) { |
| 108 |
return value; |
| 109 |
} |
| 110 |
|
| 111 |
bool testIgnored(IgnoredSecondEnum value) { |
| 112 |
return value; |
| 113 |
} |
| 114 |
|
| 115 |
enum CustomOperatorEnum { |
| 116 |
E0 = 0x1, |
| 117 |
E1 = 0x2, |
| 118 |
E2 = 0x4 |
| 119 |
}; |
| 120 |
|
| 121 |
CustomOperatorEnum operator&(CustomOperatorEnum a, CustomOperatorEnum b) { return static_cast<CustomOperatorEnum>(a & b); } |
| 122 |
|
| 123 |
void testCustomOperator(CustomOperatorEnum e) { |
| 124 |
if (e & E1) {} |
| 125 |
if ((e & E1)) {} |
| 126 |
if (!(e & E1)) {} |
| 127 |
} |
| 128 |
|
| 129 |
} |