[clang-tools-extra] [clang-tidy] fix misc-unconventional-assign-operator false positive for deducing this (PR #107409)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 5 07:49:00 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Congcong Cai (HerrCai0907)
<details>
<summary>Changes</summary>
Fixes: #<!-- -->107119
When meeting c++23 deducing this, we should skip the first parameter
---
Full diff: https://github.com/llvm/llvm-project/pull/107409.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp (+12-3)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (added) clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator-cxx23.cpp (+10)
``````````diff
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 &);
+};
``````````
</details>
https://github.com/llvm/llvm-project/pull/107409
More information about the cfe-commits
mailing list