diff options
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp b/clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp index eac744bcec1..6be46e81c93 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp @@ -138,3 +138,95 @@ void f_functional_cast() { B b; A a = A(b); } + +class StringRef +{ +public: + StringRef(const char *); + const char *begin() const; + const char *end() const; +}; + +template <typename T, typename U> +T template_value_cast(const U &u); + +template <typename T, typename U> +T *template_pointer_cast(U *u); + +template <typename T, typename U> +T &template_reference_cast(U &u); + +template <typename T, typename U> +const T *template_const_pointer_cast(const U *u); + +template <typename T, typename U> +const T &template_const_reference_cast(const U &u); + +template <typename T> +T template_value_get(StringRef s); + +struct S { + template <typename T> + const T *template_member_get(); +}; + +template <typename T> +T max(T t1, T t2); + +void f_template_cast() +{ + double d = 0; + int i1 = template_value_cast<int>(d); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = template_value_cast<int>(d); + + A *a = new B(); + B *b1 = template_value_cast<B *>(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = template_value_cast<B *>(a); + B &b2 = template_value_cast<B &>(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto &b2 = template_value_cast<B &>(*a); + B *b3 = template_pointer_cast<B>(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto *b3 = template_pointer_cast<B>(a); + B &b4 = template_reference_cast<B>(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto &b4 = template_reference_cast<B>(*a); + const B *b5 = template_const_pointer_cast<B>(a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b5 = template_const_pointer_cast<B>(a); + const B &b6 = template_const_reference_cast<B>(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: const auto &b6 = template_const_reference_cast<B>(*a); + B *b7 = template_value_get<B *>("foo"); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto *b7 = template_value_get<B *>("foo"); + B *b8 = template_value_get<B *>("foo"), *b9 = template_value_get<B *>("bar"); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: auto *b8 = template_value_get<B *>("foo"), *b9 = template_value_get<B *>("bar"); + + S s; + const B *b10 = s.template_member_get<B>(); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a template cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b10 = s.template_member_get<B>(); + + // Don't warn when auto is already being used. + auto i2 = template_value_cast<int>(d); + auto *i3 = template_value_cast<int *>(d); + auto **i4 = template_value_cast<int **>(d); + auto &i5 = template_reference_cast<int>(d); + + // Don't warn for implicit template arguments. + int i6 = max(i1, i2); + + // Don't warn for mismatched var and initializer types. + A *a1 = template_value_cast<B *>(a); + + // Don't warn for mismatched var types. + B *b11 = template_value_get<B *>("foo"), b12 = template_value_get<B>("bar"); + + // Don't warn for implicit variables. + for (auto &c : template_reference_cast<StringRef>(*a)) { + } +} |