[clang] [WebKit Checkers] Do not report lambda captures in system headers (PR #226182)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 24 07:39:20 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: geoffreygaren

<details>
<summary>Changes</summary>

This matches the policy in other WebKit checkers.

Some `std` namespace lambdas like `find` lack the `noescape` annotation, so the borrow checker flags them.

Assisted-by: Claude

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


3 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp (+2) 
- (modified) clang/test/Analysis/Checkers/WebKit/mock-system-header.h (+8) 
- (modified) clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp (+5) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
index e0debb764af00..3fc4c38c8bed7 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
@@ -549,6 +549,8 @@ class RawPtrRefLambdaCapturesChecker
   void visitLambdaExpr(const LambdaExpr *L, bool shouldCheckThis,
                        const QualType T,
                        bool ignoreParamVarDecl = false) const {
+    if (BR->getSourceManager().isInSystemHeader(L->getBeginLoc()))
+      return;
     if (TFA.isTrivial(L->getBody()))
       return;
     for (const LambdaCapture &C : L->captures()) {
diff --git a/clang/test/Analysis/Checkers/WebKit/mock-system-header.h b/clang/test/Analysis/Checkers/WebKit/mock-system-header.h
index d55b3abd34f4c..fc675418a1d79 100644
--- a/clang/test/Analysis/Checkers/WebKit/mock-system-header.h
+++ b/clang/test/Analysis/Checkers/WebKit/mock-system-header.h
@@ -1,5 +1,13 @@
 #pragma clang system_header
 
+template <typename F>
+void takeLambda(F);
+
+template <typename T>
+inline void lambdaInSystemHeader(T* obj) {
+  takeLambda([obj] { obj->method(); });
+}
+
 template <typename T, typename CreateFunction>
 void callMethod(CreateFunction createFunction) {
   createFunction()->method();
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
index 3394315effe6f..4bfd095165ca8 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_analyze_cc1 -analyzer-checker=webkit.UncountedLambdaCapturesChecker -verify %s
 
 #include "mock-types.h"
+#include "mock-system-header.h"
 
 struct A {
   static void b();
@@ -720,3 +721,7 @@ void instantiate_dependent_callables(NoEscapeCallable& noEscape,
   call_through_noescape_callable(noEscape);
   call_through_escaping_callable(escape);
 }
+
+void lambda_in_system_header(RefCountable* ref_countable) {
+  lambdaInSystemHeader(ref_countable);
+}

``````````

</details>


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


More information about the cfe-commits mailing list