[clang-tools-extra] [clang-tidy] Add readability-redundant-casting check (PR #70595)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 10:13:58 PST 2024


================
@@ -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);
----------------
PiotrZSL wrote:

No problem here, because TK_IgnoreUnlessSpelledInSource is used and template instances won't be checked by check, so if T would be A in above example, check wouldn't detect any issues anyway, and such tests exist in line 93.

https://github.com/llvm/llvm-project/pull/70595


More information about the cfe-commits mailing list