[clang] [WebKit Checkers] Trivial analysis should check destructors of function parameters and local variables (PR #181576)

Ryosuke Niwa via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 16 14:01:23 PST 2026


================
@@ -542,6 +558,29 @@ class TrivialFunctionAnalysisVisitor
     });
   }
 
+  bool HasTrivialDestructor(const VarDecl *VD) {
+    return WithCachedResult(VD, [&]() {
+      auto QT = VD->getType();
+      if (QT.isPODType(VD->getASTContext()))
+        return true;
+      auto *Type = QT.getTypePtrOrNull();
+      if (!Type)
+        return false;
+      if (isa<LValueReferenceType>(Type))
+        return true; // T& does not run its destructor.
+      if (auto *RT = dyn_cast<RValueReferenceType>(Type)) {
+        // For T&&, we evaluate the destructor of T.
+        auto *T = RT->getPointeeType().getTypePtrOrNull();
+        return T && CanTriviallyDestruct(T);
+      }
----------------
rniwa wrote:

I was thinking of a case like this (which in the test):
```
  void [[clang::annotate_type("webkit.nodelete")]] swapObj(RefPtr<RefCountable>&& obj) {
    // expected-warning at -1{{A function 'swapObj' has [[clang::annotate_type("webkit.nodelete")]] but it contains code that could destruct an object}}
    m_obj.swap(obj);
  }
```
where the argument variable gets a new value during the function such that it would destruct. Although now that I'm thinking about it more, even in the case of swap, we don't really destruct within this function so maybe we should treat both references as safe after all.

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


More information about the cfe-commits mailing list