Index: clang-tidy/misc/AssignOperatorSignatureCheck.h =================================================================== --- clang-tidy/misc/AssignOperatorSignatureCheck.h +++ clang-tidy/misc/AssignOperatorSignatureCheck.h @@ -24,10 +24,13 @@ /// * Private and deleted operators are ignored. class AssignOperatorSignatureCheck : public ClangTidyCheck { public: - AssignOperatorSignatureCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + AssignOperatorSignatureCheck(StringRef Name, ClangTidyContext *Context); + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool CheckAllAssignOperators; }; } // namespace misc Index: clang-tidy/misc/AssignOperatorSignatureCheck.cpp =================================================================== --- clang-tidy/misc/AssignOperatorSignatureCheck.cpp +++ clang-tidy/misc/AssignOperatorSignatureCheck.cpp @@ -17,6 +17,16 @@ namespace tidy { namespace misc { +AssignOperatorSignatureCheck::AssignOperatorSignatureCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + CheckAllAssignOperators(Options.get("CheckAllAssignOperators", false)) {} + +void AssignOperatorSignatureCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "CheckAllAssignOperators", CheckAllAssignOperators); +} + void AssignOperatorSignatureCheck::registerMatchers( ast_matchers::MatchFinder *Finder) { // Only register the matchers for C++; the functionality currently does not @@ -31,16 +41,19 @@ const auto IsSelf = qualType( anyOf(hasDeclaration(equalsBoundNode("class")), referenceType(pointee(hasDeclaration(equalsBoundNode("class")))))); - const auto IsSelfAssign = + const auto IsAssign = cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())), - hasName("operator="), ofClass(recordDecl().bind("class")), - hasParameter(0, parmVarDecl(hasType(IsSelf)))) + hasName("operator="), ofClass(recordDecl().bind("class"))) + .bind("method"); + const auto IsSelfAssign = + cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf)))) .bind("method"); Finder->addMatcher( - cxxMethodDecl(IsSelfAssign, unless(HasGoodReturnType)).bind("ReturnType"), + cxxMethodDecl(CheckAllAssignOperators ? IsAssign : IsSelfAssign, + unless(HasGoodReturnType)) + .bind("ReturnType"), this); - const auto BadSelf = referenceType( anyOf(lValueReferenceType(pointee(unless(isConstQualified()))), rValueReferenceType(pointee(isConstQualified())))); @@ -58,14 +71,13 @@ void AssignOperatorSignatureCheck::check( const MatchFinder::MatchResult &Result) { - const auto* Method = Result.Nodes.getNodeAs("method"); + const auto *Method = Result.Nodes.getNodeAs("method"); std::string Name = Method->getParent()->getName(); static const char *const Messages[][2] = { {"ReturnType", "operator=() should return '%0&'"}, {"ArgumentType", "operator=() should take '%0 const&', '%0&&' or '%0'"}, - {"cv", "operator=() should not be marked '%1'"} - }; + {"cv", "operator=() should not be marked '%1'"}}; for (const auto &Message : Messages) { if (Result.Nodes.getNodeAs(Message[0])) Index: test/clang-tidy/misc-assign-operator-signature.cpp =================================================================== --- test/clang-tidy/misc-assign-operator-signature.cpp +++ test/clang-tidy/misc-assign-operator-signature.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s misc-assign-operator-signature %t +// RUN: %check_clang_tidy %s misc-assign-operator-signature %t -- -config="{CheckOptions: [{key: misc-assign-operator-signature.CheckAllAssignOperators, value: 1}]}" -- struct Good { Good& operator=(const Good&); @@ -18,6 +18,8 @@ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'BadReturn&' [misc-assign-operator-signature] const BadReturn& operator=(BadReturn&&); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad + void operator=(int); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad }; struct BadReturn2 { BadReturn2&& operator=(const BadReturn2&);