Skip to content

[alpha.webkit.UnretainedCallArgsChecker] Don't emit a warning for RetainPtr::operator= #135526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Apr 13, 2025

Generalize the check for operator= so that it works for RetainPtr and CheckedPtr instead of just RefPtr.

…ainPtr::operator=

Generalize the check for operator= so that it works for RetainPtr and CheckedPtr
instead of just RefPtr.
@rniwa rniwa requested a review from t-rasmud April 13, 2025 07:14
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Apr 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 13, 2025

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

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

Generalize the check for operator= so that it works for RetainPtr and CheckedPtr instead of just RefPtr.


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

5 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefCallArgsChecker.cpp (+1-1)
  • (modified) clang/test/Analysis/Checkers/WebKit/call-args-checked.cpp (+14-4)
  • (modified) clang/test/Analysis/Checkers/WebKit/mock-types.h (+1-1)
  • (modified) clang/test/Analysis/Checkers/WebKit/objc-mock-types.h (+1-5)
  • (modified) clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm (+10)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefCallArgsChecker.cpp
index 20f7e9703c67c..51353d5acbae2 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefCallArgsChecker.cpp
@@ -264,7 +264,7 @@ class RawPtrRefCallArgsChecker
         auto *callee = MemberOp->getDirectCallee();
         if (auto *calleeDecl = dyn_cast<CXXMethodDecl>(callee)) {
           if (const CXXRecordDecl *classDecl = calleeDecl->getParent()) {
-            if (isRefCounted(classDecl))
+            if (isSafePtr(classDecl))
               return true;
           }
         }
diff --git a/clang/test/Analysis/Checkers/WebKit/call-args-checked.cpp b/clang/test/Analysis/Checkers/WebKit/call-args-checked.cpp
index 49b6bfcd7cadf..e24b04dcd3cf9 100644
--- a/clang/test/Analysis/Checkers/WebKit/call-args-checked.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/call-args-checked.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncheckedCallArgsChecker -verify %s
 
 #include "mock-types.h"
 
@@ -10,10 +10,10 @@ namespace call_args_unchecked_uncounted {
 
 static void foo() {
   someFunction(makeObj());
-  // expected-warning@-1{{Call argument is uncounted and unsafe [alpha.webkit.UncountedCallArgsChecker]}}
+  // expected-warning@-1{{Call argument is unchecked and unsafe [alpha.webkit.UncheckedCallArgsChecker]}}
 }
 
-} // namespace call_args_checked
+} // namespace call_args_unchecked_uncounted
 
 namespace call_args_checked {
 
@@ -35,7 +35,7 @@ static void baz() {
 namespace call_args_default {
 
 void someFunction(RefCountableAndCheckable* = makeObj());
-// expected-warning@-1{{Call argument is uncounted and unsafe [alpha.webkit.UncountedCallArgsChecker]}}
+// expected-warning@-1{{Call argument is unchecked and unsafe [alpha.webkit.UncheckedCallArgsChecker]}}
 void otherFunction(RefCountableAndCheckable* = makeObjChecked().ptr());
 
 void foo() {
@@ -44,3 +44,13 @@ void foo() {
 }
 
 }
+
+namespace call_args_checked_assignment {
+
+CheckedObj* provide();
+void foo() {
+  CheckedPtr<CheckedObj> ptr;
+  ptr = provide();
+}
+
+}
diff --git a/clang/test/Analysis/Checkers/WebKit/mock-types.h b/clang/test/Analysis/Checkers/WebKit/mock-types.h
index a1f0cc8b046b9..a03d31870ee0d 100644
--- a/clang/test/Analysis/Checkers/WebKit/mock-types.h
+++ b/clang/test/Analysis/Checkers/WebKit/mock-types.h
@@ -249,7 +249,7 @@ template <typename T> struct CheckedPtr {
   T *get() const { return t; }
   T *operator->() const { return t; }
   T &operator*() const { return *t; }
-  CheckedPtr &operator=(T *) { return *this; }
+  CheckedPtr &operator=(T *);
   operator bool() const { return t; }
 };
 
diff --git a/clang/test/Analysis/Checkers/WebKit/objc-mock-types.h b/clang/test/Analysis/Checkers/WebKit/objc-mock-types.h
index 3f075ca0a6e5b..51de81ac0f033 100644
--- a/clang/test/Analysis/Checkers/WebKit/objc-mock-types.h
+++ b/clang/test/Analysis/Checkers/WebKit/objc-mock-types.h
@@ -216,11 +216,7 @@ template <typename T> struct RetainPtr {
   PtrType get() const { return t; }
   PtrType operator->() const { return t; }
   T &operator*() const { return *t; }
-  RetainPtr &operator=(PtrType t) {
-    RetainPtr o(t);
-    swap(o);
-    return *this;
-  }
+  RetainPtr &operator=(PtrType t);
   PtrType leakRef()
   {
     PtrType s = t;
diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm b/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
index dd21864300387..0667e4964f1a8 100644
--- a/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
+++ b/clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
@@ -271,6 +271,16 @@ void foo() {
   }
 }
 
+namespace cxx_assignment_op {
+
+  SomeObj* provide();
+  void foo() {
+    RetainPtr<SomeObj> ptr;
+    ptr = provide();
+  }
+
+}
+
 namespace call_with_ptr_on_ref {
   RetainPtr<SomeObj> provideProtected();
   RetainPtr<CFMutableArrayRef> provideProtectedCF();

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 Apr 14, 2025

Thanks for the review!

@rniwa rniwa merged commit 2206e15 into llvm:main Apr 14, 2025
14 checks passed
@rniwa rniwa deleted the webkit-unchecked-unretained-call-args-assignment branch April 14, 2025 22:03
rniwa added a commit to rniwa/llvm-project that referenced this pull request Apr 16, 2025
…ainPtr::operator= (llvm#135526)

Generalize the check for operator= so that it works for RetainPtr and
CheckedPtr instead of just RefPtr.
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…ainPtr::operator= (llvm#135526)

Generalize the check for operator= so that it works for RetainPtr and
CheckedPtr instead of just RefPtr.
rniwa added a commit to rniwa/llvm-project that referenced this pull request Apr 22, 2025
…ainPtr::operator= (llvm#135526)

Generalize the check for operator= so that it works for RetainPtr and
CheckedPtr instead of just RefPtr.
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