[clang] [alpha.webkit.MemoryUnsafeCastChecker] Don't warn an explicit construction of an object (PR #240271)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 22:00:06 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

<details>
<summary>Changes</summary>

Don't emit a warning when we're constructing an object via a constructor.

Co-authored with Claude

rdar://189998444

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


3 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/MemoryUnsafeCastChecker.cpp (+13-2) 
- (added) clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast-value-conversion.cpp (+45) 
- (modified) clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast.cpp (+5) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/MemoryUnsafeCastChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/MemoryUnsafeCastChecker.cpp
index 10037114cf8bf9..ef9334c9dff469 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/MemoryUnsafeCastChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/MemoryUnsafeCastChecker.cpp
@@ -105,6 +105,15 @@ using BoundNodesMap = ::clang::ast_matchers::internal::BoundNodesMap;
 // Matches the plain `id` type.
 AST_MATCHER(QualType, isObjCIdType) { return Node->isObjCIdType(); }
 
+// Matches an explicit cast producing a prvalue of record type, which constructs
+// a new object through a converting constructor or a conversion operator (or
+// is a copy) rather than reinterpreting the source object's memory.
+// __builtin_bit_cast is excluded since it does reinterpret the source's bytes.
+AST_MATCHER(ExplicitCastExpr, isRecordValueConversion) {
+  return Node.isPRValue() && Node.getType()->isRecordType() &&
+         !isa<BuiltinBitCastExpr>(Node);
+}
+
 // Like equalsBoundNode, but compares canonical declarations so that different
 // redeclarations of the same class (e.g. a forward declaration and the
 // definition) are treated as the same declaration.
@@ -202,7 +211,8 @@ void MemoryUnsafeCastChecker::checkASTCodeBody(const Decl *D,
                     .bind(DerivedNode)));
 
   auto ExplicitCast =
-      explicitCastExpr(anyOf(MatchExprPtr, MatchExprRefTypeDef,
+      explicitCastExpr(unless(isRecordValueConversion()),
+                       anyOf(MatchExprPtr, MatchExprRefTypeDef,
                              MatchExprPtrObjC, MatchExprPtrVoidCast))
           .bind(WarnRecordDecl);
   auto Cast = stmt(ExplicitCast);
@@ -267,7 +277,8 @@ void MemoryUnsafeCastChecker::checkASTCodeBody(const Decl *D,
                   hasSameCanonicalDeclAsBoundNode(ToCastNode))))))))))));
 
   auto ExplicitCastUnrelated =
-      explicitCastExpr(anyOf(MatchExprPtrUnrelatedTypes,
+      explicitCastExpr(unless(isRecordValueConversion()),
+                       anyOf(MatchExprPtrUnrelatedTypes,
                              MatchExprPtrObjCUnrelatedTypes,
                              MatchExprRefTypeDefUnrelated))
           .bind(WarnRecordDecl);
diff --git a/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast-value-conversion.cpp b/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast-value-conversion.cpp
new file mode 100644
index 00000000000000..18108d7c7ca371
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast-value-conversion.cpp
@@ -0,0 +1,45 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.MemoryUnsafeCastChecker -std=c++20 -verify %s
+// expected-no-diagnostics
+
+// Explicit casts producing a prvalue of record type construct a new object
+// through a converting constructor or a conversion operator (or are a no-op
+// copy), so they don't reinterpret memory. The redeclarations below make the
+// source and destination types refer to different redeclarations of the same
+// class.
+
+class Base { };
+class Derived : public Base { };
+
+class FloatRect {
+public:
+  FloatRect();
+};
+
+class LayoutRect {
+public:
+  LayoutRect();
+  LayoutRect(const FloatRect&);
+  operator FloatRect() const;
+};
+
+FloatRect makeFloatRect();
+
+class Base;
+class Derived;
+class FloatRect;
+class LayoutRect;
+
+void test_value_conversions(const LayoutRect& layoutRect, const FloatRect& floatRect) {
+  FloatRect a = FloatRect(layoutRect); // no warning
+  FloatRect b = FloatRect(makeFloatRect()); // no warning
+  FloatRect c = FloatRect(floatRect); // no warning
+  FloatRect d = static_cast<FloatRect>(layoutRect); // no warning
+  FloatRect e = static_cast<FloatRect>(makeFloatRect()); // no warning
+  FloatRect f = (FloatRect)layoutRect; // no warning
+  LayoutRect g = LayoutRect(floatRect); // no warning
+  LayoutRect h = static_cast<LayoutRect>(makeFloatRect()); // no warning
+}
+
+void test_derived_to_base_value(Derived& derived) {
+  Base base = static_cast<Base>(derived); // no warning
+}
diff --git a/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast.cpp b/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast.cpp
index e1711c43664501..a13aa581b5dfb3 100644
--- a/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/memory-unsafe-cast.cpp
@@ -29,6 +29,11 @@ void test_non_pointers(Derived derived) {
   Base base_static = static_cast<Base>(derived);  // no warning
 }
 
+void test_bit_cast(Base base) {
+  Derived derived = __builtin_bit_cast(Derived, base);
+  // expected-warning at -1{{Unsafe cast from base type 'Base' to derived type 'Derived'}}
+}
+
 void test_refs(Base &base) {
   Derived &derived_static = static_cast<Derived&>(base);
   // expected-warning at -1{{Unsafe cast from base type 'Base' to derived type 'Derived'}}

``````````

</details>


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


More information about the cfe-commits mailing list