[clang] [alpha.webkit.NoDeleteChecker] Don't warn members with an unused default constructor (PR #241102)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 19:18:32 PST 2026


llvmorg-github-actions[bot] wrote:


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

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

<details>
<summary>Changes</summary>

A nodelete constructor was reported for constructing a member whose type has a `= default` default constructor that is non-trivial only because of default member initializers, when nothing in the translation unit used that constructor. Such a constructor has no body in the AST until something uses it, so IsFunctionTrivialImpl hit the null-body check and called it non-trivial. Whether the warning fired therefore depended on unrelated code in the same file or unified source bundle, even though the member was explicitly initialized by the constructor being checked.

For a default constructor with no body, decide from what it would run instead: in-class initializer of each field (or, for fields without one, whether the type is trivially constructible), and the bases and virtual bases. Constructors that have a body take the existing path.

Co-authored with Claude.

rdar://192380708

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


2 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp (+21) 
- (modified) clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp (+19) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index 9cc782985c834..5a4de646902a6 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -1016,6 +1016,27 @@ class TrivialFunctionAnalysisVisitor
               (CtorDecl->isMoveConstructor() &&
                RD->hasTrivialMoveConstructor()))
             return true;
+          // A defaulted default constructor that nothing has used yet has no
+          // definition, so decide from the initializers it would run.
+          if (CtorDecl->isDefaultConstructor() && !CtorDecl->isDeleted() &&
+              !CtorDecl->doesThisDeclarationHaveABody() && RD->hasDefinition()) {
+            for (const FieldDecl *F : RD->fields()) {
+              if (const Expr *Init = F->getInClassInitializer()) {
+                if (!Visit(Init))
+                  return false;
+              } else if (!CanTriviallyConstruct(F->getType()))
+                return false;
+            }
+            for (const CXXBaseSpecifier &B : RD->bases()) {
+              if (!CanTriviallyConstruct(B.getType()))
+                return false;
+            }
+            for (const CXXBaseSpecifier &B : RD->vbases()) {
+              if (!CanTriviallyConstruct(B.getType()))
+                return false;
+            }
+            return true;
+          }
         }
         if (DtorDecl)
           return CanTriviallyRunImplicitDtor(DtorDecl);
diff --git a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
index bc5ecf2803cea..3e4aa577930b6 100644
--- a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
@@ -980,6 +980,25 @@ constexpr T min(initializer_list<T> list) {
 
 } // namespace std
 
+namespace unused_defaulted_default_ctor {
+
+struct Ext { double w; };
+
+struct Size {
+  constexpr Size() = default;
+  [[clang::annotate("webkit.nodelete")]] Size(const Ext&);
+  double m_w { 0 };
+};
+
+struct Rect {
+  [[clang::annotate("webkit.nodelete")]] Rect(const Ext&);
+  Size m_size;
+};
+
+Rect::Rect(const Ext& e) : m_size(e) { }
+
+} // namespace unused_defaulted_default_ctor
+
 namespace std_initializer_list {
 
 // A braced list passed as std::initializer_list materialises a backing array

``````````

</details>


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


More information about the cfe-commits mailing list