Index: clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h +++ clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h @@ -0,0 +1,35 @@ +//===--- AssignOperatorSignatureCheck.h - clang-tidy ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGN_OPERATOR_SIGNATURE_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGN_OPERATOR_SIGNATURE_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { + +/// \brief Finds declarations of assign operators with the wrong return and/or +/// argument types. +/// +/// The return type must be \c Class&. +/// Works with move-assign and assign by value. +/// Private and deleted operators are ignored. +class AssignOperatorSignatureCheck : public ClangTidyCheck { +public: + AssignOperatorSignatureCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGN_OPERATOR_SIGNATURE_CHECK_H Index: clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp @@ -0,0 +1,67 @@ +//===--- AssignOperatorSignatureCheck.cpp - clang-tidy ----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "AssignOperatorSignatureCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void AssignOperatorSignatureCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + const auto HasGoodReturnType = methodDecl(returns(lValueReferenceType(pointee( + unless(isConstQualified()), hasDeclaration(equalsBoundNode("class")))))); + + const auto IsSelf = qualType( + anyOf(hasDeclaration(equalsBoundNode("class")), + referenceType(pointee(hasDeclaration(equalsBoundNode("class")))))); + const auto IsSelfAssign = + methodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())), + hasName("operator="), ofClass(recordDecl().bind("class")), + hasParameter(0, parmVarDecl(hasType(IsSelf)))).bind("method"); + + Finder->addMatcher( + methodDecl(IsSelfAssign, unless(HasGoodReturnType)).bind("ReturnType"), + this); + + const auto BadSelf = referenceType( + anyOf(lValueReferenceType(pointee(unless(isConstQualified()))), + rValueReferenceType(pointee(isConstQualified())))); + + Finder->addMatcher( + methodDecl(IsSelfAssign, hasParameter(0, parmVarDecl(hasType(BadSelf)))) + .bind("ArgumentType"), + this); + + Finder->addMatcher(methodDecl(IsSelfAssign, isConst()).bind("Const"), this); +} + + +void AssignOperatorSignatureCheck::check( + const MatchFinder::MatchResult &Result) { + const auto* Method = Result.Nodes.getNodeAs("method"); + std::string Name = Method->getParent()->getName(); + + static const char *Messages[][2] = { + {"ReturnType", "operator=() should return '%0&'"}, + {"ArgumentType", "operator=() should take '%0 const&', '%0&&' or '%0'"}, + {"Const", "operator=() should not be marked 'const'"}, + }; + + for (const auto& Message : Messages) { + if (Result.Nodes.getNodeAs(Message[0])) + diag(Method->getLocStart(), Message[1]) << Name; + } +} + +} // namespace tidy +} // namespace clang Index: clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt +++ clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyMiscModule ArgumentCommentCheck.cpp + AssignOperatorSignatureCheck.cpp BoolPointerImplicitConversion.cpp InefficientAlgorithmCheck.cpp MiscTidyModule.cpp Index: clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp +++ clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "ArgumentCommentCheck.h" +#include "AssignOperatorSignatureCheck.h" #include "BoolPointerImplicitConversion.h" #include "InefficientAlgorithmCheck.h" #include "SwappedArgumentsCheck.h" @@ -26,6 +27,8 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("misc-argument-comment"); + CheckFactories.registerCheck( + "misc-assign-operator-signature"); CheckFactories.registerCheck( "misc-bool-pointer-implicit-conversion"); CheckFactories.registerCheck( Index: clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp +++ clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp @@ -0,0 +1,52 @@ +// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-assign-operator-signature %t +// REQUIRES: shell + +struct Good { + Good& operator=(const Good&); + Good& operator=(Good&&); + + // Assign from other types is fine too. + Good& operator=(int); +}; + +struct AlsoGood { + // By value is also fine. + AlsoGood& operator=(AlsoGood); +}; + +struct BadReturn { + void operator=(const BadReturn&); + // 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 +}; +struct BadReturn2 { + BadReturn2&& operator=(const BadReturn2&); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad + int operator=(BadReturn2&&); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad +}; + +struct BadArgument { + BadArgument& operator=(BadArgument&); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should take 'BadArgument const&', 'BadArgument&&' or 'BadArgument' + BadArgument& operator=(const BadArgument&&); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should take 'BadAr +}; + +struct BadModifier { + BadModifier& operator=(const BadModifier&) const; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should not be marked 'const' +}; + +struct Deleted { + // We don't check the return value of deleted operators. + void operator=(const Deleted&) = delete; + void operator=(Deleted&&) = delete; +}; + +class Private { + // We don't check the return value of private operators. + // Pre-C++11 way of disabling assignment. + void operator=(const Private &); +};