[clang-tools-extra] [clang-tidy] Teach `modernize-type-traits` about more type traits (PR #147074)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 4 11:37:43 PDT 2025
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/147074
>From 7ad6f7b8663980183e8bb8e79843942870e36267 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 4 Jul 2025 07:42:54 -0700
Subject: [PATCH 1/5] [clang-tidy] Teach `modernize-type-traits` about more
type traits
---
.../clang-tidy/modernize/TypeTraitsCheck.cpp | 27 +++++++++++++++++++
clang-tools-extra/docs/ReleaseNotes.rst | 3 +++
2 files changed, 30 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
index c0766395ec5cc..92e6f61a6d9d4 100644
--- a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
@@ -28,8 +28,10 @@ static const llvm::StringSet<> ValueTraits = {
"is_array",
"is_assignable",
"is_base_of",
+ "is_bind_expression",
"is_bounded_array",
"is_class",
+ "is_clock",
"is_compound",
"is_const",
"is_constructible",
@@ -40,10 +42,14 @@ static const llvm::StringSet<> ValueTraits = {
"is_destructible",
"is_empty",
"is_enum",
+ "is_error_code_enum",
+ "is_error_condition_enum",
+ "is_execution_policy",
"is_final",
"is_floating_point",
"is_function",
"is_fundamental",
+ "is_implicit_lifetime",
"is_integral",
"is_invocable",
"is_invocable_r",
@@ -65,14 +71,17 @@ static const llvm::StringSet<> ValueTraits = {
"is_nothrow_invocable_r",
"is_nothrow_move_assignable",
"is_nothrow_move_constructible",
+ "is_nothrow_relocatable",
"is_nothrow_swappable",
"is_nothrow_swappable_with",
"is_null_pointer",
"is_object",
+ "is_placeholder",
"is_pointer",
"is_pointer_interconvertible_base_of",
"is_polymorphic",
"is_reference",
+ "is_replaceable",
"is_rvalue_reference",
"is_same",
"is_scalar",
@@ -91,15 +100,27 @@ static const llvm::StringSet<> ValueTraits = {
"is_trivially_destructible",
"is_trivially_move_assignable",
"is_trivially_move_constructible",
+ "is_trivially_relocatable",
"is_unbounded_array",
"is_union",
"is_unsigned",
+ "is_virtual_base_of",
"is_void",
"is_volatile",
"negation",
"rank",
+ "ratio_equal",
+ "ratio_greater_equal",
+ "ratio_greater",
+ "ratio_less_equal",
+ "ratio_less",
+ "ratio_not_equal",
"reference_constructs_from_temporary",
"reference_converts_from_temporary",
+ "treat_as_floating_point",
+ "tuple_size",
+ "uses_allocator",
+ "variant_size",
};
static const llvm::StringSet<> TypeTraits = {
@@ -130,6 +151,12 @@ static const llvm::StringSet<> TypeTraits = {
"result_of",
"invoke_result",
"type_identity",
+ "tuple_element",
+ "variant_alternative",
+ "compare_three_way_result",
+ "common_comparison_category",
+ "unwrap_ref_decay",
+ "unwrap_reference",
};
static DeclarationName getName(const DependentScopeDeclRefExpr &D) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f8f183e9de1cc..3d554733ad3fd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -276,6 +276,9 @@ Changes in existing checks
excluding variables with ``thread_local`` storage class specifier from being
matched.
+- Improved :doc:`modernize-type-traits
+ <clang-tidy/checks/modernize/type-traits>` check by detecting more type traits.
+
- Improved :doc:`modernize-use-default-member-init
<clang-tidy/checks/modernize/use-default-member-init>` check by matching
arithmetic operations, ``constexpr`` and ``static`` values, and detecting
>From 930db523cdbe6bf44b334a17667229e3586981aa Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 4 Jul 2025 07:59:02 -0700
Subject: [PATCH 2/5] Whoops, didn't exclude `treat_as_floating_point_v` or
`is_clock_v`
---
clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
index 92e6f61a6d9d4..aaf9d08c63b2e 100644
--- a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
@@ -31,7 +31,6 @@ static const llvm::StringSet<> ValueTraits = {
"is_bind_expression",
"is_bounded_array",
"is_class",
- "is_clock",
"is_compound",
"is_const",
"is_constructible",
@@ -117,7 +116,6 @@ static const llvm::StringSet<> ValueTraits = {
"ratio_not_equal",
"reference_constructs_from_temporary",
"reference_converts_from_temporary",
- "treat_as_floating_point",
"tuple_size",
"uses_allocator",
"variant_size",
>From 13dcaabd66610972db0e6e52e49bb63878e248a6 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 4 Jul 2025 08:15:53 -0700
Subject: [PATCH 3/5] clang-format
---
.../clang-tidy/modernize/TypeTraitsCheck.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
index aaf9d08c63b2e..c1024d8eec8b7 100644
--- a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
@@ -149,12 +149,12 @@ static const llvm::StringSet<> TypeTraits = {
"result_of",
"invoke_result",
"type_identity",
- "tuple_element",
- "variant_alternative",
- "compare_three_way_result",
- "common_comparison_category",
- "unwrap_ref_decay",
- "unwrap_reference",
+ "tuple_element",
+ "variant_alternative",
+ "compare_three_way_result",
+ "common_comparison_category",
+ "unwrap_ref_decay",
+ "unwrap_reference",
};
static DeclarationName getName(const DependentScopeDeclRefExpr &D) {
>From 52df9697be39e8e6a314e2947fb29d2852c6cf04 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 4 Jul 2025 11:00:31 -0700
Subject: [PATCH 4/5] Test new standards, exclude `tuple_element_t`
---
clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp | 6 +++++-
.../test/clang-tidy/checkers/modernize/type-traits.cpp | 2 +-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
index c1024d8eec8b7..05c4c8f5d098b 100644
--- a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
@@ -15,6 +15,9 @@ using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
+// FIXME: Add chrono::treat_as_floating_point_v and chrono::is_clock_v.
+// This will require restructuring the code to handle type traits not
+// defined directly in std.
static const llvm::StringSet<> ValueTraits = {
"alignment_of",
"conjunction",
@@ -121,6 +124,8 @@ static const llvm::StringSet<> ValueTraits = {
"variant_size",
};
+// FIXME: Add tuple_element_t. This is a recursively-defined type trait, so
+// you'll have to ensure the check doesn't fire within the trait's definition.
static const llvm::StringSet<> TypeTraits = {
"remove_cv",
"remove_const",
@@ -149,7 +154,6 @@ static const llvm::StringSet<> TypeTraits = {
"result_of",
"invoke_result",
"type_identity",
- "tuple_element",
"variant_alternative",
"compare_three_way_result",
"common_comparison_category",
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/type-traits.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/type-traits.cpp
index eaec70814d4f1..f4e96d95f9172 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/type-traits.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/type-traits.cpp
@@ -1,7 +1,7 @@
// RUN: %check_clang_tidy -std=c++14 %s modernize-type-traits %t -check-suffixes=',MACRO'
// RUN: %check_clang_tidy -std=c++14 %s modernize-type-traits %t -- \
// RUN: -config='{CheckOptions: {modernize-type-traits.IgnoreMacros: true}}'
-// RUN: %check_clang_tidy -std=c++17 %s modernize-type-traits %t -check-suffixes=',CXX17,MACRO,CXX17MACRO'
+// RUN: %check_clang_tidy -std=c++17-or-later %s modernize-type-traits %t -check-suffixes=',CXX17,MACRO,CXX17MACRO'
namespace std {
template <typename>
>From 522e9963a33aa025a7c09014b30df6581cd84cc0 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 4 Jul 2025 11:35:05 -0700
Subject: [PATCH 5/5] Exclude `variant_alternative_t`, document limitations
---
clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp | 6 +++---
.../docs/clang-tidy/checks/modernize/type-traits.rst | 7 +++++++
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
index 05c4c8f5d098b..86741c98e8a42 100644
--- a/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp
@@ -124,8 +124,9 @@ static const llvm::StringSet<> ValueTraits = {
"variant_size",
};
-// FIXME: Add tuple_element_t. This is a recursively-defined type trait, so
-// you'll have to ensure the check doesn't fire within the trait's definition.
+// FIXME: Add tuple_element_t and variant_alternative_t. These are recursively
+// defined type traits, so you'll have to ensure the check doesn't fire within
+// the trait's definition.
static const llvm::StringSet<> TypeTraits = {
"remove_cv",
"remove_const",
@@ -154,7 +155,6 @@ static const llvm::StringSet<> TypeTraits = {
"result_of",
"invoke_result",
"type_identity",
- "variant_alternative",
"compare_three_way_result",
"common_comparison_category",
"unwrap_ref_decay",
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/type-traits.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/type-traits.rst
index c2abde856c90f..0a50e9a4cae6b 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/type-traits.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/type-traits.rst
@@ -38,3 +38,10 @@ Options
#define IS_SIGNED(T) std::is_signed<T>::value
Defaults to `false`.
+
+Limitations
+-----------
+
+Does not currently diagnose uses of ``std::chrono::is_clock``,
+``std::chrono::treat_as_floating_point``, ``std::tuple_element``,
+or ``std::variant_alternative``.
More information about the cfe-commits
mailing list