Skip to content

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Aug 22, 2025

Fix a bug that webkit.UncountedLambdaCapturesChecker was erroneously emitting a warning for a DeclRefExpr which is passed in as an argument to a no-escape function argument. The bug was caused by findLambdaInArg not adding DeclRefExpr to the ignored set even when a lambda was identified as an argument.

…a in an no-escape argument

Fix a bug that webkit.UncountedLambdaCapturesChecker was erroneously emitting a warning for
a DeclRefExpr which is passed in as an argument to a no-escape function argument. The bug was
caused by findLambdaInArg not adding DeclRefExpr to the ignored set even when a lambda was
identified as an argument.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Aug 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 22, 2025

@llvm/pr-subscribers-clang

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

Author: Ryosuke Niwa (rniwa)

Changes

Fix a bug that webkit.UncountedLambdaCapturesChecker was erroneously emitting a warning for a DeclRefExpr which is passed in as an argument to a no-escape function argument. The bug was caused by findLambdaInArg not adding DeclRefExpr to the ignored set even when a lambda was identified as an argument.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp (+6-1)
  • (modified) clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp (+23)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
index 27a91132fa9be..81b08dd0831da 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
@@ -232,14 +232,19 @@ class RawPtrRefLambdaCapturesChecker
         if (!Init)
           return nullptr;
         if (auto *Lambda = dyn_cast<LambdaExpr>(Init)) {
+          DeclRefExprsToIgnore.insert(DRE);
           updateIgnoreList();
           return Lambda;
         }
         TempExpr = dyn_cast<CXXBindTemporaryExpr>(Init->IgnoreParenCasts());
         if (!TempExpr)
           return nullptr;
+        auto *Lambda = dyn_cast_or_null<LambdaExpr>(TempExpr->getSubExpr());
+        if (!Lambda)
+          return nullptr;
+        DeclRefExprsToIgnore.insert(DRE);
         updateIgnoreList();
-        return dyn_cast_or_null<LambdaExpr>(TempExpr->getSubExpr());
+        return Lambda;
       }
 
       void checkCalleeLambda(CallExpr *CE) {
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
index 3079f8e833fcd..1dfab439c58dd 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
@@ -448,4 +448,27 @@ void ranges_for_each(RefCountable* obj) {
     obj->method();
     ++(*static_cast<unsigned*>(item));
   });
+}
+
+class RefCountedObj {
+public:
+  void ref();
+  void deref();
+
+  void call() const;
+  void callLambda([[clang::noescape]] const WTF::Function<void ()>& callback) const;
+  void doSomeWork() const;
+};
+
+void RefCountedObj::callLambda([[clang::noescape]] const WTF::Function<void ()>& callback) const
+{
+    callback();
+}
+
+void RefCountedObj::call() const
+{
+    auto lambda = [&] {
+        doSomeWork();
+    };
+    callLambda(lambda);
 }
\ No newline at end of file

@rniwa rniwa requested a review from t-rasmud August 22, 2025 20:11
@rniwa rniwa force-pushed the fix-webkit-lambda-checker-declref-in-noescape-arg branch from 960a0ab to 7869ff9 Compare September 9, 2025 08:27
Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rniwa
Copy link
Contributor Author

rniwa commented Sep 9, 2025

Thanks for the review!

@rniwa rniwa merged commit e1ca3d9 into llvm:main Sep 9, 2025
9 checks passed
@rniwa rniwa deleted the fix-webkit-lambda-checker-declref-in-noescape-arg branch September 9, 2025 20:46
rniwa added a commit to rniwa/llvm-project that referenced this pull request Sep 9, 2025
…a in an no-escape argument (llvm#155025)

Fix a bug that webkit.UncountedLambdaCapturesChecker was erroneously
emitting a warning for a DeclRefExpr which is passed in as an argument
to a no-escape function argument. The bug was caused by findLambdaInArg
not adding DeclRefExpr to the ignored set even when a lambda was
identified as an argument.
adrian-prantl pushed a commit to swiftlang/llvm-project that referenced this pull request Sep 14, 2025
…a in an no-escape argument (llvm#155025)

Fix a bug that webkit.UncountedLambdaCapturesChecker was erroneously
emitting a warning for a DeclRefExpr which is passed in as an argument
to a no-escape function argument. The bug was caused by findLambdaInArg
not adding DeclRefExpr to the ignored set even when a lambda was
identified as an argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants