[clang-tools-extra] [clang-tidy] `doesNotMutateObject`: Handle calls to member functions … (PR #94362)

Clement Courbet via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 02:05:38 PDT 2024


================
@@ -36,6 +36,111 @@ void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
     Nodes.insert(Match.getNodeAs<Node>(ID));
 }
 
+// If `D` has a const-qualified overload with otherwise identical
+// ref-qualifiers, returns that overload.
+const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
+  assert(!D.isConst());
+
+  DeclContext::lookup_result lookup_result =
+      D.getParent()->lookup(D.getNameInfo().getName());
+  if (lookup_result.isSingleResult()) {
+    // No overload.
+    return nullptr;
+  }
+  for (const Decl *overload : lookup_result) {
+    const CXXMethodDecl *candidate = dyn_cast<CXXMethodDecl>(overload);
+    if (candidate && !candidate->isDeleted() && candidate->isConst() &&
+        candidate->getRefQualifier() == D.getRefQualifier()) {
----------------
legrosbuffle wrote:

Overloading would be allowed in this case, but I think having a `const` overload with the same *parameter* types is enough to say that the use is immutable (not that the case when the return value is non-const and the object might be modified through the return value is caught by `(C)`). I've added a test to make this explicit (`weird_overload()`).

That being said, your comment made me realize that we were not checking that the parameter types were the same. Done and added tests (`at(Tag1)`). Thanks :)

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


More information about the cfe-commits mailing list