llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Baranov Victor (vbvictor)

<details>
<summary>Changes</summary>

This check tells users to use `reinterpret_cast`, which is not possible in pure 
C code.

---
Full diff: https://github.com/llvm/llvm-project/pull/182844.diff


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.h 
(+3) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.c 
(+11) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.h
index 0d2eba1977e97..1556e6d1b82d5 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.h
@@ -22,6 +22,9 @@ class CastingThroughVoidCheck : public ClangTidyCheck {
       : ClangTidyCheck(Name, Context) {}
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+    return LangOpts.CPlusPlus;
+  }
   std::optional<TraversalKind> getCheckTraversalKind() const override {
     return TK_IgnoreUnlessSpelledInSource;
   }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 2bb4800df47c9..4f4e49a35fe4c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -153,6 +153,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/bad-signal-to-kill-thread>` check by fixing false
   negatives when the ``SIGTERM`` macro is obtained from a precompiled header.
 
+- Improved :doc:`bugprone-casting-through-void
+  <clang-tidy/checks/bugprone/casting-through-void>` check by running only on
+  C++ files because suggested ``reinterpret_cast`` is not available in pure C.
+
 - Improved :doc:`bugprone-exception-escape
   <clang-tidy/checks/bugprone/exception-escape>` check by adding
   `TreatFunctionsWithoutSpecificationAsThrowing` option to support reporting
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.c 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.c
new file mode 100644
index 0000000000000..28d5dfb7d229b
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.c
@@ -0,0 +1,11 @@
+// RUN: clang-tidy -checks='-*,bugprone-casting-through-void' %s -- -std=c99 
2>&1 | FileCheck %s --allow-empty
+// CHECK-NOT: warning:
+
+double d = 100;
+
+void normal_test() {
+  (int *)(void *)&d;
+  int x = 1;
+  char *y = (char*)(void*)&x;
+  char *z = (char*)&x;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/182844
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to