[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:54:55 PST 2026
https://github.com/Devanshi-cmd created https://github.com/llvm/llvm-project/pull/180007
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`.
>From 3d92fb8414c139b2c2a78ae7c89d278c3a0e1670 Mon Sep 17 00:00:00 2001
From: Devanshi-cmd <devanshitiwari250 at gmail.com>
Date: Fri, 6 Feb 2026 00:21:47 +0530
Subject: [PATCH] [clang-tidy] Fix readability-use-anyofallof false positive
for shared_ptr
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`.
---
clang/lib/Analysis/ExprMutationAnalyzer.cpp | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
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.
More information about the cfe-commits
mailing list