[clang-tools-extra] [clang-tidy] Add new check bugprone-tagged-union-member-count (PR #89925)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 24 08:40:46 PDT 2024


================
@@ -0,0 +1,181 @@
+// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t
+
+enum tags3 {
+	tags3_1,
+	tags3_2,
+	tags3_3,
+};
+
+enum tags4 {
+	tags4_1,
+	tags4_2,
+	tags4_3,
+	tags4_4,
+};
+
+enum tags5 {
+	tags5_1,
+	tags5_2,
+	tags5_3,
+	tags5_4,
+	tags5_5,
+};
+
+union union3 {
+	short *shorts;
+	int *ints;
+	float *floats;
+};
+
+union union4 {
+	short *shorts;
+	double *doubles;
+	int *ints;
+	float *floats;
+};
+
+// It is not obvious which enum is the tag for the union.
+struct taggedunion2 { // No warnings expected.
+	enum tags3 tagA;
+	enum tags4 tagB;
+	union union4 data;
+};
+
+// It is not obvious which union does the tag belong to.
+struct taggedunion4 { // No warnings expected.
+	enum tags3 tag;
+	union union3 dataB;
+	union union3 dataA;
+};
+
+struct taggedunion1 { // CHECK-MESSAGES: :[[@LINE]]:8: warning: Tagged union has more data members than tags! Data members: 4 Tags: 3 [bugprone-tagged-union-member-count]
----------------
PiotrZSL wrote:

put those logs in one line up, just simply use `[[@LINE+1]]`

https://github.com/llvm/llvm-project/pull/89925


More information about the cfe-commits mailing list