[clang-tools-extra] [clang-tidy] Add new check bugprone-tagged-union-member-count (PR #89925)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 23 02:06:15 PDT 2024
º×üm
½ÈSÍÑ¡Û
ɤ¶(Êzè¶æj)\¢c²Æ xÈ©$è[梸?[æþYoèç-þ¥ÿ=÷n`ØnmÊ&
================
@@ -0,0 +1,216 @@
+.. title:: clang-tidy - bugprone-tagged-union-member-count
+
+bugprone-tagged-union-member-count
+==================================
+
+Gives warnings for tagged unions, where the number of tags is
+different from the number of data members inside the union.
+
+A struct or a class is considered to be a tagged union if it has
+exactly one union data member and exactly one enum data member and
+any number of other data members that are neither unions or enums.
+
+Example
+-------
+
+.. code-block:: c++
+
+ enum tags {
+ tag1,
+ tag2,
+ };
+
+ struct taggedUnion { // warning: Tagged union has more data members (3) than tags (2)! [bugprone-tagged-union-member-count]
+ enum tags kind;
+ union {
+ int i;
+ float f;
+ char *str;
+ } data;
+ };
+
+Counting enum constant heuristic
+--------------------------------
+
+Some programmers use enums in such a way, where the value of the last enum
+constant is used to keep track of how many enum constants have been declared.
+
+.. code-block:: c++
+
+ enum tags_with_counter {
+ tag1, // is 0
+ tag2, // is 1
+ tag3, // is 2
+ tags_count, // is 3
----------------
whisperity wrote:
You should extend the patch with supporting a **prefix** for the tag counting enum constant. In various projects it is easy to observe enums like `LAST_Foo`.
https://github.com/llvm/llvm-project/pull/89925
More information about the cfe-commits
mailing list