Skip to content

Commit 3911696

Browse files
committed
Remove check for zero template arguments, handle enable_if_t
1 parent a189367 commit 3911696

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ matchEnableIfSpecializationImplTypename(TypeLoc TheType) {
7979
if (!TD || TD->getName() != "enable_if")
8080
return std::nullopt;
8181

82-
const TemplateParameterList *Params = TD->getTemplateParameters();
83-
if (Params->size() != 2)
84-
return std::nullopt;
85-
86-
const auto *FirstParam =
87-
dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0));
82+
const auto *FirstParam = dyn_cast<NonTypeTemplateParmDecl>(
83+
TD->getTemplateParameters()->getParam(0));
8884
if (!FirstParam || !FirstParam->getType()->isBooleanType())
8985
return std::nullopt;
9086

@@ -118,6 +114,11 @@ matchEnableIfSpecializationImplTrait(TypeLoc TheType) {
118114
if (!Specialization->isTypeAlias())
119115
return std::nullopt;
120116

117+
const auto *FirstParam = dyn_cast<NonTypeTemplateParmDecl>(
118+
TD->getTemplateParameters()->getParam(0));
119+
if (!FirstParam || !FirstParam->getType()->isBooleanType())
120+
return std::nullopt;
121+
121122
if (const auto *AliasedType =
122123
dyn_cast<DependentNameType>(Specialization->getAliasedType())) {
123124
ElaboratedTypeKeyword Keyword = AliasedType->getKeyword();

clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,20 +768,34 @@ namespace nonstd {
768768
template <bool Condition, typename T = void>
769769
struct enable_if : std::enable_if<Condition, T> {};
770770

771+
template <bool Condition, typename T = void>
772+
using enable_if_t = typename enable_if<Condition, T>::type;
773+
771774
}
772775

773776
template <typename T>
774777
typename nonstd::enable_if<some_type_trait<T>::value, void>::type nonstd_enable_if() {}
775778
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use C++20 requires constraints instead of enable_if [modernize-use-constraints]
776779
// CHECK-FIXES: {{^}}void nonstd_enable_if() requires some_type_trait<T>::value {}{{$}}
777780

781+
template <typename T>
782+
nonstd::enable_if_t<some_type_trait<T>::value, void> nonstd_enable_if_t() {}
783+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use C++20 requires constraints instead of enable_if [modernize-use-constraints]
784+
// CHECK-FIXES: {{^}}void nonstd_enable_if_t() requires some_type_trait<T>::value {}{{$}}
785+
778786
// But only if the non-standard enable_if has the same signature as the standard one.
779787
namespace boost {
780788

781789
template <typename Condition, typename T = void>
782790
struct enable_if : std::enable_if<Condition::value, T> {};
783791

792+
template <typename Condition, typename T = void>
793+
using enable_if_t = typename enable_if<Condition, T>::type;
794+
784795
}
785796

786797
template <typename T>
787798
typename boost::enable_if<some_type_trait<T>, void>::type boost_enable_if() {}
799+
800+
template <typename T>
801+
boost::enable_if_t<some_type_trait<T>, void> boost_enable_if_t() {}

0 commit comments

Comments
 (0)