[clang-tools-extra] 6c153e5 - [clang-tidy] Fix false positives with template in `misc-unconventional-assign-operator` check (#143292)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 6 05:06:21 PDT 2025
Author: flovent
Date: 2025-07-06T15:06:18+03:00
New Revision: 6c153e50f8b23dbd422c3f9eedf65344839bbbb1
URL: https://github.com/llvm/llvm-project/commit/6c153e50f8b23dbd422c3f9eedf65344839bbbb1
DIFF: https://github.com/llvm/llvm-project/commit/6c153e50f8b23dbd422c3f9eedf65344839bbbb1.diff
LOG: [clang-tidy] Fix false positives with template in `misc-unconventional-assign-operator` check (#143292)
Fix false positives when copy assignment operator function in a template
class returns the result of another assignment to `*this`, this check
doesn't consider this situation that there will be a `BinaryOperator`
for assignment rather than `CXXOperatorCallExpr` since `this`'s type is
dependent.
Closes #143237.
Added:
Modified:
clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
index afc4897eeb2ae..3fdaf9239f6af 100644
--- a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
@@ -66,8 +66,11 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
hasArgument(0, cxxThisExpr())),
cxxOperatorCallExpr(
hasOverloadedOperatorName("="),
- hasArgument(
- 0, unaryOperator(hasOperatorName("*"),
+ hasArgument(0, unaryOperator(hasOperatorName("*"),
+ hasUnaryOperand(cxxThisExpr())))),
+ binaryOperator(
+ hasOperatorName("="),
+ hasLHS(unaryOperator(hasOperatorName("*"),
hasUnaryOperand(cxxThisExpr())))))))));
const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f8f183e9de1cc..e021d6350694e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -266,6 +266,11 @@ Changes in existing checks
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
examples and fixing some macro related false positives.
+- Improved :doc:`misc-unconventional-assign-operator
+ <clang-tidy/checks/misc/unconventional-assign-operator>` check by fixing
+ false positives when copy assignment operator function in a template class
+ returns the result of another assignment to ``*this`` (``return *this=...``).
+
- Improved :doc:`misc-unused-using-decls
<clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
on ``operator""`` with template parameters.
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 74a22a7c083f4..28b53ae4af63d 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
@@ -163,3 +163,16 @@ struct TemplateTypeAlias {
Alias3<TypeAlias::Alias> &operator=(double) { return *this; }
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'TemplateTypeAlias&' [misc-unconventional-assign-operator]
};
+
+namespace gh143237 {
+template<typename T>
+struct TemplateAssignment {
+ explicit TemplateAssignment(int) {
+ }
+
+ TemplateAssignment& operator=(int n) {
+ // No warning
+ return *this = TemplateAssignment(n);
+ }
+};
+}
More information about the cfe-commits
mailing list