[clang-tidy] Skip copy assignment operators with nonstandard return types
authorAlexander Shaposhnikov <ashaposhnikov@google.com>
Wed, 31 Aug 2022 09:13:21 +0000 (09:13 +0000)
committerAlexander Shaposhnikov <ashaposhnikov@google.com>
Fri, 2 Sep 2022 22:43:39 +0000 (22:43 +0000)
Skip copy assignment operators with nonstandard return types
since they cannot be defaulted.

Test plan: ninja check-clang-tools

Differential revision: https://reviews.llvm.org/D133006

clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default-copy.cpp

index f321c0b..2de677d 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "UseEqualsDefaultCheck.h"
 #include "../utils/LexerUtils.h"
+#include "../utils/Matchers.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Lexer.h"
@@ -247,7 +248,12 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
                     // isCopyAssignmentOperator() allows the parameter to be
                     // passed by value, and in this case it cannot be
                     // defaulted.
-                    hasParameter(0, hasType(lValueReferenceType())))
+                    hasParameter(0, hasType(lValueReferenceType())),
+                    // isCopyAssignmentOperator() allows non lvalue reference
+                    // return types, and in this case it cannot be defaulted.
+                    returns(qualType(hasCanonicalType(
+                        allOf(lValueReferenceType(pointee(type())),
+                              unless(matchers::isReferenceToConst()))))))
           .bind(SpecialFunction),
       this);
 }
index e0907b2..7c91ea0 100644 (file)
@@ -145,7 +145,8 @@ Changes in existing checks
   check.
 
   The check now skips unions since in this case a default constructor with empty body
-  is not equivalent to the explicitly defaulted one. The check is restricted to c++11-or-later.
+  is not equivalent to the explicitly defaulted one. The check also skips copy assignment
+  operators with nonstandard return types. The check is restricted to c++11-or-later.
 
 Removed checks
 ^^^^^^^^^^^^^^
index 78a03f0..70fc521 100644 (file)
@@ -444,6 +444,13 @@ IL &WRT::operator=(const WRT &Other) {
   return *this;
 }
 
+// Wrong return type.
+struct WRTConstRef {
+  const WRTConstRef &operator = (const WRTConstRef &) {
+    return *this;
+  }
+};
+
 // Try-catch.
 struct ITC {
   ITC(const ITC &Other)