Index: clang-tidy/google/AssignOperatorSignatureCheck.h =================================================================== --- /dev/null +++ clang-tidy/google/AssignOperatorSignatureCheck.h @@ -0,0 +1,34 @@ +//===--- 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: + using ClangTidyCheck::ClangTidyCheck; + 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,42 @@ +//===--- 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) { + Finder->addMatcher( + methodDecl(unless(isDeleted()), unless(isPrivate()), hasName("operator="), + ofClass(recordDecl().bind("class")), + methodDecl(unless(returns(referenceType( + pointee(unless(isConstQualified()), + hasDeclaration(equalsBoundNode("class")))))))) + .bind("method"), + this); +} + + +void AssignOperatorSignatureCheck::check( + const MatchFinder::MatchResult &Result) { + const auto* Method = Result.Nodes.getNodeAs("method"); + diag(Method->getLocStart(), (Twine("operator=() should return ") + + Method->getParent()->getName() + "&.").str()); +} + +} // 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,26 @@ +// RUN: $(dirname %s)/check_clang_tidy.sh %s google-readability-assign-operator %t +// REQUIRES: shell + +struct Good { + Good& operator=(const Good&); + Good& operator=(Good&&); +}; + +struct Bad { + void operator=(const Bad&); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return Bad&. [google-readability-assign-operator] + const Bad& operator=(Bad&&); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return Bad&. [google-readability-assign-operator] +}; + +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 &); +};