[all-commits] [llvm/llvm-project] b16fe1: [clang-tidy] Fix bugprone-tagged-union-member-coun...

tigbr via All-commits all-commits at lists.llvm.org
Sun Aug 3 04:33:51 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: b16fe132c68834a601b29c0eb53c7ec25a3cd627
      https://github.com/llvm/llvm-project/commit/b16fe132c68834a601b29c0eb53c7ec25a3cd627
  Author: tigbr <gabor.tothvari at ericsson.com>
  Date:   2025-08-03 (Sun, 03 Aug 2025)

  Changed paths:
    M clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
    M clang-tools-extra/docs/ReleaseNotes.rst
    M clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/stdnamespace.h
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/system/pthread.h
    M clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
    M clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
    M clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
    M clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm

  Log Message:
  -----------
  [clang-tidy] Fix bugprone-tagged-union-member-count false-positive (#135831)

This patch implements a fix for the false-positive case described in
[issue #134840](https://github.com/llvm/llvm-project/issues/134840) for
the
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
clang-tidy check.

The example given in the linked issue was the following:

```C
#include <pthread.h>

typedef enum {
  MYENUM_ONE,
  MYENUM_TWO,
} myEnumT;

typedef struct {
  pthread_mutex_t mtx;       
  myEnumT         my_enum;
} myTypeT;
```

The
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
check emits the following a warning for this struct:

```
<source>:8:9: warning: tagged union has more data members (3) than tags (2)! [bugprone-tagged-union-member-count]
    8 | typedef struct {
      |         ^
1 warning generated.
```

The issue is that `pthread_mutex_t` can be a union behind a typedef like
the following:

```C
typedef union
{
  struct __pthread_mutex_s __data;
  char __size[__SIZEOF_PTHREAD_MUTEX_T];
  long int __align;
} pthread_mutex_t;
```

>From the checker's point of view therefore `myTypeT` contains a data
member with an enum type and another data member with a union type and
starts analyzing it like a user-defined tagged union.

The proposed solution is that the types from system headers and the std
namespace are no longer candidates to be the enum part or the union part
of a user-defined tagged union. This filtering for enums and the std
namespace may not be strictly necessary in this example, however I added
it preemptively out of (perhaps unnecessary) caution.

Fixes https://github.com/llvm/llvm-project/issues/134840.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list