[clang-tools-extra] [clang-tidy] Fix false positives with template in `misc-unconventional-assign-operator` check (PR #143292)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 16 04:59:27 PDT 2025
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/143292
>From 311bc80c0e5ab787aef8476dae51bc16805d728b Mon Sep 17 00:00:00 2001
From: flovent <144676429+flovent at users.noreply.github.com>
Date: Sun, 8 Jun 2025 10:46:48 +0800
Subject: [PATCH 1/4] [clang-tidy] Fix false positives with template in
`misc-unconventional-assign-operator` check
---
.../misc/UnconventionalAssignOperatorCheck.cpp | 7 +++++--
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
.../misc/unconventional-assign-operator.cpp | 13 +++++++++++++
3 files changed, 23 insertions(+), 2 deletions(-)
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 a275d2ccfa004..394e18cd58f84 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -208,6 +208,11 @@ Changes in existing checks
<clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
on ``operator""`` with template parameters.
+- Improved :doc:`misc-unconventional-assign-operator
+ <clang-tidy/checks/misc/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-use-internal-linkage
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
for function or variable in header file which contains macro expansion and
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..9384d88c38fd0 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 issue143237 {
+template<typename T>
+struct B {
+ explicit B(int) {
+ }
+
+ B& operator=(int n) {
+ // No warning
+ return *this = B(n);
+ }
+};
+}
>From db389c1e53208213ee8b987f9bf7244df12bba4c Mon Sep 17 00:00:00 2001
From: flovent <144676429+flovent at users.noreply.github.com>
Date: Sun, 8 Jun 2025 10:59:08 +0800
Subject: [PATCH 2/4] fix release notes
---
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 394e18cd58f84..da502c6f40b18 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -209,7 +209,7 @@ Changes in existing checks
on ``operator""`` with template parameters.
- Improved :doc:`misc-unconventional-assign-operator
- <clang-tidy/checks/misc/misc-unconventional-assign-operator>` check by fixing
+ <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=...``).
>From 101d19bb818c42d65f5af663f3d84e9c7e229910 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sun, 8 Jun 2025 22:39:26 +0800
Subject: [PATCH 3/4] fix position of release note
---
clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index da502c6f40b18..70939a5fcd9c8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -204,15 +204,15 @@ 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-unused-using-decls
- <clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
- on ``operator""`` with template parameters.
-
- 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.
+
- Improved :doc:`misc-use-internal-linkage
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
for function or variable in header file which contains macro expansion and
>From eda529eb6e976843c94778134ff61c54a9f4bc31 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Mon, 16 Jun 2025 19:59:15 +0800
Subject: [PATCH 4/4] apply suggestion
---
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 70939a5fcd9c8..6068d0fb70e26 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -207,7 +207,7 @@ Changes in existing checks
- 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=...``).
+ 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
More information about the cfe-commits
mailing list