• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %check_clang_tidy %s bugprone-suspicious-enum-usage %t -- -config="{CheckOptions: [{key: bugprone-suspicious-enum-usage.StrictMode, value: 1}]}" --
2 
3 enum A {
4   A = 1,
5   B = 2,
6   C = 4,
7   D = 8,
8   E = 16,
9   F = 32,
10   G = 63
11 };
12 
13 // CHECK-NOTES: :[[@LINE+2]]:1: warning: enum type seems like a bitmask (contains mostly power-of-2 literals) but a literal is not power-of-2
14 // CHECK-NOTES: :76:7: note: used here as a bitmask
15 enum X {
16   X = 8,
17   Y = 16,
18   Z = 4,
19   ZZ = 3
20   // CHECK-NOTES: :[[@LINE-1]]:3: warning: enum type seems like a bitmask (contains mostly power-of-2 literals), but this literal is not a power-of-2 [bugprone-suspicious-enum-usage]
21 // CHECK-NOTES: :70:13: note: used here as a bitmask
22 };
23 // CHECK-NOTES: :[[@LINE+2]]:1: warning: enum type seems like a bitmask (contains mostly power-of-2 literals) but some literals are not power-of-2
24 // CHECK-NOTES: :73:8: note: used here as a bitmask
25 enum PP {
26   P = 2,
27   Q = 3,
28   // CHECK-NOTES: :[[@LINE-1]]:3: warning: enum type seems like a bitmask (contains mostly power-of-2 literals), but this literal is not a power-of-2
29   // CHECK-NOTES: :65:11: note: used here as a bitmask
30   R = 4,
31   S = 8,
32   T = 16,
33   U = 31
34 };
35 
36 enum {
37   H,
38   I,
39   J,
40   K,
41   L
42 };
43 
44 enum Days {
45   Monday,
46   Tuesday,
47   Wednesday,
48   Thursday,
49   Friday,
50   Saturday,
51   Sunday
52 };
53 
bestDay()54 Days bestDay() {
55   return Friday;
56 }
57 
trigger()58 int trigger() {
59   if (bestDay() | A)
60     return 1;
61   // CHECK-NOTES: :[[@LINE-2]]:17: warning: enum values are from different enum types
62   if (I | Y)
63     return 1;
64   // CHECK-NOTES: :[[@LINE-2]]:9: warning: enum values are from different enum types
65   if (P + Q == R)
66     return 1;
67   else if ((S | R) == T)
68     return 1;
69   else
70     int k = ZZ | Z;
71   unsigned p = R;
72   PP pp = Q;
73   p |= pp;
74 
75   enum X x = Z;
76   p = x | Z;
77   return 0;
78 }
79 
dont_trigger()80 int dont_trigger() {
81   int a = 1, b = 5;
82   int c = a + b;
83   int d = c | H, e = b * a;
84   a = B | C;
85   b = X | Z;
86 
87   unsigned bitflag;
88   enum A aa = B;
89   bitflag = aa | C;
90 
91   if (Tuesday != Monday + 1 ||
92       Friday - Thursday != 1 ||
93       Sunday + Wednesday == (Sunday | Wednesday))
94     return 1;
95   if (H + I + L == 42)
96     return 1;
97   return 42;
98 }
99