[clang] [clang-tools-extra] [clang-tidy] fix misc-unconventional-assign-operator entity match (PR #154430)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 19 14:55:11 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Matheus Izvekov (mizvekov)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/154430.diff
4 Files Affected:
- (modified) clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp (+5-3)
- (modified) clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp (+8)
- (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+8)
- (modified) clang/lib/ASTMatchers/Dynamic/Registry.cpp (+1)
``````````diff
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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/154430
More information about the cfe-commits
mailing list