Index: clang-tidy/google/AssignOperatorSignatureCheck.h =================================================================== --- /dev/null +++ clang-tidy/google/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_GOOGLE_ASSIGN_OPERATOR_SIGNATURE_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_ASSIGN_OPERATOR_SIGNATURE_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace readability { + +/// \brief Finds declarations of assign operators with the wrong return type. +/// +/// The return type must be \c Class&. +/// Works with move-assign. 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 readability +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_ASSIGN_OPERATOR_SIGNATURE_CHECK_H Index: clang-tidy/google/AssignOperatorSignatureCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/google/AssignOperatorSignatureCheck.cpp @@ -0,0 +1,69 @@ +//===--- 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 { +namespace readability { + +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())), 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 readability +} // namespace tidy +} // namespace clang Index: clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tidy/google/CMakeLists.txt +++ clang-tidy/google/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyGoogleModule + AssignOperatorSignatureCheck.cpp AvoidCStyleCastsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp Index: clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tidy/google/GoogleTidyModule.cpp +++ clang-tidy/google/GoogleTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "AssignOperatorSignatureCheck.h" #include "AvoidCStyleCastsCheck.h" #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" @@ -50,6 +51,8 @@ "google-runtime-member-string-references"); CheckFactories.registerCheck( "google-runtime-memset"); + CheckFactories.registerCheck( + "google-readability-assign-operator"); CheckFactories.registerCheck( "google-readability-casting"); CheckFactories.registerCheck( Index: test/clang-tidy/google-assign-operator.cpp =================================================================== --- /dev/null +++ test/clang-tidy/google-assign-operator.cpp @@ -0,0 +1,52 @@ +// RUN: $(dirname %s)/check_clang_tidy.sh %s google-readability-assign-operator %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&' [google-readability-assign-operator] + 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 &); +};