Skip to content

Commit fbc4255

Browse files
authored
[clang-tidy] Teach modernize-type-traits about more type traits (#147074)
These new traits come from various standard versions: C++14: - `tuple_element_t` C++17: - `is_placeholder_v` - `is_bind_expression_v` - `ratio_equal_v` - `ratio_not_equal_v` - `ratio_less_v` - `ratio_less_equal_v` - `ratio_greater_v` - `ratio_greater_equal_v` - `is_error_code_enum_v` - `is_error_condition_enum_v` - `is_execution_policy_v` - `tuple_size_v` - `variant_size_v` - `uses_allocator_v` - `variant_alternative_t` C++20: - `compare_three_way_result_t` - `common_comparison_category_t` - `unwrap_ref_decay_t` - `unwrap_reference_t` C++23: - `is_implicit_lifetime_v` C++26: - `is_nothrow_relocatable_v` - `is_replaceable_v` - `is_trivially_relocatable_v` - `is_virtual_base_of_v` This doesn't add `treat_as_floating_point_v` or `is_clock_v` because they require more invasive changes; instead I've opened #147072 to track them.
1 parent 7a6568d commit fbc4255

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

clang-tools-extra/clang-tidy/modernize/TypeTraitsCheck.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ using namespace clang::ast_matchers;
1515

1616
namespace clang::tidy::modernize {
1717

18+
// FIXME: Add chrono::treat_as_floating_point_v and chrono::is_clock_v.
19+
// This will require restructuring the code to handle type traits not
20+
// defined directly in std.
1821
static const llvm::StringSet<> ValueTraits = {
1922
"alignment_of",
2023
"conjunction",
@@ -28,6 +31,7 @@ static const llvm::StringSet<> ValueTraits = {
2831
"is_array",
2932
"is_assignable",
3033
"is_base_of",
34+
"is_bind_expression",
3135
"is_bounded_array",
3236
"is_class",
3337
"is_compound",
@@ -40,10 +44,14 @@ static const llvm::StringSet<> ValueTraits = {
4044
"is_destructible",
4145
"is_empty",
4246
"is_enum",
47+
"is_error_code_enum",
48+
"is_error_condition_enum",
49+
"is_execution_policy",
4350
"is_final",
4451
"is_floating_point",
4552
"is_function",
4653
"is_fundamental",
54+
"is_implicit_lifetime",
4755
"is_integral",
4856
"is_invocable",
4957
"is_invocable_r",
@@ -65,14 +73,17 @@ static const llvm::StringSet<> ValueTraits = {
6573
"is_nothrow_invocable_r",
6674
"is_nothrow_move_assignable",
6775
"is_nothrow_move_constructible",
76+
"is_nothrow_relocatable",
6877
"is_nothrow_swappable",
6978
"is_nothrow_swappable_with",
7079
"is_null_pointer",
7180
"is_object",
81+
"is_placeholder",
7282
"is_pointer",
7383
"is_pointer_interconvertible_base_of",
7484
"is_polymorphic",
7585
"is_reference",
86+
"is_replaceable",
7687
"is_rvalue_reference",
7788
"is_same",
7889
"is_scalar",
@@ -91,15 +102,26 @@ static const llvm::StringSet<> ValueTraits = {
91102
"is_trivially_destructible",
92103
"is_trivially_move_assignable",
93104
"is_trivially_move_constructible",
105+
"is_trivially_relocatable",
94106
"is_unbounded_array",
95107
"is_union",
96108
"is_unsigned",
109+
"is_virtual_base_of",
97110
"is_void",
98111
"is_volatile",
99112
"negation",
100113
"rank",
114+
"ratio_equal",
115+
"ratio_greater_equal",
116+
"ratio_greater",
117+
"ratio_less_equal",
118+
"ratio_less",
119+
"ratio_not_equal",
101120
"reference_constructs_from_temporary",
102121
"reference_converts_from_temporary",
122+
"tuple_size",
123+
"uses_allocator",
124+
"variant_size",
103125
};
104126

105127
static const llvm::StringSet<> TypeTraits = {
@@ -130,6 +152,12 @@ static const llvm::StringSet<> TypeTraits = {
130152
"result_of",
131153
"invoke_result",
132154
"type_identity",
155+
"compare_three_way_result",
156+
"common_comparison_category",
157+
"unwrap_ref_decay",
158+
"unwrap_reference",
159+
"tuple_element",
160+
"variant_alternative",
133161
};
134162

135163
static DeclarationName getName(const DependentScopeDeclRefExpr &D) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ Changes in existing checks
281281
excluding variables with ``thread_local`` storage class specifier from being
282282
matched.
283283

284+
- Improved :doc:`modernize-type-traits
285+
<clang-tidy/checks/modernize/type-traits>` check by detecting more type traits.
286+
284287
- Improved :doc:`modernize-use-default-member-init
285288
<clang-tidy/checks/modernize/use-default-member-init>` check by matching
286289
arithmetic operations, ``constexpr`` and ``static`` values, and detecting

clang-tools-extra/docs/clang-tidy/checks/modernize/type-traits.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ Options
3838
#define IS_SIGNED(T) std::is_signed<T>::value
3939

4040
Defaults to `false`.
41+
42+
Limitations
43+
-----------
44+
45+
Does not currently diagnose uses of type traits with nested name
46+
specifiers (e.g. ``std::chrono::is_clock``,
47+
``std::chrono::treat_as_floating_point``).

clang-tools-extra/test/clang-tidy/checkers/modernize/type-traits.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %check_clang_tidy -std=c++14 %s modernize-type-traits %t -check-suffixes=',MACRO'
22
// RUN: %check_clang_tidy -std=c++14 %s modernize-type-traits %t -- \
33
// RUN: -config='{CheckOptions: {modernize-type-traits.IgnoreMacros: true}}'
4-
// RUN: %check_clang_tidy -std=c++17 %s modernize-type-traits %t -check-suffixes=',CXX17,MACRO,CXX17MACRO'
4+
// RUN: %check_clang_tidy -std=c++17-or-later %s modernize-type-traits %t -check-suffixes=',CXX17,MACRO,CXX17MACRO'
55

66
namespace std {
77
template <typename>
@@ -19,6 +19,11 @@ namespace std {
1919
using type = T;
2020
};
2121

22+
template <typename...>
23+
struct common_type {
24+
using type = int;
25+
};
26+
2227
inline namespace __std_lib_version1 {
2328
template<typename T>
2429
struct add_const {
@@ -66,6 +71,10 @@ using UsingNoTypename = std::enable_if<true>::type;
6671
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use c++14 style type templates
6772
// CHECK-FIXES: using UsingNoTypename = std::enable_if_t<true>;
6873

74+
using VariadicTrait = std::common_type<int, long, bool>::type;
75+
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use c++14 style type templates
76+
// CHECK-FIXES: using VariadicTrait = std::common_type_t<int, long, bool>;
77+
6978
using UsingSpace = std::enable_if <true>::type;
7079
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use c++14 style type templates
7180
// CHECK-FIXES: using UsingSpace = std::enable_if_t <true>;

0 commit comments

Comments
 (0)