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

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 7 07:00:27 PDT 2024


=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/89925 at github.com>


================
@@ -132,11 +157,11 @@ size_t TaggedUnionMemberCountCheck::getNumberOfValidEnumValues(
   // 3. The value of the counting enum constant is the largest out of every enum
   //    constant.
----------------
isuckatcs wrote:

So, if I understand it correctly, what we really care about is whether the value of the last enumerator equals to the number of the other enumerators.

E.g.:
```c++
enum tag_with_counter {
  tag1 = 100,
  tag2 = 200,
  tag_count = 2
};
```
In this example `tag_count` is still a counter, but point 3. doesn't apply, so the heuristics doesn't find it.

```c++
enum tag_with_counter {
  tag1 = 2,
  tag2 = 3,
  tag_count = 2
};
```
In this other example, the last enumerator is still a counter, but point 1. doesn't apply (?).

```c++
enum tag_with_counter {
  tag1,
  tag2,
  tag3
};
```
In this last example however all points apply, so the heuristics detects the last enumerator as a counter, when it's not one.

If you really want this heuristics, I suggest replacing this function with a simple check for the value of the last enumerator being equal to the number of the other enumerators. You use the name of the enumerator to determine whether it's a counter anyway.

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


More information about the cfe-commits mailing list