[clang] [clang-tidy]Add new check bugprone-casting-through-void (PR #69465)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 10:12:12 PDT 2023


================
@@ -0,0 +1,96 @@
+// RUN: %check_clang_tidy %s bugprone-casting-through-void %t
+
+using V = void*;
+using CV = const void*;
+
+int i = 100;
+double d = 100;
+const int ci = 100;
+const double cd = 100;
+
+void normal_test() {
+  static_cast<int *>(static_cast<void *>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<int *>(static_cast<V>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<int *>(static_cast<void *>(&i));
+  static_cast<void *>(static_cast<void *>(&i));
+}
+
+void const_pointer_test() {
+  static_cast<int *const>(static_cast<void *>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not cast 'double *' to 'int *const' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<int *const>(static_cast<V>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not cast 'double *' to 'int *const' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<int *const>(static_cast<void *>(&i));
+  static_cast<void *const>(static_cast<void *>(&i));
+}
+
+void const_test() {
+  static_cast<const int *>(static_cast<const void *>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not cast 'double *' to 'const int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<const int *>(static_cast<const V>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not cast 'double *' to 'const int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<const int *>(static_cast<const void *>(&i));
+  static_cast<const void *>(static_cast<const void *>(&i));
+
+  static_cast<const int *>(static_cast<const void *>(&cd));
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not cast 'const double *' to 'const int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<const int *>(static_cast<const CV>(&cd));
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not cast 'const double *' to 'const int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<const int *>(static_cast<const void *>(&ci));
+  static_cast<const void *>(static_cast<const void *>(&ci));
+}
+
+
+void reinterpret_cast_test() {
+  static_cast<int *>(reinterpret_cast<void *>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+  reinterpret_cast<int *>(static_cast<void *>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+  reinterpret_cast<int *>(reinterpret_cast<void *>(&d));
+  // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<int *>(reinterpret_cast<void *>(&i));
+  static_cast<void *>(reinterpret_cast<void *>(&i));
+  reinterpret_cast<int *>(reinterpret_cast<void *>(&i));
+  reinterpret_cast<void *>(reinterpret_cast<void *>(&i));
+  static_cast<int *>(reinterpret_cast<void *>(&i));
+  static_cast<void *>(reinterpret_cast<void *>(&i));
+}
+
+void c_style_cast_test() {
+  static_cast<int *>((void *)&d);
+  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+  (int *)(void *)&d;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+  static_cast<int *>((void *)&d);
+  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not cast 'double *' to 'int *' through 'void*' [bugprone-casting-through-void]
+
+  static_cast<int *>(reinterpret_cast<void *>(&i));
----------------
PiotrZSL wrote:

duplicate, you have this already in line 62

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


More information about the cfe-commits mailing list