130 lines · cpp
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 4namespace with::issue {5 6typedef enum EStatus {7 SUCCESS = 1,8 FAILURE = 2,9 INVALID_PARAM = 3,10 UNKNOWN = 411} Status;12 13bool 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 18bool 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 23bool 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 28bool 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 36bool 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 43namespace without::issue {44 45enum StatusWithZero {46 UNK = 0,47 OK = 1,48 NOT_OK = 249};50 51bool testEnumConversion(StatusWithZero value) {52 return value;53}54 55enum WithDefault {56 Value0,57 Value158};59 60bool testEnumConversion(WithDefault value) {61 return value;62}63 64enum WithNegative : int {65 Nen2 = -2,66 Nen1,67 Nen068};69 70bool testEnumConversion(WithNegative value) {71 return value;72}73 74enum EStatus {75 SUCCESS = 1,76 FAILURE,77 INVALID_PARAM,78 UNKNOWN79};80 81bool explicitCompare(EStatus value) {82 return value == SUCCESS;83}84 85bool explicitBitUsage1(EStatus value) {86 return (value & SUCCESS);87}88 89bool explicitBitUsage2(EStatus value) {90 return (value | SUCCESS);91}92 93bool testEnumeratorCompare() {94 return SUCCESS;95}96 97enum IgnoredEnum {98 IGNORED_VALUE_1 = 1,99 IGNORED_VALUE_2100};101 102enum IgnoredSecondEnum {103 IGNORED_SECOND_VALUE_1 = 1,104 IGNORED_SECOND_VALUE_2105};106 107bool testIgnored(IgnoredEnum value) {108 return value;109}110 111bool testIgnored(IgnoredSecondEnum value) {112 return value;113}114 115enum CustomOperatorEnum {116 E0 = 0x1,117 E1 = 0x2,118 E2 = 0x4119};120 121CustomOperatorEnum operator&(CustomOperatorEnum a, CustomOperatorEnum b) { return static_cast<CustomOperatorEnum>(a & b); }122 123void testCustomOperator(CustomOperatorEnum e) {124 if (e & E1) {}125 if ((e & E1)) {}126 if (!(e & E1)) {}127}128 129}130