[clang] [clang-tools-extra] [clang-tidy] fix misc-unconventional-assign-operator entity match (PR #154430)
Matheus Izvekov via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 19 14:54:43 PDT 2025
https://github.com/mizvekov created https://github.com/llvm/llvm-project/pull/154430
Makes sure UnconventionalAssignOperatorCheck checks if the types reference the same entity, not the exact declaration.
This adds a new matcher to support this check.
This fixes a regression introduced by #147835. Since this regression was never released, there are no release notes.
Fixes #153770
>From 08806abfbfbc9073964b9889007689c39aef8263 Mon Sep 17 00:00:00 2001
From: Matheus Izvekov <mizvekov at gmail.com>
Date: Tue, 19 Aug 2025 18:49:24 -0300
Subject: [PATCH] [clang-tidy] fix misc-unconventional-assign-operator entity
match
Makes sure UnconventionalAssignOperatorCheck checks if the types reference the
same entity, not the exact declaration.
This adds a new matcher to support this check.
This fixes a regression introduced by #147835. Since this regression was never
released, there are no release notes.
Fixes #153770
---
.../clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp | 8 +++++---
.../checkers/misc/unconventional-assign-operator.cpp | 8 ++++++++
clang/include/clang/ASTMatchers/ASTMatchers.h | 8 ++++++++
clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 +
4 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
index 3fdaf9239f6af..8200239b982a0 100644
--- a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
@@ -29,11 +29,13 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
const auto HasGoodReturnType =
cxxMethodDecl(returns(hasCanonicalType(lValueReferenceType(pointee(
unless(isConstQualified()),
- anyOf(autoType(), hasDeclaration(equalsBoundNode("class"))))))));
+ anyOf(autoType(),
+ hasDeclaration(declaresSameEntityAsBoundNode("class"))))))));
const auto IsSelf = qualType(hasCanonicalType(
- anyOf(hasDeclaration(equalsBoundNode("class")),
- referenceType(pointee(hasDeclaration(equalsBoundNode("class")))))));
+ anyOf(hasDeclaration(declaresSameEntityAsBoundNode("class")),
+ referenceType(pointee(
+ hasDeclaration(declaresSameEntityAsBoundNode("class")))))));
const auto IsAssign =
cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
hasName("operator="), ofClass(recordDecl().bind("class")))
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp
index 28b53ae4af63d..d7a5797cc2844 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp
@@ -176,3 +176,11 @@ struct TemplateAssignment {
}
};
}
+
+namespace GH153770 {
+ struct A;
+ struct A {
+ A() = default;
+ A& operator=(const A&) = default;
+ };
+} // namespace GH153770
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index cbd931cabd806..289711ddfb1b8 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -5739,6 +5739,14 @@ AST_POLYMORPHIC_MATCHER_P(equalsBoundNode,
return Builder->removeBindings(Predicate);
}
+/// Matches a declaration if it declares the same entity as the node previously
+/// bound to \p ID.
+AST_MATCHER_P(Decl, declaresSameEntityAsBoundNode, std::string, ID) {
+ return Builder->removeBindings([&](const internal::BoundNodesMap &Nodes) {
+ return !clang::declaresSameEntity(&Node, Nodes.getNodeAs<Decl>(ID));
+ });
+}
+
/// Matches the condition variable statement in an if statement.
///
/// Given
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 7f6a6aaed1734..48a7b91969aef 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -240,6 +240,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(enumDecl);
REGISTER_MATCHER(enumType);
REGISTER_MATCHER(equalsBoundNode);
+ REGISTER_MATCHER(declaresSameEntityAsBoundNode);
REGISTER_MATCHER(equalsIntegralValue);
REGISTER_MATCHER(explicitCastExpr);
REGISTER_MATCHER(exportDecl);
More information about the cfe-commits
mailing list