================
@@ -0,0 +1,200 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting
%t
+// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s
readability-redundant-casting %t -- \
+// RUN: -config='{CheckOptions: {
readability-redundant-casting.IgnoreMacros: false }}'
+// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s
readability-redundant-casting %t -- \
+// RUN: -config='{CheckOptions: {
readability-redundant-casting.IgnoreTypeAliases: true }}'
+
+struct A {};
+struct B : A {};
+A getA();
+
+void testRedundantStaticCasting(A& value) {
+ A& a1 = static_cast<A&>(value);
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:36: note: source type originates from
referencing this parameter
+ // CHECK-FIXES: {{^}} A& a1 = value;
+}
+
+void testRedundantConstCasting1(A& value) {
+ A& a2 = const_cast<A&>(value);
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:36: note: source type originates from
referencing this parameter
+ // CHECK-FIXES: {{^}} A& a2 = value;
+}
+
+void testRedundantConstCasting2(const A& value) {
+ const A& a3 = const_cast<const A&>(value);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to
the same type 'const A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:42: note: source type originates from
referencing this parameter
+ // CHECK-FIXES: {{^}} const A& a3 = value;
+}
+
+void testRedundantReinterpretCasting(A& value) {
+ A& a4 = reinterpret_cast<A&>(value);
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:41: note: source type originates from
referencing this parameter
+ // CHECK-FIXES: {{^}} A& a4 = value;
+}
+
+void testRedundantCCasting(A& value) {
+ A& a5 = (A&)(value);
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:31: note: source type originates from
referencing this parameter
+ // CHECK-FIXES: {{^}} A& a5 = value;
+}
+
+void testDoubleCasting(A& value) {
+ A& a6 = static_cast<A&>(reinterpret_cast<A&>(value));
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-2]]:27: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-4]]:27: note: source type originates from
referencing this parameter
+ // CHECK-FIXES: {{^}} A& a6 = value;
+}
+
+void testDiffrentTypesCast(B& value) {
+ A& a7 = static_cast<A&>(value);
+}
+
+void testCastingWithAuto() {
+ auto a = getA();
+ A& a8 = static_cast<A&>(a);
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to
the same type 'A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:8: note: source type originates from
referencing this variable
+ // CHECK-FIXES: {{^}} A& a8 = a;
+}
+
+void testCastingWithConstAuto() {
+ const auto a = getA();
+ const A& a9 = static_cast<const A&>(a);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to
the same type 'const A' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:14: note: source type originates from
referencing this variable
+ // CHECK-FIXES: {{^}} const A& a9 = a;
+}
+
+void testCastingWithAutoPtr(A& ptr) {
+ auto* a = &ptr;
+ A* a10 = static_cast<A*>(a);
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant explicit casting to
the same type 'A *' as the sub-expression, remove this casting
[readability-redundant-casting]
+ // CHECK-MESSAGES: :[[@LINE-3]]:9: note: source type originates from
referencing this variable
+ // CHECK-FIXES: {{^}} A* a10 = a;
+}
+
+template<typename T>
+void testRedundantTemplateCasting(T& value) {
+ A& a = static_cast<A&>(value);
+ T& t = static_cast<T&>(value);
----------------
HerrCai0907 wrote:
I don't think redundant casting should be diagnosed for template parameters.
It may break existing code when template function supports different type input
and need to cast them to the same type.
https://github.com/llvm/llvm-project/pull/70595
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits