[clang-tools-extra] [clang-tidy] fix misc-unconventional-assign-operator false positive for deducing this (PR #107409)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 5 07:48:21 PDT 2024
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/107409
Fixes: #107119
When meeting c++23 deducing this, we should skip the first parameter
>From b845ad06929470d9b20ce9170c49939ebe8a901c Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 5 Sep 2024 22:21:57 +0800
Subject: [PATCH] [clang-tidy] fix misc-unconventional-assign-operator false
positive for deducing this
Fixes: #107119
When meeting c++23 deducing this, we should skip the first parameter
---
.../misc/UnconventionalAssignOperatorCheck.cpp | 15 ++++++++++++---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../misc/unconventional-assign-operator-cxx23.cpp | 10 ++++++++++
3 files changed, 26 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator-cxx23.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
index 42c4b6edb6d209..afc4897eeb2aee 100644
--- a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
@@ -14,6 +14,16 @@ using namespace clang::ast_matchers;
namespace clang::tidy::misc {
+namespace {
+
+AST_MATCHER_P(CXXMethodDecl, firstParameter,
+ ast_matchers::internal::Matcher<ParmVarDecl>, InnerMatcher) {
+ unsigned N = Node.isExplicitObjectMemberFunction() ? 1 : 0;
+ return (N < Node.parameters().size() &&
+ InnerMatcher.matches(*Node.parameters()[N], Finder, Builder));
+}
+} // namespace
+
void UnconventionalAssignOperatorCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
const auto HasGoodReturnType =
@@ -29,7 +39,7 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
hasName("operator="), ofClass(recordDecl().bind("class")))
.bind("method");
const auto IsSelfAssign =
- cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf))))
+ cxxMethodDecl(IsAssign, firstParameter(parmVarDecl(hasType(IsSelf))))
.bind("method");
Finder->addMatcher(
@@ -41,8 +51,7 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
rValueReferenceType(pointee(isConstQualified()))))));
Finder->addMatcher(
- cxxMethodDecl(IsSelfAssign,
- hasParameter(0, parmVarDecl(hasType(BadSelf))))
+ cxxMethodDecl(IsSelfAssign, firstParameter(parmVarDecl(hasType(BadSelf))))
.bind("ArgumentType"),
this);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6999c1ef2ea4b0..1863e09c50f200 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -112,6 +112,10 @@ Changes in existing checks
<clang-tidy/checks/modernize/use-std-format>` check to support replacing
member function calls too.
+- Improved :doc:`misc-unconventional-assign-operator
+ <clang-tidy/checks/misc/unconventional-assign-operator>` check to avoid
+ false positive for c++23 deducing this.
+
- Improved :doc:`modernize-use-std-print
<clang-tidy/checks/modernize/use-std-print>` check to support replacing
member function calls too.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator-cxx23.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator-cxx23.cpp
new file mode 100644
index 00000000000000..d947df164be868
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator-cxx23.cpp
@@ -0,0 +1,10 @@
+// RUN: %check_clang_tidy -std=c++23 %s misc-unconventional-assign-operator %t
+
+struct BadArgument {
+ BadArgument &operator=(this BadArgument& self, BadArgument &);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should take 'BadArgument const&', 'BadArgument&&' or 'BadArgument'
+};
+
+struct GoodArgument {
+ GoodArgument &operator=(this GoodArgument& self, GoodArgument const &);
+};
More information about the cfe-commits
mailing list