[clang] [alpha.webkit.NoDeleteChecker] Don't warn members with an unused default constructor (PR #241102)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 19:17:56 PST 2026
https://github.com/rniwa created https://github.com/llvm/llvm-project/pull/241102
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
>From 3b1b3a9c4733ec39ea06fa90886691e39b7f64f8 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Tue, 10 Nov 2026 19:11:56 -0800
Subject: [PATCH] [alpha.webkit.NoDeleteChecker] Don't warn members with an
unused default constructor
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
---
.../Checkers/WebKit/PtrTypesSemantics.cpp | 21 +++++++++++++++++++
.../Checkers/WebKit/nodelete-annotation.cpp | 19 +++++++++++++++++
2 files changed, 40 insertions(+)
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
More information about the cfe-commits
mailing list