[clang-tools-extra] [clang-tidy] Fix bugprone-tagged-union-member-count false-positive (PR #135831)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 24 07:12:57 PDT 2025
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/135831 at github.com>
https://github.com/tigbr updated https://github.com/llvm/llvm-project/pull/135831
>From 61847fc005370b3083be8e5a4f82ee08a0f091d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/8] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++++++----
.../bugprone/tagged-union-member-count.rst | 22 +++++++++++++++++++
.../bugprone/tagged-union-member-count.c | 13 +++++++++++
.../bugprone/tagged-union-member-count.cpp | 13 +++++++++++
.../bugprone/tagged-union-member-count.m | 13 +++++++++++
.../bugprone/tagged-union-member-count.mm | 13 +++++++++++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index c1ea63cda5003..ddbb14e3ac62b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -105,11 +105,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion())))))));
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()))))));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)))))));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+ enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)))))));
auto HasOneUnionField = fieldCountOfKindIsOne(UnionField, UnionMatchBindName);
auto HasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ 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.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include <pthread.h>
+
+ struct NotTaggedUnion {
+ enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+ pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
------------------------------
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+ MyEnum
+ } EnumField;
+};
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
index 25827e8c8de0c..f21c23b87ae44 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
@@ -308,3 +308,16 @@ void DoNotMatchLambdas() {
} u;
auto L = [e, u] () {};
}
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+ MyEnum
+ } EnumField;
+};
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
index 60c93c553baca..96255c7fdd4fe 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
@@ -147,3 +147,16 @@
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+ MyEnum
+ } EnumField;
+};
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
index 8b308555281c5..b169b5cd480b5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
@@ -307,3 +307,16 @@ void DoNotMatchLambdas() {
} u;
auto L = [e, u] () {};
}
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+ MyEnum
+ } EnumField;
+};
>From 7c3f0b29d71df86f4efd71e4ee9dd0f2ce33dd0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Thu, 17 Apr 2025 10:54:23 +0200
Subject: [PATCH 2/8] Update release notes and improve formatting in
documentation.
---
clang-tools-extra/docs/ReleaseNotes.rst | 13 +++++++++++++
.../checks/bugprone/tagged-union-member-count.rst | 8 ++++----
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index edab04730cb25..caf15e21a5c8c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -110,6 +110,19 @@ Changes in existing checks
<clang-tidy/checks/bugprone/signed-char-misuse>` check by fixing
false positives on C23 enums with the fixed underlying type of signed char.
+- Improved :doc:`bugprone-tagged-union-member-count
+ <clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
+ positive when typedefed enums or unions from system header files or the
+ ``std`` namespace are treated as the tag or the data part of a user-defined
+ tagged union respectively.
+
+- Improved :doc:`bugprone-unchecked-optional-access
+ <clang-tidy/checks/bugprone/unchecked-optional-access>` fixing false
+ positives from smart pointer accessors repeated in checking ``has_value``
+ and accessing ``value``. The option `IgnoreSmartPointerDereference` should
+ no longer be needed and will be removed. Also fixing false positive from
+ const reference accessors to objects containing optional member.
+
- Improved :doc:`bugprone-unhandled-self-assignment
<clang-tidy/checks/bugprone/unhandled-self-assignment>` check by adding
an additional matcher that generalizes the copy-and-swap idiom pattern
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index b47a49543143b..cdfe341f42dba 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -10,7 +10,7 @@ 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.
The union and enum data members that are from system header files or
-the std namespace are not considered to make up the tagged union part
+the ``std`` namespace are not considered to make up the tagged union part
of a user-defined tagged union type.
Example:
@@ -32,7 +32,7 @@ Example:
};
The following example illustrates the exception for unions and enums from
-system header files and the std namespace.
+system header files and the ``std`` namespace.
.. code-block:: c++
@@ -43,11 +43,11 @@ system header files and the std namespace.
pthread_mutex_t Mutex;
};
-The pthread_mutex_t type may be defined as a union behind a typedef,
+The ``pthread_mutex_t`` type may be defined as a union behind a typedef,
in which case the check could mistake this type as a user-defined tagged union.
After all it has exactly one enum data member and exactly one union data member.
To avoid false-positive cases originating from this, unions and enums from
-system headers and the std namespace are ignored when pinpointing the
+system headers and the ``std`` namespace are ignored when pinpointing the
union part and the enum part of a potential user-defined tagged union.
How enum constants are counted
>From bad89cb20d6b5d995dddc53f8d1b7756ad337936 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Thu, 17 Apr 2025 13:37:07 +0200
Subject: [PATCH 3/8] Properly stub the pthread.h header in the tests.
---
.../Inputs/tagged-union-member-count/system/pthread.h | 10 ++++++++++
.../checkers/bugprone/tagged-union-member-count.c | 5 +++--
.../checkers/bugprone/tagged-union-member-count.cpp | 5 +++--
.../checkers/bugprone/tagged-union-member-count.m | 5 +++--
.../checkers/bugprone/tagged-union-member-count.mm | 5 +++--
5 files changed, 22 insertions(+), 8 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/system/pthread.h
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/system/pthread.h b/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/system/pthread.h
new file mode 100644
index 0000000000000..43aa224ebf5eb
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/system/pthread.h
@@ -0,0 +1,10 @@
+#define __SIZEOF_PTHREAD_MUTEX_T 40
+
+typedef union {
+ struct __pthread_mutex_s {
+ int __lock;
+ unsigned int __count;
+ } __data;
+ char __size[__SIZEOF_PTHREAD_MUTEX_T];
+ long int __align;
+} pthread_mutex_t;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 96255c7fdd4fe..79a86cd675cea 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -1,4 +1,5 @@
-// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t
+// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t -- -- \
+// RUN: -isystem %S/Inputs/tagged-union-member-count/system
typedef enum Tags3 {
tags3_1,
@@ -150,7 +151,7 @@ DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
// Typedefed unions from system header files should be ignored when
// we are trying to pinpoint the union part in a user-defined tagged union.
-#include "pthread.h"
+#include <pthread.h>
// This should not be analyzed as a user-defined tagged union,
// even though pthread_mutex_t may be declared as a typedefed union.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
index f21c23b87ae44..465667b896e63 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
@@ -1,4 +1,5 @@
-// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-tagged-union-member-count %t
+// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-tagged-union-member-count %t -- -- \
+// RUN: -isystem %S/Inputs/tagged-union-member-count/system
// Test check with C++ features
typedef enum Tags3 {
@@ -311,7 +312,7 @@ void DoNotMatchLambdas() {
// Typedefed unions from system header files should be ignored when
// we are trying to pinpoint the union part in a user-defined tagged union.
-#include "pthread.h"
+#include <pthread.h>
// This should not be analyzed as a user-defined tagged union,
// even though pthread_mutex_t may be declared as a typedefed union.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
index 96255c7fdd4fe..79a86cd675cea 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
@@ -1,4 +1,5 @@
-// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t
+// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t -- -- \
+// RUN: -isystem %S/Inputs/tagged-union-member-count/system
typedef enum Tags3 {
tags3_1,
@@ -150,7 +151,7 @@
// Typedefed unions from system header files should be ignored when
// we are trying to pinpoint the union part in a user-defined tagged union.
-#include "pthread.h"
+#include <pthread.h>
// This should not be analyzed as a user-defined tagged union,
// even though pthread_mutex_t may be declared as a typedefed union.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
index b169b5cd480b5..531b10becb2e3 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
@@ -1,4 +1,5 @@
-// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t
+// RUN: %check_clang_tidy %s bugprone-tagged-union-member-count %t -- -- \
+// RUN: -isystem %S/Inputs/tagged-union-member-count/system
typedef enum Tags3 {
tags3_1,
@@ -310,7 +311,7 @@ void DoNotMatchLambdas() {
// Typedefed unions from system header files should be ignored when
// we are trying to pinpoint the union part in a user-defined tagged union.
-#include "pthread.h"
+#include <pthread.h>
// This should not be analyzed as a user-defined tagged union,
// even though pthread_mutex_t may be declared as a typedefed union.
>From 5f888b46d5347e2d229ecca0d8ae0b1739b14f41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Sat, 19 Apr 2025 10:09:53 +0200
Subject: [PATCH 4/8] Create test case for when the enum or the union comes
from the std namespace but not a system header.
---
.../Inputs/tagged-union-member-count/stdnamespace.h | 12 ++++++++++++
.../checkers/bugprone/tagged-union-member-count.cpp | 11 +++++++++++
2 files changed, 23 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/stdnamespace.h
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/stdnamespace.h b/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/stdnamespace.h
new file mode 100644
index 0000000000000..4f6eafde8501e
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/tagged-union-member-count/stdnamespace.h
@@ -0,0 +1,12 @@
+#define __SIZEOF_PTHREAD_MUTEX_T 40
+
+namespace std {
+ typedef union {
+ struct __pthread_mutex_s {
+ int __lock;
+ unsigned int __count;
+ } __data;
+ char __size[__SIZEOF_PTHREAD_MUTEX_T];
+ long int __align;
+ } pthread_mutex_t;
+};
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
index 465667b896e63..c8e36bc6e1a44 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
@@ -1,4 +1,5 @@
// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-tagged-union-member-count %t -- -- \
+// RUN: -I%S/Inputs/tagged-union-member-count \
// RUN: -isystem %S/Inputs/tagged-union-member-count/system
// Test check with C++ features
@@ -322,3 +323,13 @@ struct SystemTypedefedUnionDataMemberShouldBeIgnored {
MyEnum
} EnumField;
};
+
+// Filter when union or enum comes from the std namespace but not a system header
+#include "stdnamespace.h"
+
+struct StdNameSpaceUnionDataMemberShouldBeIgnored {
+ std::pthread_mutex_t Mutex;
+ enum {
+ MyEnum
+ } EnumField;
+};
>From f4d0b351f9b4766d016dff5d746633e6cb271d54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Sat, 26 Apr 2025 22:29:52 +0200
Subject: [PATCH 5/8] Use code formatting style on typedef word in
documentation.
---
.../clang-tidy/checks/bugprone/tagged-union-member-count.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index cdfe341f42dba..4bf4ca9eec17b 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -43,7 +43,7 @@ system header files and the ``std`` namespace.
pthread_mutex_t Mutex;
};
-The ``pthread_mutex_t`` type may be defined as a union behind a typedef,
+The ``pthread_mutex_t`` type may be defined as a union behind a ``typedef``,
in which case the check could mistake this type as a user-defined tagged union.
After all it has exactly one enum data member and exactly one union data member.
To avoid false-positive cases originating from this, unions and enums from
>From 6e2d9a619fbf703b78f79d7656e58c1f21d8a5c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Wed, 23 Jul 2025 10:36:54 +0200
Subject: [PATCH 6/8] Update the description of the checks behavior to be more
accurate in the documentation, release notes and a test file.
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
.../checks/bugprone/tagged-union-member-count.rst | 7 +++----
.../checkers/bugprone/tagged-union-member-count.c | 4 ++--
3 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index caf15e21a5c8c..4378565e7e425 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -112,8 +112,8 @@ Changes in existing checks
- Improved :doc:`bugprone-tagged-union-member-count
<clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
- positive when typedefed enums or unions from system header files or the
- ``std`` namespace are treated as the tag or the data part of a user-defined
+ positive when enums or unions from system header files or the ``std``
+ namespace are treated as the tag or the data part of a user-defined
tagged union respectively.
- Improved :doc:`bugprone-unchecked-optional-access
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 4bf4ca9eec17b..5bcfa68ee5d2e 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,9 +9,8 @@ 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.
-The union and enum data members that are from system header files or
-the ``std`` namespace are not considered to make up the tagged union part
-of a user-defined tagged union type.
+Furthermore, the types of the union and the enum members must
+not come from a system header files or the ``std`` namespace
Example:
@@ -45,7 +44,7 @@ system header files and the ``std`` namespace.
The ``pthread_mutex_t`` type may be defined as a union behind a ``typedef``,
in which case the check could mistake this type as a user-defined tagged union.
-After all it has exactly one enum data member and exactly one union data member.
+After all, it has exactly one enum data member and exactly one union data member.
To avoid false-positive cases originating from this, unions and enums from
system headers and the ``std`` namespace are ignored when pinpointing the
union part and the enum part of a potential user-defined tagged union.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 79a86cd675cea..f78a05f54a15e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -149,12 +149,12 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
-// Typedefed unions from system header files should be ignored when
+// Unions from system header files should be ignored when
// we are trying to pinpoint the union part in a user-defined tagged union.
#include <pthread.h>
// This should not be analyzed as a user-defined tagged union,
-// even though pthread_mutex_t may be declared as a typedefed union.
+// even though pthread_mutex_t could be a union.
struct SystemTypedefedUnionDataMemberShouldBeIgnored {
pthread_mutex_t Mutex;
enum {
>From cea53d471f545cbc304cd3037977e220e056401f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Thu, 24 Jul 2025 16:06:36 +0200
Subject: [PATCH 7/8] Remove rebase artifact.
---
clang-tools-extra/docs/ReleaseNotes.rst | 7 -------
1 file changed, 7 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 4378565e7e425..d32bf11120c12 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -116,13 +116,6 @@ Changes in existing checks
namespace are treated as the tag or the data part of a user-defined
tagged union respectively.
-- Improved :doc:`bugprone-unchecked-optional-access
- <clang-tidy/checks/bugprone/unchecked-optional-access>` fixing false
- positives from smart pointer accessors repeated in checking ``has_value``
- and accessing ``value``. The option `IgnoreSmartPointerDereference` should
- no longer be needed and will be removed. Also fixing false positive from
- const reference accessors to objects containing optional member.
-
- Improved :doc:`bugprone-unhandled-self-assignment
<clang-tidy/checks/bugprone/unhandled-self-assignment>` check by adding
an additional matcher that generalizes the copy-and-swap idiom pattern
>From 8b314c1f591b42514219707d4c6a05812d81df5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
<gabor.tothvari at ericsson.com>
Date: Thu, 24 Jul 2025 16:12:34 +0200
Subject: [PATCH 8/8] Fix wording in documentation.
---
.../clang-tidy/checks/bugprone/tagged-union-member-count.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 5bcfa68ee5d2e..072b5a3eee20f 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -10,7 +10,7 @@ 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.
Furthermore, the types of the union and the enum members must
-not come from a system header files or the ``std`` namespace
+not come from system header files nor the ``std`` namespace.
Example:
More information about the cfe-commits
mailing list