99 lines · cpp
1// RUN: %check_clang_tidy -std=c++17 %s bugprone-suspicious-enum-usage %t -- -config="{CheckOptions: {bugprone-suspicious-enum-usage.StrictMode: true}}" --2 3enum A {4 A = 1,5 B = 2,6 C = 4,7 D = 8,8 E = 16,9 F = 32,10 G = 6311};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-214// CHECK-NOTES: :76:7: note: used here as a bitmask15enum X {16 X = 8,17 Y = 16,18 Z = 4,19 ZZ = 320 // 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 bitmask22};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-224// CHECK-NOTES: :73:8: note: used here as a bitmask25enum 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-229 // CHECK-NOTES: :65:11: note: used here as a bitmask30 R = 4,31 S = 8,32 T = 16,33 U = 3134};35 36enum {37 H,38 I,39 J,40 K,41 L42};43 44enum Days {45 Monday,46 Tuesday,47 Wednesday,48 Thursday,49 Friday,50 Saturday,51 Sunday52};53 54Days bestDay() {55 return Friday;56}57 58int trigger() {59 if (bestDay() | A)60 return 1;61 // CHECK-NOTES: :[[@LINE-2]]:17: warning: enum values are from different enum types62 if (I | Y)63 return 1;64 // CHECK-NOTES: :[[@LINE-2]]:9: warning: enum values are from different enum types65 if (P + Q == R)66 return 1;67 else if ((S | R) == T)68 return 1;69 else70 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 80int 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