[clang] [clang-tidy] Fix readability-use-anyofallof false positive for shared_ptr (PR #180007)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 5 10:55:46 PST 2026


llvmbot wrote:


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

@llvm/pr-subscribers-clang

Author: Devanshi (Devanshi-cmd)

<details>
<summary>Changes</summary>

Fixes issue #<!-- -->179941 where `readability-use-anyofallof` was incorrectly suggesting `std::any_of` for loops that mutate data through `std::shared_ptr`. The checker detected mutations through `unique_ptr` but not `shared_ptr`, even though both use `operator->` the same way. Extended the mutation detection in `ExprMutationAnalyzer` to also match `std::shared_ptr` and `std::weak_ptr`.

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


1 Files Affected:

- (modified) clang/lib/Analysis/ExprMutationAnalyzer.cpp (+7-1) 


``````````diff
diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
index 86d7dcab807d3..1ae16e285effe 100644
--- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp
+++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
@@ -449,7 +449,13 @@ ExprMutationAnalyzer::Analyzer::findDirectMutation(const Expr *Exp) {
   const auto AsOperatorArrowThis = cxxOperatorCallExpr(
       hasOverloadedOperatorName("->"),
       callee(
-          cxxMethodDecl(ofClass(isMoveOnly()), returns(nonConstPointerType()))),
+          cxxMethodDecl(
+             anyOf(
+              ofClass(isMoveOnly()),
+              ofClass(hasAnyName("std::shared_ptr", "std::weak_ptr"))
+            ),
+            returns(nonConstPointerType())
+          )),
       argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)));
 
   // Used as non-const-ref argument when calling a function.

``````````

</details>


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


More information about the cfe-commits mailing list