diff --git a/clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp index 07274d0376207..1818e30971c21 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp @@ -77,7 +77,7 @@ matchEnableIfSpecializationImplTypename(TypeLoc TheType) { const TemplateDecl *TD = Specialization->getTemplateName().getAsTemplateDecl(); - if (!TD || TD->getName() != "enable_if") + if (!TD || TD->getName() != "enable_if" || !TD->isInStdNamespace()) return std::nullopt; int NumArgs = SpecializationLoc.getNumArgs(); @@ -101,7 +101,7 @@ matchEnableIfSpecializationImplTrait(TypeLoc TheType) { const TemplateDecl *TD = Specialization->getTemplateName().getAsTemplateDecl(); - if (!TD || TD->getName() != "enable_if_t") + if (!TD || TD->getName() != "enable_if_t" || !TD->isInStdNamespace()) return std::nullopt; if (!Specialization->isTypeAlias()) diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints.cpp index 3bcd5cd74024e..8289efe338e3f 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints.cpp @@ -756,3 +756,33 @@ abs(const number &v) { } } + +// NOLINTBEGIN +namespace custom { +template struct enable_if { }; + +template struct enable_if { typedef T type; }; + +template +using enable_if_t = typename enable_if::type; + +} // namespace custom +// NOLINTEND + +namespace use_custom { +// We cannot assume anything about the behavior of templates that happen to be +// named `enable_if` or `enable_if_t` if they are not declared in the namespace +// `std`. (E.g. the first template parameter of `boost::enable_if` is a class +// and not a boolean and `boost::enable_if` is equivalent to +// `std::enable_if`.) + +template +typename custom::enable_if::type custom_basic() { + return Obj{}; +} + +template +custom::enable_if_t custom_basic_t() { + return Obj{}; +} +}