[clang-tools-extra] cdd2c06 - [clang-tidy] fix misc-unconventional-assign-operator false positive for deducing this (#107409)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 6 04:11:30 PDT 2024
Author: Congcong Cai
Date: 2024-09-06T19:11:26+08:00
New Revision: cdd2c0693b6dd75816f64960a479aacbe4e34549
URL: https://github.com/llvm/llvm-project/commit/cdd2c0693b6dd75816f64960a479aacbe4e34549
DIFF: https://github.com/llvm/llvm-project/commit/cdd2c0693b6dd75816f64960a479aacbe4e34549.diff
LOG: [clang-tidy] fix misc-unconventional-assign-operator false positive for deducing this (#107409)
Fixes: #107119
When meeting c++23 deducing this, we should skip the first parameter
Added:
clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator-cxx23.cpp
Modified:
clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
Removed:
################################################################################
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..8d028f8863cb7a 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