https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/185797
None >From 4d731b096a5b0c69f5817a83b3102abf82b17325 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Wed, 11 Mar 2026 11:46:36 +0800 Subject: [PATCH] [clang-tidy][NFC] Use universal utility mock in testcases [2/N] --- .../checkers/Inputs/Headers/utility | 4 ++ .../checkers/abseil/cleanup-ctad.cpp | 10 +--- .../checkers/bugprone/incorrect-enable-if.cpp | 12 +---- .../narrowing-conversions-bitfields.cpp | 50 ++++++++----------- .../throw-by-value-catch-by-reference.cpp | 11 +--- .../misc/unconventional-assign-operator.cpp | 12 +---- .../checkers/modernize/avoid-c-arrays.cpp | 25 +--------- .../use-constraints-first-greatergreater.cpp | 12 +---- .../checkers/modernize/use-constraints.cpp | 12 +---- .../checkers/modernize/use-emplace.cpp | 9 +--- .../checkers/modernize/use-string-view.cpp | 8 +-- .../use-trailing-return-type-cxx20.cpp | 8 +-- ...ng-return-type-transform-lambdas-cxx20.cpp | 8 +-- .../modernize/use-transparent-functors.cpp | 11 +--- .../performance/string-view-conversions.cpp | 27 +--------- .../readability/container-data-pointer.cpp | 9 +--- 16 files changed, 45 insertions(+), 183 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility index ab2125e59abae..e87465118ab2a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility @@ -52,6 +52,10 @@ struct enable_if<true, T> { typedef T type; }; template <bool B, class T = void> using enable_if_t = typename enable_if<B, T>::type; +template <class T> struct remove_const { using type = T; }; +template <class T> struct remove_const<const T> { using type = T; }; +template <class T> using remove_const_t = typename remove_const<T>::type; + template <class T> struct remove_cv { using type = T; }; template <class T> struct remove_cv<const T> { using type = T; }; template <class T> struct remove_cv<volatile T> { using type = T; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/abseil/cleanup-ctad.cpp b/clang-tools-extra/test/clang-tidy/checkers/abseil/cleanup-ctad.cpp index e4eb968950297..50e6bec874169 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/abseil/cleanup-ctad.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/abseil/cleanup-ctad.cpp @@ -1,14 +1,8 @@ // RUN: %check_clang_tidy %s abseil-cleanup-ctad -std=c++17-or-later %t -namespace std { - -template <typename, typename> -struct is_same { - static const bool value = false; -}; +#include <utility> -template <typename T> -struct is_same<T, T> { static const bool value = true; }; +namespace std { template <typename> class function { diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp index 7882156f19a44..0c418efe1420b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-if.cpp @@ -1,17 +1,7 @@ // RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s bugprone-incorrect-enable-if %t // RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=CXX20 %s bugprone-incorrect-enable-if %t -// NOLINTBEGIN -namespace std { -template <bool B, class T = void> struct enable_if { }; - -template <class T> struct enable_if<true, T> { typedef T type; }; - -template <bool B, class T = void> -using enable_if_t = typename enable_if<B, T>::type; - -} // namespace std -// NOLINTEND +#include <utility> template <typename T, typename = typename std::enable_if<T::some_value>::type> void valid_function1() {} diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-bitfields.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-bitfields.cpp index 9770f44749267..3458aa0539273 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-bitfields.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-bitfields.cpp @@ -1,21 +1,11 @@ // RUN: %check_clang_tidy %s bugprone-narrowing-conversions %t \ // RUN: -std=c++17-or-later -- -target x86_64-unknown-linux +#include <utility> + #define CHAR_BITS 8 static_assert(sizeof(unsigned int) == 32 / CHAR_BITS); -template <typename T, typename U> -struct is_same { - static constexpr bool value = false; -}; -template <typename T> -struct is_same<T, T> { - static constexpr bool value = true; -}; - -template <typename T, typename U> -static constexpr bool is_same_v = is_same<T, U>::value; - struct NoBitfield { unsigned int id; }; @@ -36,8 +26,8 @@ int example_warning(unsigned x) { } void test_binary_and(SmallBitfield x) { - static_assert(is_same_v<decltype(x.id & 1), int>); - static_assert(is_same_v<decltype(x.id & 1u), unsigned>); + static_assert(std::is_same_v<decltype(x.id & 1), int>); + static_assert(std::is_same_v<decltype(x.id & 1u), unsigned>); x.id & 1; x.id & 1u; @@ -47,8 +37,8 @@ void test_binary_and(SmallBitfield x) { } void test_binary_or(SmallBitfield x) { - static_assert(is_same_v<decltype(x.id | 1), int>); - static_assert(is_same_v<decltype(x.id | 1u), unsigned>); + static_assert(std::is_same_v<decltype(x.id | 1), int>); + static_assert(std::is_same_v<decltype(x.id | 1u), unsigned>); x.id | 1; x.id | 1u; @@ -107,10 +97,10 @@ void test_parameter_passing(CompleteBitfield x) { } void test(NoBitfield x) { - static_assert(is_same_v<decltype(x.id << 1), unsigned>); - static_assert(is_same_v<decltype(x.id << 1u), unsigned>); - static_assert(is_same_v<decltype(x.id + 1), unsigned>); - static_assert(is_same_v<decltype(x.id + 1u), unsigned>); + static_assert(std::is_same_v<decltype(x.id << 1), unsigned>); + static_assert(std::is_same_v<decltype(x.id << 1u), unsigned>); + static_assert(std::is_same_v<decltype(x.id + 1), unsigned>); + static_assert(std::is_same_v<decltype(x.id + 1u), unsigned>); x.id << 1; x.id << 1u; @@ -128,8 +118,8 @@ void test(NoBitfield x) { } void test(SmallBitfield x) { - static_assert(is_same_v<decltype(x.id << 1), int>); - static_assert(is_same_v<decltype(x.id << 1u), int>); + static_assert(std::is_same_v<decltype(x.id << 1), int>); + static_assert(std::is_same_v<decltype(x.id << 1u), int>); x.id << 1; x.id << 1u; @@ -149,8 +139,8 @@ void test(SmallBitfield x) { } void test(BigBitfield x) { - static_assert(is_same_v<decltype(x.id << 1), int>); - static_assert(is_same_v<decltype(x.id << 1u), int>); + static_assert(std::is_same_v<decltype(x.id << 1), int>); + static_assert(std::is_same_v<decltype(x.id << 1u), int>); x.id << 1; x.id << 1u; @@ -170,8 +160,8 @@ void test(BigBitfield x) { } void test(CompleteBitfield x) { - static_assert(is_same_v<decltype(x.id << 1), unsigned>); - static_assert(is_same_v<decltype(x.id << 1u), unsigned>); + static_assert(std::is_same_v<decltype(x.id << 1), unsigned>); + static_assert(std::is_same_v<decltype(x.id << 1u), unsigned>); x.id << 1; x.id << 1u; @@ -191,13 +181,13 @@ void test(CompleteBitfield x) { } void test_parens(SmallBitfield x) { - static_assert(is_same_v<decltype(x.id << (2)), int>); - static_assert(is_same_v<decltype(((x.id)) << (2)), int>); + static_assert(std::is_same_v<decltype(x.id << (2)), int>); + static_assert(std::is_same_v<decltype(((x.id)) << (2)), int>); x.id << (2); ((x.id)) << (2); - static_assert(is_same_v<decltype((2) << x.id), int>); - static_assert(is_same_v<decltype((2) << ((x.id))), int>); + static_assert(std::is_same_v<decltype((2) << x.id), int>); + static_assert(std::is_same_v<decltype((2) << ((x.id))), int>); (2) << x.id; (2) << ((x.id)); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/throw-by-value-catch-by-reference.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/throw-by-value-catch-by-reference.cpp index 269ef90f38fb8..e24c78e56e272 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/throw-by-value-catch-by-reference.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/throw-by-value-catch-by-reference.cpp @@ -1,5 +1,6 @@ // RUN: %check_clang_tidy %s misc-throw-by-value-catch-by-reference %t -- -- -fcxx-exceptions +#include <utility> class logic_error { public: @@ -11,14 +12,6 @@ typedef logic_ptr logic_double_typedef; int lastException; -template <class T> struct remove_reference { typedef T type; }; -template <class T> struct remove_reference<T &> { typedef T type; }; -template <class T> struct remove_reference<T &&> { typedef T type; }; - -template <typename T> typename remove_reference<T>::type &&move(T &&arg) { - return static_cast<typename remove_reference<T>::type &&>(arg); -} - logic_error CreateException() { return logic_error("created"); } void testThrowFunc() { @@ -41,7 +34,7 @@ void testThrowFunc() { throw lvalue; // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference] - throw move(lvalue); + throw std::move(lvalue); int &ex = lastException; throw ex; // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference] diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp index d7a5797cc2844..c3b1e729de1db 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp @@ -1,16 +1,6 @@ // RUN: %check_clang_tidy %s misc-unconventional-assign-operator %t -- -- -fno-delayed-template-parsing -namespace std { -template <typename T> -struct remove_reference { typedef T type; }; -template <typename T> -struct remove_reference<T &> { typedef T type; }; -template <typename T> -struct remove_reference<T &&> { typedef T type; }; -template <typename T> -typename remove_reference<T>::type &&move(T &&t); -} - +#include <utility> struct Good { Good& operator=(const Good&); diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp index 95e04d53eb6ac..5f1bbee075b18 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp @@ -1,5 +1,7 @@ // RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t +#include <utility> + int a[] = {1, 2}; // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead @@ -100,29 +102,6 @@ const char name[] = "Some string"; void takeCharArray(const char name[]); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] -namespace std { - template<class T, class U> - struct is_same { constexpr static bool value{false}; }; - - template<class T> - struct is_same<T, T> { constexpr static bool value{true}; }; - - template<class T, class U> - constexpr bool is_same_v = is_same<T, U>::value; - - template<class T> struct remove_const { typedef T type; }; - template<class T> struct remove_const<const T> { typedef T type; }; - - template<class T> - using remove_const_t = typename remove_const<T>::type; - - template<bool B, class T = void> struct enable_if {}; - template<class T> struct enable_if<true, T> { typedef T type; }; - - template< bool B, class T = void > - using enable_if_t = typename enable_if<B, T>::type; -} - // within below template decl, no array type findings are expected within the template parameter declarations since not a single C-style array type got written explicitly template <typename T, bool = std::is_same_v<T, int>, diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints-first-greatergreater.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints-first-greatergreater.cpp index bbe1f3ca06a47..3aac6473b5986 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints-first-greatergreater.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints-first-greatergreater.cpp @@ -1,16 +1,6 @@ // RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-constraints %t -- -- -fno-delayed-template-parsing -// NOLINTBEGIN -namespace std { -template <bool B, class T = void> struct enable_if { }; - -template <class T> struct enable_if<true, T> { typedef T type; }; - -template <bool B, class T = void> -using enable_if_t = typename enable_if<B, T>::type; - -} // namespace std -// NOLINTEND +#include <utility> // Separate test file for the case where the first '>>' token part of // an enable_if expression correctly handles the synthesized token. 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 ecae36165e05e..4a07baf2f3196 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 @@ -1,16 +1,6 @@ // RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-constraints %t -- -- -fno-delayed-template-parsing -// NOLINTBEGIN -namespace std { -template <bool B, class T = void> struct enable_if { }; - -template <class T> struct enable_if<true, T> { typedef T type; }; - -template <bool B, class T = void> -using enable_if_t = typename enable_if<B, T>::type; - -} // namespace std -// NOLINTEND +#include <utility> template <typename...> struct ConsumeVariadic; diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp index e6562cd18dbab..7d88c1be24747 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp @@ -7,6 +7,8 @@ // RUN: modernize-use-emplace.TupleMakeFunctions: \ // RUN: '::std::make_pair; ::std::make_tuple; ::test::MakeSingle'}}" +#include <utility> + namespace std { template <typename E> class initializer_list { @@ -275,13 +277,6 @@ class priority_queue { void emplace(Args &&...args){}; }; -template <typename T> -struct remove_reference { using type = T; }; -template <typename T> -struct remove_reference<T &> { using type = T; }; -template <typename T> -struct remove_reference<T &&> { using type = T; }; - template <typename T1, typename T2> pair<typename remove_reference<T1>::type, typename remove_reference<T2>::type> make_pair(T1 &&, T2 &&) { diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-string-view.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-string-view.cpp index 63656b4959bb3..26a72c1c242e1 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-string-view.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-string-view.cpp @@ -1,13 +1,7 @@ // RUN: %check_clang_tidy \ // RUN: -std=c++17-or-later %s modernize-use-string-view %t #include <string> - -namespace std { -template <class T, class U> struct is_same { static constexpr bool value = false; }; -template <class T> struct is_same<T, T> { static constexpr bool value = true; }; -template <class T, class U> constexpr bool is_same_v = is_same<T, U>::value; -} // namespace std - +#include <utility> // ========================================================== // Positive tests diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-cxx20.cpp index dd45094464d42..83f75b64b055b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-cxx20.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-cxx20.cpp @@ -1,12 +1,8 @@ // RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-trailing-return-type %t -namespace std { -template <typename T, typename U> -struct is_same { static constexpr auto value = false; }; - -template <typename T> -struct is_same<T, T> { static constexpr auto value = true; }; +#include <utility> +namespace std { template <typename T> concept floating_point = std::is_same<T, float>::value || std::is_same<T, double>::value || std::is_same<T, long double>::value; } diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp index 83b9b84ef4014..24f135054fded 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp @@ -1,12 +1,8 @@ // RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-trailing-return-type %t -- -- -fno-delayed-template-parsing -namespace std { -template <typename T, typename U> -struct is_same { static constexpr auto value = false; }; - -template <typename T> -struct is_same<T, T> { static constexpr auto value = true; }; +#include <utility> +namespace std { template <typename T> concept floating_point = std::is_same<T, float>::value || std::is_same<T, double>::value || std::is_same<T, long double>::value; } diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-transparent-functors.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-transparent-functors.cpp index d18f5f5a4b780..2c7f743acc63d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-transparent-functors.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-transparent-functors.cpp @@ -1,15 +1,8 @@ // RUN: %check_clang_tidy -std=c++14-or-later %s modernize-use-transparent-functors %t -namespace std { -template<class T> -struct remove_reference; - -template <class T> -constexpr T &&forward(typename std::remove_reference<T>::type &t); - -template <class T> -constexpr T &&forward(typename std::remove_reference<T>::type &&t); +#include <utility> +namespace std { template <typename T = void> struct plus { constexpr T operator()(const T &Lhs, const T &Rhs) const; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp index 99d2a64a7d5d8..d632c566a30e6 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp @@ -1,35 +1,10 @@ // RUN: %check_clang_tidy -std=c++17-or-later %s performance-string-view-conversions %t #include <string> +#include <utility> using namespace std::literals::string_literals; using namespace std::literals::string_view_literals; -// Support for std::move -namespace std { -template <typename> -struct remove_reference; - -template <typename _Tp> -struct remove_reference { - typedef _Tp type; -}; - -template <typename _Tp> -struct remove_reference<_Tp &> { - typedef _Tp type; -}; - -template <typename _Tp> -struct remove_reference<_Tp &&> { - typedef _Tp type; -}; - -template <typename _Tp> -constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) { - return static_cast<typename std::remove_reference<_Tp>::type &&>(__t); -} -} // namespace std - void foo_sv(int p1, std::string_view p2, double p3); void foo_wsv(int p1, std::wstring_view p2, double p3); void foo_wsv(std::wstring_view p2); diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp index cac0cbc3de85d..70ade83eed0f0 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp @@ -3,6 +3,7 @@ #include <string> #include <vector> +#include <utility> typedef __SIZE_TYPE__ size_t; @@ -16,14 +17,6 @@ struct is_integral<size_t> { static const bool value = true; }; -template <bool, typename T = void> -struct enable_if { }; - -template <typename T> -struct enable_if<true, T> { - typedef T type; -}; - template <typename T> struct unique_ptr { T &operator*() const; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
