1// RUN: %check_clang_tidy -std=c++11-or-later %s bugprone-non-zero-enum-to-bool-conversion %t
2
3namespace with::issue {
4
5enum class EStatusC : char {
6 SUCCESS = 1,
7 FAILURE = 2,
8 INVALID_PARAM = 3,
9 UNKNOWN = 4
10};
11
12bool testEnumConversion(EStatusC value) {
13 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EStatusC' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
14 return static_cast<bool>(value);
15}
16
17enum class EStatusS : short {
18 SUCCESS = 1,
19 FAILURE = 2,
20 INVALID_PARAM = 3,
21 UNKNOWN = 4
22};
23
24bool testEnumConversion(EStatusS value) {
25 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EStatusS' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
26 return static_cast<bool>(value);
27}
28
29
30enum class EStatusI : int {
31 SUCCESS = 1,
32 FAILURE = 2,
33 INVALID_PARAM = 3,
34 UNKNOWN = 4
35};
36
37bool testEnumConversion(EStatusI value) {
38 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EStatusI' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
39 return static_cast<bool>(value);
40}
41
42enum class EStatus {
43 SUCCESS = 1,
44 FAILURE = 2,
45 INVALID_PARAM = 3,
46 UNKNOWN = 4
47};
48
49bool testEnumConversion(EStatus value) {
50 // 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]
51 return static_cast<bool>(value);
52}
53
54namespace enum_int {
55
56enum EResult : int {
57 OK = 1,
58 NOT_OK
59};
60
61bool testEnumConversion(const EResult& value) {
62 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EResult' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
63 return value;
64}
65
66}
67
68namespace enum_short {
69
70enum EResult : short {
71 OK = 1,
72 NOT_OK
73};
74
75bool testEnumConversion(const EResult& value) {
76 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EResult' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
77 return value;
78}
79
80}
81
82namespace enum_char {
83
84enum EResult : char {
85 OK = 1,
86 NOT_OK
87};
88
89bool testEnumConversion(const EResult& value) {
90 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EResult' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
91 return value;
92}
93
94}
95
96namespace enum_default {
97
98enum EResult {
99 OK = 1,
100 NOT_OK
101};
102
103bool testEnumConversion(const EResult& value) {
104 // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: conversion of 'EResult' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
105 return value;
106}
107
108}
109
110}
111

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion-cpp11.cpp